Use Java to create Excel and add content
One, the dependent jar package
Jxl.jar, using JXL to manipulate Excel
JXL is an open source Java Excel API project that can be easily manipulated by Jxl,java Microsoft Excel documents. In addition to JXL, there is a POI project Apache, you can also operate Excel, in contrast: JXL easy to use, but the function relative to the POI is weaker. POI is complex to use.
Second, the source code:
1 Packagecn.com.zfc.day006;2 3 ImportJava.io.File;4 Importjava.util.Date;5 ImportJXL. Workbook;6 ImportJxl.write.Label;7 ImportJxl.write.WritableSheet;8 ImportJxl.write.WritableWorkbook;9 ImportJxl.write.Number;Ten ImportJxl.write.DateTime; One A /** - * Use Java programs to create Excel and add content - * the * @authorZFC - * @date October 29, 2017 pm 12:20:54 - */ - Public classCreateexcel { + Public Static voidMain (String args[]) { - Try { + //Open File AWritableworkbook book = Workbook.createworkbook (NewFile ("D:/test.xls")); at //generate a worksheet named "Sheet1", with parameter 0 indicating that this is the first page -Writablesheet sheet = book.createsheet ("Sheet1", 0); - - /*First line*/ - //in the constructor of the label object, the cell position is the first row (0,0) and the cell contents are string -Label label =NewLabel (0, 0, "Zhang Fuchang"); in //add a defined cell to a worksheet - Sheet.addcell (label); to //generates a cell that holds a number, the cell position is the second column, the first row, and the contents of the cell are 1234.5 +Number number =NewNumber (1, 0, 1234.5); - Sheet.addcell (number); the //generates a saved date cell, the cell position is the third column, the first row, and the cell contents are the current date *DateTime Dtime =NewDateTime (2, 0,NewDate ()); $ Sheet.addcell (dtime);Panax Notoginseng - /*Second Line*/ the //in the constructor of the label object, the cell position is the first row (0,0) and the cell contents are string +Label =NewLabel (0, 1, "ZFC"); A //add a defined cell to a worksheet the Sheet.addcell (label); + //generates a cell that holds a number, the cell position is the second column, the first row, and the contents of the cell are 1234.5 -Number =NewNumber (1, 1, 1234); $ Sheet.addcell (number); $ //generates a saved date cell, the cell position is the third column, the first row, and the cell contents are the current date -Dtime =NewDateTime (2, 1,NewDate ()); - Sheet.addcell (dtime); the - //writing data and closing filesWuyi book.write (); the book.close (); -}Catch(Exception e) { Wu System.out.println (e); - } About } $}
Third, the Operation effect
Test.xls (stored in D packing directory)
Content in Excel
Use Java to create Excel and add content