1、在Java程序中引入jar.
2、在程序中键入如下代码内容:import com.spire.doc.Document; import com.spire.doc.Tabl髫潋啜缅e; import com.spire.doc.TableCell; import com.spire.doc.documents.HorizontalAlignment; import com.spire.doc.documents.VerticalAlignment; import com.spire.doc.fields.TextRange; import com.spire.xls.CellRange; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class CopyExcelTableToWord { public static void main(String[] args) { //加载Excel 示例文档 Workbook workbook = new Workbook(); workbook.loadFromFile("test.xlsx"); //获取第一个工作表 Worksheet sheet = workbook.getWorksheets().get(0); //复制到Word文档 copyToWord(sheet.getAllocatedRange(), "result.docx"); } public static void copyToWord(CellRange cell, String fPath) { //添加表格 Document doc = new Document(); Table table = doc.addSection().addTable(true); table.resetCells(cell.getRowCount(), cell.getColumnCount()); //复制表格内容 for (int r = 1; r <= cell.getRowCount(); r++) { for (int c = 1; c <= cell.getColumnCount(); c++) { CellRange xCell = cell.get(r, c); CellRange mergeArea = xCell.getMergeArea(); //合并单元格 if (mergeArea != null && mergeArea.getRow() == r && mergeArea.getColumn() == c) { int rowIndex = mergeArea.getRow(); int columnIndex = mergeArea.getColumn(); int rowCount = mergeArea.getRowCount(); int columnCount = mergeArea.getColumnCount(); for (int m = 0; m < rowCount; m++) { table.applyHorizontalMerge(rowIndex - 1 + m, columnIndex - 1, columnIndex + columnCount - 2); } table.applyVerticalMerge(columnIndex - 1, rowIndex - 1, rowIndex + rowCount - 2); } //复制内容 TableCell wCell = table.getRows().get(r - 1).getCells().get(c - 1); if (!xCell.getDisplayedText().isEmpty()) { TextRange textRange = wCell.addParagraph().appendText(xCell.getDisplayedText()); copyStyle(textRange, xCell, wCell); } else { wCell.getCellFormat().setBackColor(xCell.getStyle().getColor()); } } } doc.saveToFile(fPath,com.spire.doc.FileFormat.Docx); } 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; default: 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; default: break; } } }
3、执行程序后,生成Word结果文档。以上代码中的文件路径为IDEA项目程序运行路径,如本次路径F:\IDEAproject\潮贾篡绐CopyExcelTableToWord_Office\result.docx,文件路径可另行自定义。测试的Excel源文档及Word结果文档如下图: