Apache POI 中文使用指南

小灰灰 2022-10-30 11:22 438阅读 0赞

Apache POI 中文使用手册

    1. Apache POI 中文使用手册
    • 1.1 Apache POI 项目介绍
    • 1.2 处理组件
      • 1.2.1 Excel 文件处理组件
      • 1.2.2 Word 文件处理组件
      • 1.2.3 PPT 文件处理组件
      • 1.2.4 文档属性组件
      • 1.2.5 Visio 文件处理组件
      • 1.2.6 Microsoft Publisher 98(-2007)文件处理组件
      • 1.2.7 OutLook 文件处理组件
      • 1.2.8 OutLook 附件文件处理组件
    • 1.3 Apache POI 下载
      • 1.3.1 直接下载
      • 1.3.2 Maven依赖下载
    • 1.4 使用Apache POI
      • 1.4.1 使用Apache POI 读写Excel 文件
        • 1.4.1.1 如何创建一个WorkBook?
        • 1.4.1.2 如何创建一个Sheet?
        • 1.4.1.3 如何创建一个单元格?
        • 1.4.1.4 如何创建一个日期类型的单元格?
        • 1.4.1.5 创建多种格式的单元格
        • 1.4.1.6 Files 和InputStream
        • 1.4.1.7 展示各种对齐方式
        • 1.4.1.8 设置边框
        • 1.4.1.9 遍历行和单元格
        • 1.4.1.10 遍历单元格,控制丢失/空白的单元格
        • 1.4.1.11 获取单元格内容
        • 1.4.1.11 文字提取
        • 1.4.1.12 填充和颜色
        • 1.4.1.13 合并单元格
        • 1.4.1.14 设置字体
        • 1.4.1.15 自定义颜色
        • 1.4.1.16 读和写WorksBooks
        • 1.4.1.17 在单元格中使用换行符
        • 1.4.1.18 数据格式化
        • 1.4.1.19 将工作表调整为一页
        • 1.4.1.20 设置打印区域
        • 1.4.1.21 设置页脚页数
      • 1.4.2 Apache POI 提取Word中的文件
        • 1.4.2.1 读取Excel 数据并写入到Word示例
        • 1.4.2.2 读取Excel 数据并从Word中提取图片示例

1. Apache POI 中文使用手册

1.1 Apache POI 项目介绍

  • Apache POI项目的任务是创建和维护Java API,以基于Office Open XML标准(OOXML)和Microsoft的OLE 2复合文档格式(OLE2)处理各种文件格式。
  • 简而言之,您可以使用Java读写MS Excel文件。
  • 此外,您可以使用Java读写MS Word和MS PowerPoint文件。
  • Apache POI是您的Java Excel解决方案(适用于Excel 97-2008)
  • Apache POI项目是用于基于Microsoft的OLE 2复合文档格式开发文件格式的纯Java端口的主项目
  • Apache POI还是用于基于Office Open XML(ooxml)开发文件格式的纯Java端口的主项目

简言之,Apache POI 是Java 领域中可以操作World,Excel,PPT文件的类库,可以用于生成报表,数据处理等.

值得注意的是,Apache POI 从4.0.1版本开始,需要JDK 8 或更高版本支持。

1.2 处理组件

1.2.1 Excel 文件处理组件


















POIFS HSSF XSSF SXSSF
OIFS是POI中最古老,最稳定的部分。OLE 2复合文档格式到纯Java的移植。 读写.xls文件 读写.xlsx 文件 读写*.xlsx 大文件

1.2.2 Word 文件处理组件














HWPF XWPF
处理.doc文件 处理.docx 文件

1.2.3 PPT 文件处理组件














HSLF XSLF
处理.ppt文件 处理.pptx 文件

1.2.4 文档属性组件

  • HPSF是OLE 2属性集格式到纯Java的移植。
  • 属性集通常用于存储文档的属性(标题,作者,最后修改日期等),但是它们也可以用于特定于应用程序的目的。
  • HPSF支持读取和写入属性。

1.2.5 Visio 文件处理组件














HDGF XDGF
处理.vsd 文件 处理.vsdx文件

1.2.6 Microsoft Publisher 98(-2007)文件处理组件

  • HPBF是我们将Microsoft Publisher 98(-2007)文件格式移植到纯Java的端口。
  • 目前,它仅支持低水平读取大约一半的文件部分,并支持简单的文本提取

1.2.7 OutLook 文件处理组件

  • SMF是Microsoft Outlook消息文件格式到纯Java的移植。目前,它仅包含MSG文件的某些文本内容以及一些附件。进一步的支持和文档进展缓慢
  • Microsoft最近在其OSP中添加了Outlook文件格式

1.2.8 OutLook 附件文件处理组件

  • HMEF是Microsoft TNEF(传输中性编码格式)文件格式到纯Java的移植。Outlook有时会使用TNEF对消息进行编码,通常会以winmail.dat的形式出现。HMEF当前仅支持较低级别的阅读,但我们希望添加文本和附件提取。
  • 处理winmail.dat 格式文件

1.3 Apache POI 下载

依赖引用关系图如下所示:
在这里插入图片描述

1.3.1 直接下载

点击下载Apache POI
在这里插入图片描述

  • 二进制版本发布仓库: https://archive.apache.org/dist/poi/release/bin/
  • 源码版本发布仓库:https://archive.apache.org/dist/poi/release/src/

1.3.2 Maven依赖下载

  1. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
  2. <dependency>
  3. <groupId>org.apache.poi</groupId>
  4. <artifactId>poi</artifactId>
  5. <version>5.0.0</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
  8. <dependency>
  9. <groupId>org.apache.poi</groupId>
  10. <artifactId>poi-ooxml</artifactId>
  11. <version>5.0.0</version>
  12. </dependency>

点击查看最新版

1.4 使用Apache POI

Apache POI 虽然支持很多种文档格式的读写,但是我们最常用的还是对Excel 文件的读写。

1.4.1 使用Apache POI 读写Excel 文件

正如前面所说,Apache POI 针对读写Excel 文件提供了三个组件:





















HSSF XSSF SXSSF
HSSF是POI项目对Excel ‘97(-2007)文件格式的纯Java实现 XSSF是POI项目对Excel 2007 OOXML(.xlsx)文件格式的纯Java实现。 SXSSF是XSSF的API兼容流扩展,可用于必须生成非常大的电子表格且堆空间有限的情况
处理.xls 文件 处理.xlsx 文件 处理超大的*xlsx 文件
  • 生成电子表格的另一种方法是通过Cocoon序列化器(但是您仍将间接使用HSSF)。使用Cocoon,您可以通过简单地应用样式表并指定序列化程序来序列化任何XML数据源(例如,可能是在SQL中输出的ESQL页面)。
  • 3.8-beta3开始,POI提供了基于XSSF的低内存占用的SXSSF API。
  • SXSSF是XSSF的API兼容流扩展,可用于必须生成非常大的电子表格且堆空间有限的情况。SXSSF通过限制对滑动窗口内的行的访问来实现其低内存占用,而XSSF允许对文档中的所有行进行访问。不再存在于窗口中的较旧的行由于被写入磁盘而变得不可访问.
  • 在自动刷新模式下,可以指定访问窗口的大小,以在内存中保留一定数量的行。当达到该值时,创建额外的一行会导致索引最低的行从访问窗口中删除并写入磁盘。或者,可以将窗口大小设置为动态增长。可以根据需要通过显式调用flushRows(int keepRows)定期对其进行修剪。
  • 由于实现的流性质,与XSSF相比存在以下限制:

    • 在某个时间点只能访问有限数量的行。
    • 不支持Sheet.clone()
    • 不支持公式评估
  • 电子表格API功能摘要
    在这里插入图片描述

1.4.1.1 如何创建一个WorkBook?

创建一个*.xls 文件

  1. Workbook wb = new HSSFWorkbook();
  2. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  3. wb.write(fileOut);
  4. }

创建一个*.xlsx 文件

  1. Workbook wb = new XSSFWorkbook();
  2. try (OutputStream fileOut = new FileOutputStream("workbook.xlsx")) {
  3. wb.write(fileOut);
  4. }

1.4.1.2 如何创建一个Sheet?

  1. Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
  2. Sheet sheet1 = wb.createSheet("new sheet");
  3. Sheet sheet2 = wb.createSheet("second sheet");
  4. // Note that sheet name is Excel must not exceed 31 characters
  5. // and must not contain any of the any of the following characters:
  6. // 0x0000
  7. // 0x0003
  8. // colon (:)
  9. // backslash (\)
  10. // asterisk (*)
  11. // question mark (?)
  12. // forward slash (/)
  13. // opening square bracket ([)
  14. // closing square bracket (])
  15. // You can use org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)}
  16. // for a safe way to create valid names, this utility replaces invalid characters with a space (' ')
  17. String safeName = WorkbookUtil.createSafeSheetName("[O'Brien's sales*?]"); // returns " O'Brien's sales "
  18. Sheet sheet3 = wb.createSheet(safeName);
  19. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  20. wb.write(fileOut);
  21. }

1.4.1.3 如何创建一个单元格?

  1. Workbook wb = new HSSFWorkbook();
  2. //Workbook wb = new XSSFWorkbook();
  3. CreationHelper createHelper = wb.getCreationHelper();
  4. Sheet sheet = wb.createSheet("new sheet");
  5. // Create a row and put some cells in it. Rows are 0 based.
  6. Row row = sheet.createRow(0);
  7. // Create a cell and put a value in it.
  8. Cell cell = row.createCell(0);
  9. cell.setCellValue(1);
  10. // Or do it on one line.
  11. row.createCell(1).setCellValue(1.2);
  12. row.createCell(2).setCellValue(
  13. createHelper.createRichTextString("This is a string"));
  14. row.createCell(3).setCellValue(true);
  15. // Write the output to a file
  16. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  17. wb.write(fileOut);
  18. }

1.4.1.4 如何创建一个日期类型的单元格?

  1. Workbook wb = new HSSFWorkbook();
  2. //Workbook wb = new XSSFWorkbook();
  3. CreationHelper createHelper = wb.getCreationHelper();
  4. Sheet sheet = wb.createSheet("new sheet");
  5. // Create a row and put some cells in it. Rows are 0 based.
  6. Row row = sheet.createRow(0);
  7. // Create a cell and put a date value in it. The first cell is not styled
  8. // as a date.
  9. Cell cell = row.createCell(0);
  10. cell.setCellValue(new Date());
  11. // we style the second cell as a date (and time). It is important to
  12. // create a new cell style from the workbook otherwise you can end up
  13. // modifying the built in style and effecting not only this cell but other cells.
  14. CellStyle cellStyle = wb.createCellStyle();
  15. cellStyle.setDataFormat(
  16. createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
  17. cell = row.createCell(1);
  18. cell.setCellValue(new Date());
  19. cell.setCellStyle(cellStyle);
  20. //you can also set date as java.util.Calendar
  21. cell = row.createCell(2);
  22. cell.setCellValue(Calendar.getInstance());
  23. cell.setCellStyle(cellStyle);
  24. // Write the output to a file
  25. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  26. wb.write(fileOut);
  27. }

1.4.1.5 创建多种格式的单元格

  1. Workbook wb = new HSSFWorkbook();
  2. Sheet sheet = wb.createSheet("new sheet");
  3. Row row = sheet.createRow(2);
  4. row.createCell(0).setCellValue(1.1);
  5. row.createCell(1).setCellValue(new Date());
  6. row.createCell(2).setCellValue(Calendar.getInstance());
  7. row.createCell(3).setCellValue("a string");
  8. row.createCell(4).setCellValue(true);
  9. row.createCell(5).setCellType(CellType.ERROR);
  10. // Write the output to a file
  11. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  12. wb.write(fileOut);
  13. }

1.4.1.6 Files 和InputStream

当打开 WorkBook(.xls HSSFWorkbook或.xlsx XSSFWorkbook)时,可以从File 或InputStream加载工作簿。

使用File对象可以减少内存消耗,而InputStream需要更多内存,因为它必须缓冲整个文件。

如果使用WorkbookFactory,则使用其中一个非常容易:

  1. // Use a file
  2. Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));
  3. // Use an InputStream, needs more memory
  4. Workbook wb = WorkbookFactory.create(new FileInputStream("MyExcel.xlsx"));

如果直接使用HSSFWorkbook或XSSFWorkbook,通常应遍历POIFSFileSystem或 OPCPackage,以完全控制生命周期(包括完成后关闭文件):

  1. // HSSFWorkbook, File
  2. POIFSFileSystem fs = new POIFSFileSystem(new File("file.xls"));
  3. HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true);
  4. ....
  5. fs.close();
  6. // HSSFWorkbook, InputStream, needs more memory
  7. POIFSFileSystem fs = new POIFSFileSystem(myInputStream);
  8. HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true);
  9. // XSSFWorkbook, File
  10. OPCPackage pkg = OPCPackage.open(new File("file.xlsx"));
  11. XSSFWorkbook wb = new XSSFWorkbook(pkg);
  12. ....
  13. pkg.close();
  14. // XSSFWorkbook, InputStream, needs more memory
  15. OPCPackage pkg = OPCPackage.open(myInputStream);
  16. XSSFWorkbook wb = new XSSFWorkbook(pkg);
  17. ....
  18. pkg.close();

1.4.1.7 展示各种对齐方式

  1. public static void main(String[] args) throws Exception {
  2. Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
  3. Sheet sheet = wb.createSheet();
  4. Row row = sheet.createRow(2);
  5. row.setHeightInPoints(30);
  6. createCell(wb, row, 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
  7. createCell(wb, row, 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM);
  8. createCell(wb, row, 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER);
  9. createCell(wb, row, 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER);
  10. createCell(wb, row, 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY);
  11. createCell(wb, row, 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP);
  12. createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);
  13. // Write the output to a file
  14. try (OutputStream fileOut = new FileOutputStream("xssf-align.xlsx")) {
  15. wb.write(fileOut);
  16. }
  17. wb.close();
  18. }
  19. /** * Creates a cell and aligns it a certain way. * * @param wb the workbook * @param row the row to create the cell in * @param column the column number to create the cell in * @param halign the horizontal alignment for the cell. * @param valign the vertical alignment for the cell. */
  20. private static void createCell(Workbook wb, Row row, int column, HorizontalAlignment halign, VerticalAlignment valign) {
  21. Cell cell = row.createCell(column);
  22. cell.setCellValue("Align It");
  23. CellStyle cellStyle = wb.createCellStyle();
  24. cellStyle.setAlignment(halign);
  25. cellStyle.setVerticalAlignment(valign);
  26. cell.setCellStyle(cellStyle);
  27. }

1.4.1.8 设置边框

  1. Workbook wb = new HSSFWorkbook();
  2. Sheet sheet = wb.createSheet("new sheet");
  3. // Create a row and put some cells in it. Rows are 0 based.
  4. Row row = sheet.createRow(1);
  5. // Create a cell and put a value in it.
  6. Cell cell = row.createCell(1);
  7. cell.setCellValue(4);
  8. // Style the cell with borders all around.
  9. CellStyle style = wb.createCellStyle();
  10. style.setBorderBottom(BorderStyle.THIN);
  11. style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  12. style.setBorderLeft(BorderStyle.THIN);
  13. style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
  14. style.setBorderRight(BorderStyle.THIN);
  15. style.setRightBorderColor(IndexedColors.BLUE.getIndex());
  16. style.setBorderTop(BorderStyle.MEDIUM_DASHED);
  17. style.setTopBorderColor(IndexedColors.BLACK.getIndex());
  18. cell.setCellStyle(style);
  19. // Write the output to a file
  20. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  21. wb.write(fileOut);
  22. }
  23. wb.close()

1.4.1.9 遍历行和单元格

有时,可能只想遍历工作簿中的所有工作表中的所有行或行中的所有单元格。这可以通过简单的for循环来实现。

通过调用workbook.sheetIterator(), sheet.rowIterator()和row.cellIterator() 或隐式使用for-each循环,可以使用这些迭代器。请注意,rowIterator和cellIterator遍历已创建的行或单元格,跳过空的行和单元格。

  1. for (Sheet sheet : wb ) {
  2. for (Row row : sheet) {
  3. for (Cell cell : row) {
  4. // Do something here
  5. }
  6. }
  7. }

1.4.1.10 遍历单元格,控制丢失/空白的单元格

在某些情况下,进行迭代时,您需要完全控制如何处理丢失或空白的行和单元格,并且需要确保访问每个单元格,而不仅仅是访问文件中定义的那些单元格。(CellIterator将仅返回文件中定义的单元格,这在很大程度上是具有值或样式的单元格,但取决于Excel)。

在这种情况下,应获取一行的第一列和最后一列信息,然后调用getCell(int,MissingCellPolicy) 来获取单元格。使用 MissingCellPolicy 控制空白或空单元格的处理方式。

  1. // Decide which rows to process
  2. int rowStart = Math.min(15, sheet.getFirstRowNum());
  3. int rowEnd = Math.max(1400, sheet.getLastRowNum());
  4. for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
  5. Row r = sheet.getRow(rowNum);
  6. if (r == null) {
  7. // This whole row is empty
  8. // Handle it as needed
  9. continue;
  10. }
  11. int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);
  12. for (int cn = 0; cn < lastColumn; cn++) {
  13. Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
  14. if (c == null) {
  15. // The spreadsheet is empty in this cell
  16. } else {
  17. // Do something useful with the cell's contents
  18. }
  19. }
  20. }

1.4.1.11 获取单元格内容

要获取单元格的内容,您首先需要知道它是哪种单元格(例如,将字符串单元格作为其数字内容将获得NumberFormatException)。因此,您将需要打开单元格的类型,然后为该单元格调用适当的getter。

在下面的代码中,我们遍历一张纸中的每个单元格,打印出该单元格的引用(例如A3),然后打印出该单元格的内容。

  1. // import org.apache.poi.ss.usermodel.*;
  2. DataFormatter formatter = new DataFormatter();
  3. Sheet sheet1 = wb.getSheetAt(0);
  4. for (Row row : sheet1) {
  5. for (Cell cell : row) {
  6. CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
  7. System.out.print(cellRef.formatAsString());
  8. System.out.print(" - ");
  9. // get the text that appears in the cell by getting the cell value and applying any data formats (Date, 0.00, 1.23e9, $1.23, etc)
  10. String text = formatter.formatCellValue(cell);
  11. System.out.println(text);
  12. // Alternatively, get the value and format it yourself
  13. switch (cell.getCellType()) {
  14. case CellType.STRING:
  15. System.out.println(cell.getRichStringCellValue().getString());
  16. break;
  17. case CellType.NUMERIC:
  18. if (DateUtil.isCellDateFormatted(cell)) {
  19. System.out.println(cell.getDateCellValue());
  20. } else {
  21. System.out.println(cell.getNumericCellValue());
  22. }
  23. break;
  24. case CellType.BOOLEAN:
  25. System.out.println(cell.getBooleanCellValue());
  26. break;
  27. case CellType.FORMULA:
  28. System.out.println(cell.getCellFormula());
  29. break;
  30. case CellType.BLANK:
  31. System.out.println();
  32. break;
  33. default:
  34. System.out.println();
  35. }
  36. }
  37. }

1.4.1.11 文字提取

对于大多数文本提取要求,标准ExcelExtractor类应提供您所需要的全部。

  1. try (InputStream inp = new FileInputStream("workbook.xls")) {
  2. HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
  3. ExcelExtractor extractor = new ExcelExtractor(wb);
  4. extractor.setFormulasNotResults(true);
  5. extractor.setIncludeSheetNames(false);
  6. String text = extractor.getText();
  7. wb.close();
  8. }

对于非常精美的文本提取,XLS到CSV等,请查看 /src/examples/src/org/apache/poi/examples/hssf/eventusermodel/XLS2CSVmra.java

1.4.1.12 填充和颜色

  1. Workbook wb = new XSSFWorkbook();
  2. Sheet sheet = wb.createSheet("new sheet");
  3. // Create a row and put some cells in it. Rows are 0 based.
  4. Row row = sheet.createRow(1);
  5. // Aqua background
  6. CellStyle style = wb.createCellStyle();
  7. style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
  8. style.setFillPattern(FillPatternType.BIG_SPOTS);
  9. Cell cell = row.createCell(1);
  10. cell.setCellValue("X");
  11. cell.setCellStyle(style);
  12. // Orange "foreground", foreground being the fill foreground not the font color.
  13. style = wb.createCellStyle();
  14. style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
  15. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  16. cell = row.createCell(2);
  17. cell.setCellValue("X");
  18. cell.setCellStyle(style);
  19. // Write the output to a file
  20. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  21. wb.write(fileOut);
  22. }
  23. wb.close();

1.4.1.13 合并单元格

  1. Workbook wb = new HSSFWorkbook();
  2. Sheet sheet = wb.createSheet("new sheet");
  3. Row row = sheet.createRow(1);
  4. Cell cell = row.createCell(1);
  5. cell.setCellValue("This is a test of merging");
  6. sheet.addMergedRegion(new CellRangeAddress(
  7. 1, //first row (0-based)
  8. 1, //last row (0-based)
  9. 1, //first column (0-based)
  10. 2 //last column (0-based)
  11. ));
  12. // Write the output to a file
  13. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  14. wb.write(fileOut);
  15. }
  16. wb.close();

1.4.1.14 设置字体

  1. Workbook wb = new HSSFWorkbook();
  2. Sheet sheet = wb.createSheet("new sheet");
  3. // Create a row and put some cells in it. Rows are 0 based.
  4. Row row = sheet.createRow(1);
  5. // Create a new font and alter it.
  6. Font font = wb.createFont();
  7. font.setFontHeightInPoints((short)24);
  8. font.setFontName("Courier New");
  9. font.setItalic(true);
  10. font.setStrikeout(true);
  11. // Fonts are set into a style so create a new one to use.
  12. CellStyle style = wb.createCellStyle();
  13. style.setFont(font);
  14. // Create a cell and put a value in it.
  15. Cell cell = row.createCell(1);
  16. cell.setCellValue("This is a test of fonts");
  17. cell.setCellStyle(style);
  18. // Write the output to a file
  19. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  20. wb.write(fileOut);
  21. }
  22. wb.close();

请注意,工作簿中唯一字体的最大数量限制为32767。您应该在应用程序中重新使用字体,而不是为每个单元格创建字体。

例子:

错误的写法

  1. for (int i = 0; i < 10000; i++) {
  2. Row row = sheet.createRow(i);
  3. Cell cell = row.createCell(0);
  4. CellStyle style = workbook.createCellStyle();
  5. Font font = workbook.createFont();
  6. font.setBoldweight(Font.BOLDWEIGHT_BOLD);
  7. style.setFont(font);
  8. cell.setCellStyle(style);
  9. }

正确的写法

  1. CellStyle style = workbook.createCellStyle();
  2. Font font = workbook.createFont();
  3. font.setBoldweight(Font.BOLDWEIGHT_BOLD);
  4. style.setFont(font);
  5. for (int i = 0; i < 10000; i++) {
  6. Row row = sheet.createRow(i);
  7. Cell cell = row.createCell(0);
  8. cell.setCellStyle(style);
  9. }

1.4.1.15 自定义颜色

HSSF(*.xls文件)

  1. HSSFWorkbook wb = new HSSFWorkbook();
  2. HSSFSheet sheet = wb.createSheet();
  3. HSSFRow row = sheet.createRow(0);
  4. HSSFCell cell = row.createCell(0);
  5. cell.setCellValue("Default Palette");
  6. //apply some colors from the standard palette,
  7. // as in the previous examples.
  8. //we'll use red text on a lime background
  9. HSSFCellStyle style = wb.createCellStyle();
  10. style.setFillForegroundColor(HSSFColor.LIME.index);
  11. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  12. HSSFFont font = wb.createFont();
  13. font.setColor(HSSFColor.RED.index);
  14. style.setFont(font);
  15. cell.setCellStyle(style);
  16. //save with the default palette
  17. try (OutputStream out = new FileOutputStream("default_palette.xls")) {
  18. wb.write(out);
  19. }
  20. //now, let's replace RED and LIME in the palette
  21. // with a more attractive combination
  22. // (lovingly borrowed from freebsd.org)
  23. cell.setCellValue("Modified Palette");
  24. //creating a custom palette for the workbook
  25. HSSFPalette palette = wb.getCustomPalette();
  26. //replacing the standard red with freebsd.org red
  27. palette.setColorAtIndex(HSSFColor.RED.index,
  28. (byte) 153, //RGB red (0-255)
  29. (byte) 0, //RGB green
  30. (byte) 0 //RGB blue
  31. );
  32. //replacing lime with freebsd.org gold
  33. palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
  34. //save with the modified palette
  35. // note that wherever we have previously used RED or LIME, the
  36. // new colors magically appear
  37. try (out = new FileOutputStream("modified_palette.xls")) {
  38. wb.write(out);
  39. }

XSSF(*.xlsx 文件)

  1. XSSFWorkbook wb = new XSSFWorkbook();
  2. XSSFSheet sheet = wb.createSheet();
  3. XSSFRow row = sheet.createRow(0);
  4. XSSFCell cell = row.createCell( 0);
  5. cell.setCellValue("custom XSSF colors");
  6. XSSFCellStyle style1 = wb.createCellStyle();
  7. style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128), new DefaultIndexedColorMap()));
  8. style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);

1.4.1.16 读和写WorksBooks

  1. try (InputStream inp = new FileInputStream("workbook.xls")) {
  2. //InputStream inp = new FileInputStream("workbook.xlsx");
  3. Workbook wb = WorkbookFactory.create(inp);
  4. Sheet sheet = wb.getSheetAt(0);
  5. Row row = sheet.getRow(2);
  6. Cell cell = row.getCell(3);
  7. if (cell == null)
  8. cell = row.createCell(3);
  9. cell.setCellType(CellType.STRING);
  10. cell.setCellValue("a test");
  11. // Write the output to a file
  12. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  13. wb.write(fileOut);
  14. }
  15. }

1.4.1.17 在单元格中使用换行符

  1. Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
  2. Sheet sheet = wb.createSheet();
  3. Row row = sheet.createRow(2);
  4. Cell cell = row.createCell(2);
  5. cell.setCellValue("Use \n with word wrap on to create a new line");
  6. //to enable newlines you need set a cell styles with wrap=true
  7. CellStyle cs = wb.createCellStyle();
  8. cs.setWrapText(true);
  9. cell.setCellStyle(cs);
  10. //increase row height to accommodate two lines of text
  11. row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
  12. //adjust column width to fit the content
  13. sheet.autoSizeColumn(2);
  14. try (OutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx")) {
  15. wb.write(fileOut);
  16. }
  17. wb.close();

1.4.1.18 数据格式化

  1. Workbook wb = new HSSFWorkbook();
  2. Sheet sheet = wb.createSheet("format sheet");
  3. CellStyle style;
  4. DataFormat format = wb.createDataFormat();
  5. Row row;
  6. Cell cell;
  7. int rowNum = 0;
  8. int colNum = 0;
  9. row = sheet.createRow(rowNum++);
  10. cell = row.createCell(colNum);
  11. cell.setCellValue(11111.25);
  12. style = wb.createCellStyle();
  13. style.setDataFormat(format.getFormat("0.0"));
  14. cell.setCellStyle(style);
  15. row = sheet.createRow(rowNum++);
  16. cell = row.createCell(colNum);
  17. cell.setCellValue(11111.25);
  18. style = wb.createCellStyle();
  19. style.setDataFormat(format.getFormat("#,##0.0000"));
  20. cell.setCellStyle(style);
  21. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  22. wb.write(fileOut);
  23. }
  24. wb.close();

1.4.1.19 将工作表调整为一页

  1. Workbook wb = new HSSFWorkbook();
  2. Sheet sheet = wb.createSheet("format sheet");
  3. PrintSetup ps = sheet.getPrintSetup();
  4. sheet.setAutobreaks(true);
  5. ps.setFitHeight((short)1);
  6. ps.setFitWidth((short)1);
  7. // Create various cells and rows for spreadsheet.
  8. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  9. wb.write(fileOut);
  10. }
  11. wb.close();

1.4.1.20 设置打印区域

  1. Workbook wb = new HSSFWorkbook();
  2. Sheet sheet = wb.createSheet("Sheet1");
  3. //sets the print area for the first sheet
  4. wb.setPrintArea(0, "$A$1:$C$2");
  5. //Alternatively:
  6. wb.setPrintArea(
  7. 0, //sheet index
  8. 0, //start column
  9. 1, //end column
  10. 0, //start row
  11. 0 //end row
  12. );
  13. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  14. wb.write(fileOut);
  15. }
  16. wb.close();

1.4.1.21 设置页脚页数

  1. Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
  2. Sheet sheet = wb.createSheet("format sheet");
  3. Footer footer = sheet.getFooter();
  4. footer.setRight( "Page " + HeaderFooter.page() + " of " + HeaderFooter.numPages() );
  5. // Create various cells and rows for spreadsheet.
  6. try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
  7. wb.write(fileOut);
  8. }
  9. wb.close();

1.4.2 Apache POI 提取Word中的文件

Apache POI 针对Word 的处理有两套API,分别如下:














HWPF XWPF
处理.doc文件 处理.docx 文件

如果是Maven项目添加如下依赖:

  1. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
  2. <dependency>
  3. <groupId>org.apache.poi</groupId>
  4. <artifactId>poi</artifactId>
  5. <version>4.1.2</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
  8. <dependency>
  9. <groupId>org.apache.poi</groupId>
  10. <artifactId>poi-ooxml</artifactId>
  11. <version>4.1.2</version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
  14. <dependency>
  15. <groupId>org.apache.poi</groupId>
  16. <artifactId>poi-ooxml-schemas</artifactId>
  17. <version>4.1.2</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.apache.poi</groupId>
  21. <artifactId>poi-scratchpad</artifactId>
  22. <version>4.1.2</version>
  23. </dependency>

如果是Gradle 项目,添加如下依赖:

  1. // Apache POI
  2. // https://mvnrepository.com/artifact/org.apache.poi/poi
  3. implementation group: 'org.apache.poi', name: 'poi', version: '4.1.2'
  4. // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
  5. implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
  6. // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas
  7. implementation group: 'org.apache.poi', name: 'poi-ooxml-schemas', version: '4.1.2'
  8. // doc 需要用
  9. implementation group: 'org.apache.poi', name: 'poi-scratchpad', version: '4.1.2'

1.4.2.1 读取Excel 数据并写入到Word示例

读取Excel 数据并写入到Word示例

1.4.2.2 读取Excel 数据并从Word中提取图片示例

接下来我们来演示如何从word中提取图片。

  1. import lombok.extern.slf4j.Slf4j;
  2. import org.apache.poi.hwpf.HWPFDocument;
  3. import org.apache.poi.hwpf.model.PicturesTable;
  4. import org.apache.poi.hwpf.usermodel.Picture;
  5. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  6. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  7. import org.apache.poi.xwpf.usermodel.XWPFPictureData;
  8. import org.apache.poi.xwpf.usermodel.XWPFRun;
  9. import java.io.*;
  10. import java.util.List;
  11. /** * @author qing-feng.zhao */
  12. @Slf4j
  13. public class SmartPoiWordUtils {
  14. /** * 静态工具类应该禁用构造方法 */
  15. private SmartPoiWordUtils(){ }
  16. /** * 写入数据到word * @param fileName * @param dataListArg * @throws IOException */
  17. public static void writeDataToWord(String fileName, List<String> dataListArg) throws IOException {
  18. try(FileOutputStream out = new FileOutputStream(fileName);){
  19. //创建一个文档
  20. XWPFDocument xwpfDocument=new XWPFDocument();
  21. //创建一个段落
  22. XWPFParagraph xwpfParagraph;
  23. //创建一片区域
  24. XWPFRun run;
  25. for (String lineData:dataListArg
  26. ) {
  27. xwpfParagraph= xwpfDocument.createParagraph();
  28. run=xwpfParagraph.createRun();
  29. run.setText(lineData);
  30. }
  31. xwpfDocument.write(out);
  32. xwpfDocument.close();
  33. }
  34. }
  35. public synchronized static void fetchPictureFromWord(File worldFilePath,String targetPictureFolder,String pictureFileName) throws IOException {
  36. String fileType=SmartFileUtils.getNormalFileType(worldFilePath.getName());
  37. Integer numberId=1;
  38. switch (fileType.toLowerCase()){
  39. case "doc":
  40. try(HWPFDocument hwpfDocument=new HWPFDocument(new FileInputStream(worldFilePath.getAbsolutePath()))
  41. ){
  42. PicturesTable picturesTable=hwpfDocument.getPicturesTable();
  43. List<Picture> pictures = picturesTable.getAllPictures();
  44. for (Picture picture : pictures) {
  45. // 输出图片到磁盘
  46. String pictureFiePath=targetPictureFolder + File.separator + pictureFileName+numberId+"."+picture.suggestFileExtension();
  47. try(OutputStream out = new FileOutputStream(pictureFiePath)){
  48. picture.writeImageContent(out);
  49. }
  50. numberId++;
  51. }
  52. }
  53. break;
  54. case "docx":
  55. try(XWPFDocument xwpfDocument=new XWPFDocument(new FileInputStream(worldFilePath.getAbsolutePath()))){
  56. //得到word的数据流
  57. List<XWPFPictureData> xwpfPictureDataList=xwpfDocument.getAllPictures();
  58. if(null!=xwpfPictureDataList&&xwpfPictureDataList.size()>0){
  59. for (XWPFPictureData item:xwpfPictureDataList
  60. ) {
  61. byte[] bytes = item.getData();
  62. String pictureFiePath=targetPictureFolder + File.separator + pictureFileName+numberId+"."+item.suggestFileExtension();
  63. File file=new File(pictureFiePath);
  64. if(file.exists()){
  65. file.delete();
  66. }
  67. try(FileOutputStream fos = new FileOutputStream(pictureFiePath)){
  68. fos.write(bytes);
  69. }
  70. numberId++;
  71. }
  72. }
  73. } catch (IOException e) {
  74. log.error("错误的文件:{}",e);
  75. log.error("出错文件路径",worldFilePath.getAbsolutePath());
  76. }
  77. break;
  78. default:
  79. log.error("{}无效的文件格式:{}",worldFilePath,fileType);
  80. break;
  81. }
  82. }
  83. }

测试调用类如下:

  1. public static void main(String[] args) {
  2. File currentWordFile=new File("/Users/zhaoqingfeng/desktop/test.docx");
  3. String targetPicturePath=currentWordFile.getParentFile().getPath();
  4. String wordFileNameWithOutFileType=SmartFileUtils.getNormalFileNameWithOutFileType(currentWordFile.getName());
  5. try {
  6. SmartPoiWordUtils.fetchPictureFromWord(currentWordFile,targetPicturePath,wordFileNameWithOutFileType);
  7. } catch (IOException e) {
  8. log.error("文件I/O异常",e);
  9. log.error("处理文件名称:{}",currentWordFile.getName());
  10. log.error("处理文件路径:{}",currentWordFile.getAbsolutePath());
  11. }
  12. }

如果word中插入一张图片,上面代码执行完成后,在/Users/zhaoqingfeng/Desktop/ 路径下就会生成一张图片。

本篇完~

发表评论

表情:
评论列表 (有 0 条评论,438人围观)

还没有评论,来说两句吧...

相关阅读