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