当前位置 : 主页 > 编程语言 > java >

如何通过Java应用程序将Word转为Excel

来源:互联网 收集:自由互联 发布时间:2023-02-04
平时在工作中,很多小伙伴会习惯性地将文件保存为Word文档格式,但有时会发现某些文件如果保存成Excel表格可能会更好地呈现。例如有的文本在Word文本中不如在Excel工作表编辑计算方

平时在工作中,很多小伙伴会习惯性地将文件保存为Word文档格式,但有时会发现某些文件如果保存成Excel表格可能会更好地呈现。例如有的文本在Word文本中不如在Excel工作表编辑计算方便,所以要把Word转Excel表格。那么对于之前已经保存成Word格式的文件该怎么转成Excel文件格式呢?下面就给大家分享一个高效又方便的方法,通过编程方式将Word转为Excel。

引入jar包

导入方法1:

手动引入。将 ​​Free Spire.Office for Java​​ 下载到本地,解压,找到lib文件夹下的Spire.Office.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序:

如何通过Java应用程序将Word转为Excel_转换

导入方法2:如果您想通过 ​​​Maven​​​安装,则可以在 pom.xml 文件中添加以下代码导入 JAR文件。

<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.cn/repository/maven-public/</url> </repository></repositories><dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.office.free</artifactId> <version>5.3.1</version> </dependency></dependencies>

将Word转为Excel具体步骤

为了使此代码示例易于理解,我们创建了以下三个执行特定功能的自定义方法。

  • exportTableInExcel() - 将数据从 Word 表格导出到指定的Excel 单元格。
  • copyContentInTable() - 将内容从 Word 中的表格单元格复制到Excel 单元格。
  • copyTextAndStyle() - 将带格式的文本从 Word段落复制到 Excel 单元格。

以下步骤演示了如何使用 Spire.Office for Java 将数据从 Word 文档导出到工作表。

  • 创建一个 Document 对象以加载 Word 文件。
  • 创建一个 Workbook 对象并向其添加一个名为“WordToExcel”的工作表。
  • 遍历 Word 文档中的所有节,遍历某个节下的所有文档对象,然后判断一个文档对象是段落还是表格。
  • 如果文档对象是一个段落,使用 copyTextAndStyle() 方法将段落写在 Excel 的指定单元格中。
  • 如果文档对象是表格,则使用 exportTableInExcel() 方法将表格数据从 Word 导出到 Excel 单元格。
  • 自动调整 Excel 中的行高和列宽,使单元格内的数据不会超出单元格的边界。
  • 使用 Workbook.saveToFile() 方法将工作簿保存到 Excel 文件。

完整代码

Java

import com.spire.doc.*;import com.spire.doc.documents.Paragraph;import com.spire.doc.fields.DocPicture;import com.spire.doc.fields.TextRange;import com.spire.xls.*;import java.awt.*;public class ConvertWordToExcel { public static void main(String[] args) { //创建一个 Document 对象 Document doc = new Document(); //加载 Word 文件 doc.loadFromFile("XXX公司采购单.docx"); //创建一个 Workbook 对象 Workbook wb = new Workbook(); //删除默认工作表 wb.getWorksheets().clear(); //创建一个名为“WordToExcel”的工作表 Worksheet worksheet = wb.createEmptySheet("WordToExcel"); int row = 1; int column = 1; //循环遍历 Word 文档中的各个节 for (int i = 0; i < doc.getSections().getCount(); i++) { //获取特定节 Section section = doc.getSections().get(i); //遍历某个节下的所有文档对象 for (int j = 0; j < section.getBody().getChildObjects().getCount(); j++) { //获取特定的文档对象 DocumentObject documentObject = section.getBody().getChildObjects().get(j); //判断对象是否为段落 if (documentObject instanceof Paragraph) { CellRange cell = worksheet.getCellRange(row, column); Paragraph paragraph = (Paragraph) documentObject; //将段落从 Word 复制到特定单元格 copyTextAndStyle(cell, paragraph); row++; } //判断对象是否为表格 if (documentObject instanceof Table) { Table table = (Table) documentObject; //将表格数据从 Word 导出到 Excel int currentRow = exportTableInExcel(worksheet, row, table); row = currentRow; } } } //在单元格中换行文本 worksheet.getAllocatedRange().isWrapText(true); //自动调整行高和列宽 worksheet.getAllocatedRange().autoFitRows(); worksheet.getAllocatedRange().autoFitColumns(); //将工作簿保存到 Excel 文件 wb.saveToFile("output.xlsx", ExcelVersion.Version2013); } //将数据从 Word 表格导出到 Excel 单元格 private static int exportTableInExcel(Worksheet worksheet, int row, Table table) { CellRange cell; int column; for (int i = 0; i < table.getRows().getCount(); i++) { column = 1; TableRow tbRow = table.getRows().get(i); for (int j = 0; j < tbRow.getCells().getCount(); j++) { TableCell tbCell = tbRow.getCells().get(j); cell = worksheet.getCellRange(row, column); cell.borderAround(LineStyleType.Thin, Color.BLACK); copyContentInTable(tbCell, cell); column++; } row++; } return row; } //将内容从 Word 表格单元格复制到 Excel 单元格 private static void copyContentInTable(TableCell tbCell, CellRange cell) { Paragraph newPara = new Paragraph(tbCell.getDocument()); for (int i = 0; i < tbCell.getChildObjects().getCount(); i++) { DocumentObject documentObject = tbCell.getChildObjects().get(i); if (documentObject instanceof Paragraph) { Paragraph paragraph = (Paragraph) documentObject; for (int j = 0; j < paragraph.getChildObjects().getCount(); j++) { DocumentObject cObj = paragraph.getChildObjects().get(j); newPara.getChildObjects().add(cObj.deepClone()); } if (i < tbCell.getChildObjects().getCount() - 1) { newPara.appendText("\n"); } } } copyTextAndStyle(cell, newPara); } //将段落的文本和样式复制到单元格 private static void copyTextAndStyle(CellRange cell, Paragraph paragraph) { RichText richText = cell.getRichText(); richText.setText(paragraph.getText()); int startIndex = 0; for (int i = 0; i < paragraph.getChildObjects().getCount(); i++) { DocumentObject documentObject = paragraph.getChildObjects().get(i); if (documentObject instanceof TextRange) { TextRange textRange = (TextRange) documentObject; String fontName = textRange.getCharacterFormat().getFontName(); boolean isBold = textRange.getCharacterFormat().getBold(); Color textColor = textRange.getCharacterFormat().getTextColor(); float fontSize = textRange.getCharacterFormat().getFontSize(); String textRangeText = textRange.getText(); int strLength = textRangeText.length(); ExcelFont font = new ExcelFont(cell.getWorksheet().getWorkbook().createFont()); font.setColor(textColor); font.isBold(isBold); font.setSize(fontSize); font.setFontName(fontName); int endIndex = startIndex + strLength; richText.setFont(startIndex, endIndex, font); startIndex += strLength; } if (documentObject instanceof DocPicture) { DocPicture picture = (DocPicture) documentObject; cell.getWorksheet().getPictures().add(cell.getRow(), cell.getColumn(), picture.getImage()); cell.getWorksheet().setRowHeightInPixels(cell.getRow(), 1, picture.getImage().getHeight()); } } switch (paragraph.getFormat().getHorizontalAlignment()) { case Left: cell.setHorizontalAlignment(HorizontalAlignType.Left); break; case Center: cell.setHorizontalAlignment(HorizontalAlignType.Center); break; case Right: cell.setHorizontalAlignment(HorizontalAlignType.Right); break; } }}

效果图

如何通过Java应用程序将Word转为Excel_Java_02

—本文完—

上一篇:破惑、问道、安顿。我的2022年终总结。
下一篇:没有了
网友评论