共计 4261 个字符,预计需要花费 11 分钟才能阅读完成。
在 Word 中创立报告时,咱们常常会遇到这样的状况:咱们须要将数据从 Excel 中复制和粘贴到 Word 中,这样读者就能够间接在 Word 中浏览数据,而不必关上 Excel 文档。
程序环境
装置 Spire.Office for Java
首先,你须要在你的 Java 程序中增加 Spire.Office.jar 文件作为一个依赖项。该 JAR 文件能够从这个链接下载。如果你应用 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</artifactId> | |
<version>7.9.6</version> | |
</dependency> | |
</dependencies> |
将带格局的 Excel 数据导出到 Word 表格
步骤如下:
创立一个 Workbook 对象,并应用 Workbook.loadFromFile() 办法加载一个 Excel 样本文件。
• 应用 Workbook.getWorksheets().get() 办法获取一个特定的工作表。
• 创立一个 Document 对象,并向其增加一个章节。
• 应用 Section.addTable() 办法增加一个表格。
• 检测工作表中的合并单元格,并应用自定义办法 mergeCells() 合并 Word tale 中的相应单元格。
• 应用 CellRange.getValue() 办法获取特定 Excel 单元格的值,并应用 TableCell.addParagraph().appendText() 办法将其增加到 Word 表中的一个单元格。
• 应用自定义办法 copyStyle() 将字体款式和单元格款式从 Excel 复制到 Word 表格中。
• 应用 Document.saveToFile() 办法将文档保留到 Word 文件中。
代码示例
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("C:/Users/Administrator/Desktop/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 复制字体和单元格款式到 Word | |
copyStyle(textRange, xCell, wCell); | |
} | |
} | |
//Save the document to a Word file 保存文档为 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; | |
} | |
} | |
} |
效果图
正文完