Java operations Excel way to have two total Poi and Jxl, the following code is a simple realization:
Then
import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.springframework.stereotype.Component; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @Component public class ExcelByPoi { public static void writeExcelByPoi(String path) { try { // 创建工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作表 HSSFSheet sheet = workbook.createSheet("sheet1"); for (int row = 0; row < 10; row++) { HSSFRow rows = sheet.createRow(row); for (int col = 0; col < 10; col++) { if(Row == 0 ) { // add header rows.createCell (0) .setCellValue ( "name" ); rows.createCell ( . 1) .setCellValue ( "Class" ); rows.createCell ( 2) .setCellValue ( " gender " ); rows.createCell ( 3) .setCellValue (" Age " ); rows.createCell ( 4) .setCellValue (" birthday " ); rows.createCell ( 5) .setCellValue (" old " ); rows.createCell ( 6) .setCellValue ( "school" ); rows.createCell(7) .setCellValue ( "teacher" ); rows.createCell ( . 8) .setCellValue ( "Dad" ); rows.createCell ( . 9) .setCellValue ( "Mom" ); } the else { // add data to the worksheet rows .createCell (COL) .setCellValue ( "Data" Row + + COL); } } } File xlsFile = new new File (path); a FileOutputStream xlsStream = new new a FileOutputStream (xlsFile); workbook.write (xlsStream); } The catch (IOException E) { e.printStackTrace (); } } public static void readExcelByPoi (String path) { the try { File xlsFile = new new File (path); // get the workbook the Workbook Workbook = WorkbookFactory.create (xlsFile); // get the number of sheets int SheetCount = workbook.getNumberOfSheets (); // traverse the worksheet for ( int I = 0; I <SheetCount; I ++ ) { sheet sheet = workbook.getSheetAt (I); // get the row int rows sheet.getLastRowNum = () +. 1 ; // get the number of columns, the first line is obtained, resulting in changing the number of ranks Row sheet.getRow tmp = (0 ); IF (tmp == null ) { Continue ; } int cols = tmp.getPhysicalNumberOfCells (); // read the data for ( int Row = 0; Row <rows; Row ++ ) { Row R & lt = sheet.getRow (Row); for (int col = 0; col < cols; col++) { System.out.printf("%10s", r.getCell(col).getStringCellValue()); } System.out.println(); } } } catch (IOException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } } }
Jxl
<dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.10</version> </dependency>
import java.io.File; import java.io.IOException; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.Sheet; import jxl.read.biff.BiffException; import org.springframework.stereotype.Component; @Component public class ExcelByJxl { public static void writeExcelByJxl(String path) { try { File xlsFile = newFile (path); // create a workbook WritableWorkbook Workbook = Workbook.createWorkbook (xlsFile); // create a worksheet WritableSheet Sheet = workbook.createSheet ( "sheet1", 0 ); for ( int Row = 0; Row < 10; Row ++ ) { for ( int COL = 0; COL <10; COL ++ ) { // add to the data in the worksheet sheet.addCell ( new new the Label (COL, Row, "data" Row + + COL)); } } // create a worksheet WritableSheet sheet1 = workbook.createSheet("sheet2", 0); for (int row = 0; row < 10; row++) { for (int col = 0; col < 10; col++) { if(row == 0) { //添加表头 sheet1.addCell(new Label(0, 0, "班级")); sheet1.addCell(new Label(1, 0, "姓名")); sheet1.addCell(new Label(2, 0, "年级")); sheet1.addCell (elsenew new the Label (3, 0, "Age" )); sheet1.addCell ( new new the Label (4, 0, "birthday" )); sheet1.addCell ( new new the Label (5, 0, "address" )); sheet1.addCell ( new new the Label (6, 0, "gender" )); sheet1.addCell ( new new the Label (7, 0, "Dad" )); sheet1.addCell ( new new the Label (8, 0, "mother" )); sheet1. addCell ( new new the Label (. 9, 0, "class" )); } { // 向工作表中添加数据 sheet1.addCell(new Label(col, row, "data" + row + col)); } } } workbook.write(); workbook.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } public static void readExcelByJxl(String path) { try { File xlsFile = New newFile (path); // obtain a workbook object Workbook Workbook = Workbook.getWorkbook (xlsFile); // get all the worksheets Sheet [] = sheets workbook.getSheets (); // traverse the worksheet IF (sheets =! Null ) { for (Sheet Sheet: sheets) { // get the row int rows = sheet.getRows (); // get the number of columns int cols = sheet.getColumns (); // read the data for ( int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { System.out.printf("%10s", sheet.getCell(col, row) .getContents()); } System.out.println(); } } } workbook.close(); } catch (IOException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace (); } } }