如何通过Java导出带格式的 Excel 数据到 Word 表格

在Word中制作报表时,我们经常需要将Excel中的数据复制粘贴到Word中,这样则可以直接在Word文档中查看数据而无需打开另一个Excel文件 。但是如果表格比较长,内容就会存在一定程度的丢失,无法完整显示数据 。并且当工作量到达一定程度时 , 整个过程会非常费时,降低工作效率 。那么如何轻松地将带格式的 Excel 数据导出到 Word 表格呢?不用担心,本文将通过Java应用程序详细介绍如何把带格式的Excel数据导入Word表格 。希望这篇文章能对大家有所帮助 。
使用工具:Free Spire.Office for Java
程序环境:
方法1:手动引入 。将 Free Spire.Office for Java 下载到本地,解压,找到lib文件夹下的Spire.XLS.jar文件 。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序

如何通过Java导出带格式的 Excel 数据到 Word 表格

文章插图
【如何通过Java导出带格式的 Excel 数据到 Word 表格】方法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>具体步骤:
  • 创建一个 Workbook 对象并使用 Workbook.LoadFromFile() 方法加载一个示例 Excel 文件 。
  • 通过 Workbook.Worksheets[index] 属性获取特定的工作表 。
  • 创建一个 Document 对象,并向其添加一个节 。
  • 使用 Section.AddTable() 方法添加一个表 。
  • 检测工作表中合并的单元格,并使用自定义方法 MergeCells() 合并 Word表格相应的单元格 。
  • 通过 CellRange.Value 属性获取特定 Excel 单元格的值,并使用 TableCell.AddParagraph().AppendText() 方法将其添加到 Word 表格的单元格中 。
  • 使用自定义方法 CopyStyle() 将字体样式和单元格样式从 Excel 复制到 Word 表格中 。
  • 使用 Document.SaveToFile() 方法将文档保存到 Word 文件 。
完整代码:
【Java】
import com.spire.doc.*;import com.spire.doc.FileFormat;import com.spire.doc.documents.HorizontalAlignment;import com.spire.doc.documents.PageOrientation;import com.spire.doc.documents.VerticalAlignment;import com.spire.doc.fields.TextRange;import com.spire.xls.*;public class ExportExcelToWord {public static void main(String[] args) {//下载一个Excel文件Workbook workbook = new Workbook();workbook.loadFromFile("sample.xlsx");//得到第一张工作表Worksheet sheet = workbook.getWorksheets().get(0);//创建一个Word文档Document doc = new Document();Section section = doc.addSection();section.getPageSetup().setOrientation(PageOrientation.Landscape);//添加一个表格Table table = section.addTable(true);table.resetCells(sheet.getLastRow(), sheet.getLastColumn());//合并单元格mergeCells(sheet, table);for (int r = 1; r <= sheet.getLastRow(); r++) {//设置行高table.getRows().get(r - 1).setHeight((float) sheet.getRowHeight(r));for (int c = 1; c <= sheet.getLastColumn(); c++) {CellRange xCell = sheet.getCellRange(r, c);TableCell wCell = table.get(r - 1, c - 1);//获得特定Excel单元格的值并将其添加到Word表格单元格TextRange textRange = wCell.addParagraph().appendText(xCell.getValue());// 从Excel复制字体和单元格样式到WordcopyStyle(textRange, xCell, wCell);}}//保存文档为Word文件doc.saveToFile("ExportToWord.docx", FileFormat.Docx);}//如果有合并的区域,则合并单元格private static void mergeCells(Worksheet sheet, Table table) {if (sheet.hasMergedCells()) {//从Excel中获取合并的单元格范围CellRange[] ranges = sheet.getMergedCells();for (int i = 0; i < ranges.length; i++) {int startRow = ranges[i].getRow();int startColumn = ranges[i].getColumn();int rowCount = ranges[i].getRowCount();int columnCount = ranges[i].getColumnCount();//合并Word表格中的对应单元格if (rowCount > 1 && columnCount > 1) {for (int j = startRow; j <= startRow + rowCount ; j++) {table.applyHorizontalMerge(j - 1, startColumn - 1, startColumn - 1 + columnCount - 1);}table.applyVerticalMerge(startColumn - 1, startRow - 1, startRow - 1 + rowCount - 1 );}if (rowCount > 1 && columnCount == 1 ) {table.applyVerticalMerge(startColumn - 1, startRow - 1, startRow - 1 + rowCount - 1);}if (columnCount > 1 && rowCount == 1 ) {table.applyHorizontalMerge(startRow - 1, startColumn - 1,startColumn - 1 + columnCount-1);}}}}//复制Excel单元格样式到Word表格private static void copyStyle(TextRange wTextRange, CellRange xCell, TableCell wCell) {//复制字体样式wTextRange.getCharacterFormat().setTextColor(xCell.getStyle().getFont().getColor());wTextRange.getCharacterFormat().setFontSize((float) xCell.getStyle().getFont().getSize());wTextRange.getCharacterFormat().setFontName(xCell.getStyle().getFont().getFontName());wTextRange.getCharacterFormat().setBold(xCell.getStyle().getFont().isBold());wTextRange.getCharacterFormat().setItalic(xCell.getStyle().getFont().isItalic());//复制背景色wCell.getCellFormat().setBackColor(xCell.getStyle().getColor());//复制水平对齐方式switch (xCell.getHorizontalAlignment()) {case Left:wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Left);break;case Center:wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);break;case Right:wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Right);break;}//复制垂直对齐方式switch (xCell.getVerticalAlignment()) {case Bottom:wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Bottom);break;case Center:wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);break;case Top:wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Top);break;}}}

推荐阅读