1. Java讀寫excel的兩種方式
(1)apache poi
(2)alibaba easyexcel
兩種方式寫入excel的區別;
poi會先把所有的數據讀入到內存中,然后寫入到excel中;easyexcel則是把數據一條一條寫入excel中。所以如果有大量數據時,比如有100w條數據寫入excel中,使用poi會先把數據讀入內存中,可能會造成內存溢出;而easyexcel則把數據一條一條寫入excel中。
2. 依賴引入
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>
3. 使用poi讀寫excel
excel有03和07兩個版本,所以使用poi讀寫excel也略有區別。
(1)03版本excel文件后綴名.xls,最多能寫入65535條數據;07版本excel文件后綴名.xlsx,理論上能寫更多條數據,但是使用普通對象寫入excel消耗時間更長,大量寫入可以使用SXSSFWorkbook;
(2)03和07版本excel創建對象不同,03版本創建HSSFWorkbook對象,07版本創建XSSFWorkbook對象,大量數據讀寫使用SXSSFWorkbook;
(3)部分方法不用;
寫入excel的幾個步驟:
a. 創建空的表格(工作簿);
b. 創建工作區;
c. 創建行;
d. 創建單元格;
e. 給單元格設置內容;(對單元格內容格式設置)
f. 把內存中的單元格生成到本地的磁盤上;
寫入03版本exel
public class ExcelWrite03 { public static void main( String[] args ) { //創建空的excel表格,HSSF普通excel Workbook wb = new HSSFWorkbook(); //創建工作區 Sheet sheet = wb.createSheet("員工數據"); //設置列寬(第一個參數表示:第二列;第二個參數表示:列寬為50,此處注意設置列寬要*256) sheet.setColumnWidth(2,50*256); //創建行 方法參數是行號,從0開始 Row row = sheet.createRow(1); //設置行高 row.setHeightInPoints(40f); //創建 2C 單元格,參數:列號,從0開始 Cell cell = row.createCell(2); Row row1 = sheet.createRow(0); Cell cell1 = row1.createCell(0); cell1.setCellValue("hello"); /** * 設置單元格樣式 */ //創建單元格樣式 CellStyle style = wb.createCellStyle(); //設置字體樣式,要先創建字體對象 Font font = wb.createFont(); //字體加粗 font.setBold(true); //設置字體大小 font.setFontHeightInPoints((short) 20); //設置字體類型 font.setFontName("楷體"); //給style添加字體樣式 style.setFont(font); //給單元格設置樣式 cell.setCellStyle(style); //給單元格設置內容 cell.setCellValue("hello world"); FileOutputStream fos = null; try { fos = new FileOutputStream(new File("D:\\JavaProject\\hello.xls")); wb.write(fos); fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if (wb != null){ try { wb.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
寫入07版本excel,與03版本創建對象不用,其他相同

public class ExcelWrite07 { public static void main(String[] args) { Workbook wb = new XSSFWorkbook(); //03和07創建對象不同 Sheet sheet = wb.createSheet("員工數據"); //設置列寬(第一個參數表示:第二列;第二個參數表示:列寬為50,此處注意設置列寬要*256) sheet.setColumnWidth(2,50*256); //創建行 方法參數是行號,從0開始 Row row = sheet.createRow(1); //設置行高 row.setHeightInPoints(40f); //創建 2C 單元格,參數:列號,從0開始 Cell cell = row.createCell(2); /** * 設置單元格樣式 */ //創建單元格樣式 CellStyle style = wb.createCellStyle(); //設置字體樣式,要先創建字體對象 Font font = wb.createFont(); //字體加粗 font.setBold(true); //設置字體大小 font.setFontHeightInPoints((short) 20); //設置字體類型 font.setFontName("楷體"); //給style添加字體樣式 style.setFont(font); //給單元格設置樣式 cell.setCellStyle(style); //給單元格設置內容 cell.setCellValue("hello world"); FileOutputStream fos = null; try { fos = new FileOutputStream(new File("D:\\JavaProject\\hello07.xlsx")); //后綴不同 wb.write(fos); fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if (wb != null){ try { wb.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
讀excel的基本步驟:
a. 創建工作簿;
b. 獲取sheet;
c. 獲取行;
d. 獲取單元格;
e. 獲取單元格中的數據
讀03版本excel
public class ExcelRead03 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream( new File("D:\\JavaProject\\read03.xls")); //1. 創建工作簿 Workbook wb = new HSSFWorkbook(fis); //2. 獲取sheet Sheet sheet = wb.getSheetAt(0); System.out.println(sheet); //3. 獲取行 Row row = sheet.getRow(0); //4. 獲取單元格 Cell cell = row.getCell(0); //5. 獲取單元格中的字符串內容 System.out.println(cell.getStringCellValue()); //獲取單元格中數字內容 System.out.println(sheet.getRow(1).getCell(1).getNumericCellValue()); fis.close(); } }
讀07版本excel,與03版本創建對象不同,其他相同

public class ExcelRead07 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream( new File("D:\\JavaProject\\read07.xlsx")); //1. 創建工作簿 Workbook wb = new XSSFWorkbook(fis); //2. 獲取sheet Sheet sheet = wb.getSheetAt(0); System.out.println(sheet); //3. 獲取行 Row row = sheet.getRow(0); //4. 獲取單元格 Cell cell = row.getCell(0); //5. 獲取單元格中的字符串內容 System.out.println(cell.getStringCellValue()); //獲取單元格中數字內容 System.out.println(sheet.getRow(1).getCell(1).getNumericCellValue()); fis.close(); } }
單元格不同類型數據讀取,在讀excel時,單元格中會存在不用的數據類型,日期型,字符串型,數字型等,需要做不同類型的獲取
public class ReadCellType07 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream( new File("D:\\JavaProject\\test.xlsx")); Workbook wb = new XSSFWorkbook(fis); Sheet sheet = wb.getSheetAt(0); //獲取標題行內容 Row rowTitle = sheet.getRow(0); if(rowTitle != null){ //獲取單元格的個數 int cellCount = rowTitle.getPhysicalNumberOfCells(); for (int cellNum = 0; cellNum < cellCount; cellNum ++){ Cell cell = rowTitle.getCell(cellNum); if (cell != null){ String cellValue = cell.getStringCellValue(); System.out.print(cellValue + " | "); } } System.out.println(); } //獲取表中的內容 int rowCount = sheet.getPhysicalNumberOfRows(); for(int rowNum = 1; rowNum < rowCount; rowNum++){ Row rowData = sheet.getRow(rowNum); if(rowData != null){ int cellCount = rowTitle.getPhysicalNumberOfCells(); for(int cellNum = 0; cellNum < cellCount; cellNum++){ System.out.print("[" + (rowNum + 1) + "-" + (cellNum + 1) + "]"); Cell cell = rowData.getCell(cellNum); //匹配單元格中數據的類型 if (cell != null) { CellType cellType = cell.getCellType(); String cellValue = ""; switch (cellType){ case STRING: System.out.print("【String】"); cellValue = cell.getStringCellValue(); break; case BOOLEAN: System.out.print("【Boolean】"); cellValue = String.valueOf(cell.getBooleanCellValue()); break; case BLANK: System.out.print("【Blank】"); break; case NUMERIC: System.out.print("【NUMERIC】"); if(DateUtil.isCellDateFormatted(cell)){ //日期 System.out.print("日期"); Date dateCellValue = cell.getDateCellValue(); cellValue = dateCellValue.toString(); }else { //不是日期,防止數字過長 System.out.print("【轉換為字符串輸出】"); cell.setCellType(CellType.STRING); cellValue = cell.toString(); } break; case ERROR: System.out.print("數據類型錯誤"); break; } System.out.println(cellValue); } } } } fis.close(); } }
讀取單元格中的公式,表格中A5是一個計算和的公式
public class CalcCellFormula03 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream( new File("D:\\JavaProject\\公式.xls")); Workbook wb = new HSSFWorkbook(fis); Sheet sheet = wb.getSheetAt(0); Row row = sheet.getRow(4); Cell cell = row.getCell(0); //獲取計算公式 FormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) wb); //輸出單元格內容 CellType cellType = cell.getCellType(); switch (cellType){ case FORMULA: //公式 String cellFormula = cell.getCellFormula(); System.out.println(cellFormula); //計算 CellValue evaluate = formulaEvaluator.evaluate(cell); String s = evaluate.formatAsString(); System.out.println(s); } } }
4. 使用easyexcel讀寫excel
https://www.yuque.com/easyexcel/doc/easyexcel