Original https://www.cnblogs.com/wangyang108/p/6030420.html
Excel is commonly used in our work to store two-dimensional table data, Java can also directly to Excel operation, in this blog will introduce two ways to operate Excel, respectively:JXL and poi.
For the difference between the two online there are tests as follows:
Test results
Type data amount (line) execution time (ms) execution time (ms) time (MS) Average time (ms)
POI 1000 579 562 532 558
JXL 1000 500 469 484 484
POI 5000 984 984 969 979
JXL 5000 922 860 890 891
POI 10000 1609 1594 1641 1615
JXL 10000 1437 1453 1406 1432
POI 30000 3782 3765 3828 3792
JXL 30000 3922 3906 3922 3917
POI 50000 5953 6484 5859 6099
JXL 50000 6765 7421 6984 7057
JXL faster than poi at small data volumes , and POI is faster than JXL in large data volumes . But the gap is not obvious.
(a) JXL write Excel
ImportJava.io.File;Importjava.io.IOException;ImportJXL. Workbook;ImportJxl.write.Label;ImportJxl.write.WritableSheet;ImportJxl.write.WritableWorkbook;Importjxl.write.WriteException;/*** JXL Write Excel * *@authorJianggujin **/ Public classjxlwritedemo{ Public Static voidMain (string[] args)throwsIOException, writeexception {File xlsfile=NewFile ("Jxl.xls"); //Create a workbookWritableworkbook Workbook =Workbook.createworkbook (Xlsfile); //Create a worksheetWritablesheet sheet = workbook.createsheet ("Sheet1", 0); for(introw = 0; Row < 10; row++) { for(intcol = 0; Col < 10; col++) { //add data to a worksheetSheet.addcell (NewLabel (col, row, "data" + row +col)); }} workbook.write (); Workbook.close (); }}Read Excel
ImportJava.io.File;Importjava.io.IOException;ImportJXL. Sheet;ImportJXL. Workbook;Importjxl.read.biff.BiffException;/*** JXL Read Excel * *@authorJianggujin **/ Public classjxlreaddemo{ Public Static voidMain (string[] args)throwsbiffexception, IOException {File xlsfile=NewFile ("Jxl.xls"); //Get Workbook ObjectsWorkbook Workbook =Workbook.getworkbook (Xlsfile); //Get all WorksheetsSheet[] Sheets =workbook.getsheets (); //Traversing Worksheets if(Sheets! =NULL) { for(Sheet sheet:sheets) {//get the number of rows introws =sheet.getrows (); //get number of columns intcols =Sheet.getcolumns (); //reading Data for(introw = 0; Row < rows; row++) { for(intcol = 0; Col < cols; col++) {System.out.printf ("%10s", Sheet.getcell (col, Row). getcontents ()); } System.out.println (); }}} workbook.close (); }}
(ii) POI write Excel
ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;/*** POI Write Excel * *@authorJianggujin **/ Public classpoiwritedemo{ Public Static voidMain (string[] args)throwsIOException {//Create a workbookHssfworkbook Workbook =NewHssfworkbook (); //Create a worksheetHssfsheet sheet = workbook.createsheet ("Sheet1"); for(introw = 0; Row < 10; row++) {hssfrow rows=sheet.createrow (row); for(intcol = 0; Col < 10; col++) { //add data to a worksheetRows.createcell (col). Setcellvalue ("Data" + row +col); }} File Xlsfile=NewFile ("Poi.xls"); FileOutputStream Xlsstream=NewFileOutputStream (Xlsfile); Workbook.write (Xlsstream); }}Read Excel
ImportJava.io.File;Importjava.io.IOException;Importorg.apache.poi.openxml4j.exceptions.InvalidFormatException;ImportOrg.apache.poi.ss.usermodel.Row;ImportOrg.apache.poi.ss.usermodel.Sheet;ImportOrg.apache.poi.ss.usermodel.Workbook;Importorg.apache.poi.ss.usermodel.WorkbookFactory;/*** POI Write Excel * *@authorJianggujin **/ Public classpoireaddemo{ Public Static voidMain (string[] args)throwsIOException, invalidformatexception {File xlsfile=NewFile ("Poi.xls"); //Get workbooksWorkbook Workbook =workbookfactory.create (Xlsfile); //get the number of worksheets intSheetcount =workbook.getnumberofsheets (); //Traversing Worksheets for(inti = 0; i < Sheetcount; i++) {Sheet Sheet=Workbook.getsheetat (i); //get the number of rows introws = Sheet.getlastrownum () + 1; //Gets the number of columns, first a row, the number of row columnsRow tmp = Sheet.getrow (0); if(TMP = =NULL) { Continue; } intcols =Tmp.getphysicalnumberofcells (); //reading Data for(introw = 0; Row < rows; row++) {Row R=sheet.getrow (row); for(intcol = 0; Col < cols; col++) {System.out.printf ("%10s", R.getcell (col). Getstringcellvalue ()); } System.out.println (); } } }}
Java Excel two ways to operate