In the open source world, there are two more influential APIs to use, one for POI and one for Jexcelapi. Where the function is relatively weak relative to the POI. However, JEXCELAPI support for Chinese is very good, the API is pure Java, and does not rely on the Windows system, even if running under Linux, it can also correctly handle Excel files. It is also important to note that this set of APIs has limited support for graphics and charts, and only the PNG format is recognized.
1. Generate Excel (Createexcel.java)
Packagetest;//Generating classes for ExcelImportJava.io.File;ImportJXL. Workbook;ImportJxl.write.Label;ImportJxl.write.WritableSheet;ImportJxl.write.WritableWorkbook; Public classCreateexcel { Public Static voidMain (String args[]) {Try { //Open FileWritableworkbook book = Workbook.createworkbook (NewFile ("Test.xls" )); //generate a worksheet named "first page", parameter 0 indicates this is the first pageWritablesheet sheet = book.createsheet ("First page", 0 ); //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 testLabel label =NewLabel (0, 0, "test" ); //add a defined cell to a worksheetSheet.addcell (label); /**/* * 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*/jxl.write.Number Number=NewJxl.write.Number (1, 0, 555.12541 ); Sheet.addcell (number); //writing data and closing filesBook.write (); Book.close (); } Catch(Exception e) {System.out.println (e); } }}
2. Read Excel (Readexcel.java)
Packagetest;//read the class for ExcelImportJava.io.File;ImportJXL. Cell;ImportJXL. Sheet;ImportJXL. Workbook; Public classReadexcel { Public Static voidMain (String args[]) {Try{Workbook Book= Workbook.getworkbook (NewFile ("Test.xls" )); //get the first sheet objectSheet Sheet = Book.getsheet (0 ); //get the first row of cells in the first columnCell cell1 = Sheet.getcell (0, 0 ); String result=cell1.getcontents (); SYSTEM.OUT.PRINTLN (result); Book.close (); } Catch(Exception e) {System.out.println (e); } }}
3. Modify Excel (Updateexcel.java)
Packagetest;ImportJava.io.File;ImportJXL. Workbook;ImportJxl.write.Label;ImportJxl.write.WritableSheet;ImportJxl.write.WritableWorkbook; Public classUpdateexcel { Public Static voidMain (String args[]) {Try { //Excel gets the fileWorkbook WB = Workbook.getworkbook (NewFile ("Test.xls" )); //open a copy of a file, and specify that the data is written back to the original fileWritableworkbook book = Workbook.createworkbook (NewFile ("Test.xls"), WB); //Add a worksheetWritablesheet sheet = book.createsheet ("Second page", 1 ); Sheet.addcell (NewLabel (0, 0, "test data on page two" )); Book.write (); Book.close (); } Catch(Exception e) {System.out.println (e); } }}
4. Formatting cells
Writablefont font1 = new Writablefont (writablefont.times, + , writablefont.bold); New Writablecellformat (font1); New Label (0, 0, "Data 4 test", FORMAT1)
5. Set cell alignment
// Assign horizontal alignment to center format1.setalignment (Jxl.format.Alignment.CENTRE); // The vertical alignment is specified as the center format1.setverticalalignment (Jxl.format.VerticalAlignment.CENTRE);
6. Merging cells
int int int int q); // The function is to merge all the cells from (M,n) to (P,Q), such as: Writablesheet sheet = book.createsheet ("First page", 0 ); // Merges all cells Sheet.mergecells (0, 0, 5, 0) in the first row of the first column to the first row of the sixth column ;
7. Set row height and column width
int int height); // The function is to specify the height of line i+1, for example: // set the height of the first row tosheet.setrowview (0,intint. width); // The function is to specify the width of column i+1, such as:// to set the width of the first column tosheet.setcolumnview (0, 30);
Jexcelapi Manipulating Excel files