前言
在Word中創建報告時,我們經常會遇到這樣的情況:我們需要將資料從Excel中復制和粘貼到Word中,這樣讀者就可以直接在Word中瀏覽資料,而不用打開Excel檔案,在本文中,您將學習如何使用Spire.Office for Java將Excel資料轉換為Word表格并保留格式,
程式環境
安裝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>
小tips:請注意版本號的變化
將帶格式的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;
}
}
}
效果圖

---THE END---
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/510662.html
標籤:Java
