官网:https://yuque.com/easyexcel
狂神学习视频:https://www.bilibili.com/video/BV1Ua4y1x7BK?from=search&seid=282810424102829485&spm_id_from=333.337.0.0e

基本功能

HSSF Excel 2003 限制行数65535
XSSF Excel OOXML 2017 不限制行数
HWPF Word
HSLF PowerPoint
HDGF Visio

POI 先加载到内存 EasyExcel 会一行一行的写 时间换空间
工作簿: _Workbook _workbook = _new _XSSFWorkbook(); //07
工作表: _Sheet _sheet = workbook.createSheet(“豪”);
行: _Row _row = sheet.createRow(0);
列: _Cell _cell1 = row.createCell(0);//单元格

Excel03

  1. @Test
  2. public void testWrite03() throws Exception {
  3. //1.创建工作簿
  4. Workbook workbook = new HSSFWorkbook();
  5. //2.创建工作表
  6. Sheet sheet = workbook.createSheet("豪");
  7. //3.创建行
  8. Row row = sheet.createRow(0);
  9. //4.创建单元格
  10. Cell cell1 = row.createCell(0);
  11. cell1.setCellValue("今日新增观众");
  12. Cell cell2 = row.createCell(1);
  13. cell2.setCellValue(666);
  14. // 第二行
  15. Row row2 = sheet.createRow(1);
  16. Cell cell21 = row2.createCell(0);
  17. cell21.setCellValue("统计时间");
  18. Cell cell22 = row2.createCell(1);
  19. String s = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
  20. cell22.setCellValue(s);
  21. //生成一张表(io流) 03版本后缀是xls结尾
  22. FileOutputStream fileOutputStream = new FileOutputStream(PATH + "统计表03.xls");
  23. workbook.write(fileOutputStream);
  24. //关闭流
  25. fileOutputStream.close();
  26. System.out.println("豪 03生成完毕");
  27. }

Excel07

  1. @Test
  2. public void testWrite07() throws Exception {
  3. //1.创建工作簿
  4. Workbook workbook = new XSSFWorkbook();
  5. //2.创建工作表
  6. Sheet sheet = workbook.createSheet("豪");
  7. //3.创建行
  8. Row row = sheet.createRow(0);
  9. //4.创建单元格
  10. Cell cell1 = row.createCell(0);
  11. cell1.setCellValue("今日新增观众");
  12. Cell cell2 = row.createCell(1);
  13. cell2.setCellValue(666);
  14. // 第二行
  15. Row row2 = sheet.createRow(1);
  16. Cell cell21 = row2.createCell(0);
  17. cell21.setCellValue("统计时间");
  18. Cell cell22 = row2.createCell(1);
  19. String s = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
  20. cell22.setCellValue(s);
  21. //生成一张表(io流) 07版本后缀是xlsx结尾
  22. FileOutputStream fileOutputStream = new FileOutputStream(PATH + "统计表07.xlsx");
  23. workbook.write(fileOutputStream);
  24. //关闭流
  25. fileOutputStream.close();
  26. System.out.println("豪 07生成完毕");
  27. }

Excel03和Excel07区别 创建工作簿用的实体类不同 后缀不同
03 _Workbook _Workbook = _new _HSSFWorkbook(); (不能超过65536行)
07 _Workbook _Workbook = _new _XSSFWorkbook(); (慢 占用内存高)
_Workbook _workbook = _new _SXSSFWorkbook();(快 能超过65536行有可能占用内存过高 需清理临时文件((SXSSFWorkbook) workbook).dispose();)

读取不同类型

  1. @Test
  2. public void testCellType() throws Exception {
  3. FileInputStream fileInputStream = new FileInputStream(PATH + "统计表03.xls");
  4. Workbook workbook = new HSSFWorkbook(fileInputStream);
  5. Sheet sheet = workbook.getSheetAt(0);
  6. //获取标题内容
  7. Row row = sheet.getRow(0);
  8. if (row != null) {
  9. int cellCount = row.getPhysicalNumberOfCells();
  10. for (int i = 0; i < cellCount; i++) {
  11. Cell cell = row.getCell(i);
  12. if (cell != null) {
  13. int cellType = cell.getCellType();
  14. String cellValue = cell.getStringCellValue();
  15. System.out.print(cellValue + " | ");
  16. }
  17. }
  18. System.out.println();
  19. }
  20. //获取表中的内容
  21. int rowCount = sheet.getPhysicalNumberOfRows();
  22. for (int i = 0; i < rowCount; i++) {
  23. Row sheetRow = sheet.getRow(i);
  24. if (sheetRow != null) {
  25. int cellCount = sheetRow.getPhysicalNumberOfCells();
  26. for (int j = 0; j < cellCount; j++) {
  27. System.out.print("[" + (i + 1) + "-" + (j + 1) + "]");
  28. //匹配列的数据类型
  29. Cell cell = sheetRow.getCell(j);
  30. String s = cell.toString();
  31. if (cell != null) {
  32. int cellType = cell.getCellType();
  33. String cellValue = "";
  34. switch (cellType) {
  35. case HSSFCell.CELL_TYPE_STRING: //字符串
  36. System.out.print("[String]");
  37. cellValue = cell.getStringCellValue();
  38. break;
  39. case HSSFCell.CELL_TYPE_BOOLEAN: // 布尔
  40. System.out.print("[boolean]");
  41. cellValue = String.valueOf(cell.getBooleanCellValue());
  42. break;
  43. case HSSFCell.CELL_TYPE_BLANK: // 空
  44. System.out.print("[blank]");
  45. break;
  46. case HSSFCell.CELL_TYPE_NUMERIC: // 数字
  47. System.out.print("[numeric]");
  48. if (HSSFDateUtil.isCellDateFormatted(cell)) { //日期
  49. System.out.print("日期");
  50. Date date = cell.getDateCellValue();
  51. cellValue = new DateTime(date).toString("yyyy-MM-dd");
  52. } else {
  53. //如果不是日期
  54. System.out.print("[转换为字符串输出]");
  55. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  56. cellValue = cell.toString();
  57. }
  58. break;
  59. case HSSFCell.CELL_TYPE_ERROR: // 数据类型错误
  60. System.out.print("[数据类型错误]");
  61. break;
  62. default:
  63. }
  64. System.out.println(cellValue);
  65. }
  66. }
  67. }
  68. }
  69. fileInputStream.close();
  70. }

注意:类型转换问题

计算公式

  1. @Test
  2. public void testFormula() throws Exception{
  3. FileInputStream fileInputStream = new FileInputStream(PATH + "公式.xls");
  4. Workbook workbook = new HSSFWorkbook(fileInputStream);
  5. Sheet sheet = workbook.getSheetAt(0);
  6. Row row = sheet.getRow(4);
  7. Cell cell = row.getCell(0);
  8. //拿到计算公式
  9. FormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
  10. //输出单元格内容
  11. int cellType = cell.getCellType();
  12. switch (cellType){
  13. case HSSFCell.CELL_TYPE_FORMULA:
  14. String formula = cell.getCellFormula();
  15. System.out.println(formula);
  16. //计算
  17. CellValue evaluate = formulaEvaluator.evaluate(cell);
  18. String s = evaluate.formatAsString();
  19. System.out.println(s);
  20. break;
  21. default:
  22. }
  23. }

数字转换性别

  1. public class SexConverter implements Converter<Integer> {
  2. @Override
  3. public Class supportJavaTypeKey() {
  4. return Integer.class;
  5. }
  6. @Override
  7. public CellDataTypeEnum supportExcelTypeKey() {
  8. return CellDataTypeEnum.STRING;
  9. }
  10. @Override
  11. public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
  12. return "男".equals(cellData.getStringValue()) ? 1 : 0;
  13. }
  14. @Override
  15. public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
  16. return new WriteCellData<>(value.equals(1) ? "男" : "女");
  17. }
  18. }

让converter生效

  1. @ExcelProperty(value = "订单状态",index = 11,converter = OrderRefundStatusConverter.class)
  2. private Integer refundStatus;