Java uses JXL for reading and writing Excel

Source: Internet
Author: User

First, write Excel

String path= "D:/zhangxinxin/files/excels/test.xls";
Open File
File File = new file (path);
if (file.exists ()) {
File.delete ();
}
Writableworkbook excelfile = workbook.createworkbook (file);
Generate a worksheet named "first page", parameter 0 indicates this is the first page
Writablesheet sheet = excelfile.createsheet ("A", 0);
Writablesheet Sheet2 = Excelfile.createsheet ("B", 1);
Named cell position in the construction of a label object is the first column in the first row (0,0)
And the cell content is test
Label name = new label (0, 0, "name");
Label age = new label (1,0, "Provence");
Label S1name=new label (0,1, "zhangxinxin");
Label s1age = new Label (1, 1, "Hebei");
Label S2name=new label (0,2, "Zhangnasen");
Label s2age = new Label (1, 2, "Hebei");

Add a defined cell to a worksheet
Sheet.addcell (name);
Sheet.addcell (age);

Sheet2.addcell (S1name);
Sheet2.addcell (S1age);
Sheet2.addcell (S2name);
Sheet2.addcell (S2age);
/*
* Generate a cell that holds numbers must use the full package path of number, otherwise there is a syntax ambiguity cell position is the second column, the first row, the value is 789.123
*/
Number number = new Number (0, 1, 555.12541);
Sheet.addcell (number);

Writing data and closing files
Excelfile.write ();
Excelfile.close ();

Second, read Excel

String path= "D:/zhangxinxin/files/excels/test.xls";
Workbook excelfile = Workbook.getworkbook (new File (path));
Get the first sheet object
Sheet Sheet = excelfile.getsheet (0);
Get the first row of cells in the first column
Cell cell1 = Sheet.getcell (1, 1);
String result = Cell1.getcontents ();
System.out.println (Book.getnumberofsheets ());
System.out.println (Sheet.getcolumns () + "= =" + sheet.getname () + "= =" + sheet.getrows ());
for (int i = 0; i < sheet.getrows (); i++) {
for (int j = 0; J < Sheet.getcolumns (); j + +) {
result = Sheet.getcell (j, i). getcontents ();
System.out.print (Result + "");
}
System.out.println ("zhixing");
}
Excelfile.close ();

Java uses JXL for reading and writing Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.