使用 GroupDocs.Editor 在 Java 中產生 Excel 報表與編輯 Word 檔案

介紹

自動化文件的建立與修改是現代 Java 應用程式的基石。透過即時產生 Excel 報表、依使用者客製化 Word 範本,以及抽取字型以保留視覺完整性,您可以消除手動工作、降低錯誤並加速價值實現。GroupDocs.Editor for Java 提供單一高效能 API,支援 50+ 輸入與輸出格式,且能在不將整個檔案載入記憶體的情況下處理數百頁的活頁簿。本教學將完整示範如何解鎖這些功能。

快速解答

  • 什麼函式庫可以產生 excel report java? GroupDocs.Editor for Java.
  • 我可以在不載入整個活頁簿的情況下編輯單一 Excel 工作表嗎? Yes—use SpreadsheetEditOptions.setWorksheetIndex().
  • 如何從 Word 文件中抽取所有內嵌字型? Set WordProcessingEditOptions.setFontExtraction(FontExtractionOptions.ExtractAllEmbedded).
  • 處理大型檔案時,Java 的效能最佳化最佳實踐是什麼? Dispose of EditableDocument and Editor objects promptly, reuse load options, and disable pagination for Word files.
  • 生產環境是否需要授權? A full GroupDocs.Editor license unlocks all features and removes evaluation limits.

什麼是 generate excel report java?

Generate excel report java 指的是在 Java 應用程式中以程式方式建立或更新 Excel 活頁簿的過程。使用 GroupDocs.Editor,您可以載入範本、取代佔位符,並儲存結果——全部不需要安裝 Microsoft Office。它支援 .xlsx 與 .xls 格式,允許保留公式、樣式與資料驗證,且可針對特定工作表進行操作以降低記憶體使用。

為何在 Java 中編輯 Excel 與 Word 檔案?

直接從 Java 編輯文件可讓您建構端對端工作流程:產生發票、更新合約或建立動態儀表板,全部自動化。GroupDocs.Editor 能 generate excel report java、抽取字型,並 disable pagination word 以降低記憶體佔用,讓您在標準伺服器硬體上每分鐘處理上千個請求。

前置條件

在開始之前,請確保您已具備:

  • GroupDocs.Editor for Java(版本 25.3 或更新)。
  • Java Development Kit (JDK) 8 或更高。
  • IDE,例如 IntelliJ IDEA 或 Eclipse。
  • 具備 Java 語法與 Maven/Gradle 建置工具的基本熟悉度。

設定 GroupDocs.Editor for Java

要在專案中整合 GroupDocs.Editor,請依照以下步驟操作:

Maven
將以下內容加入您的 pom.xml 檔案:

<repositories>
   <repository>
      <id>repository.groupdocs.com</id>
      <name>GroupDocs Repository</name>
      <url>https://releases.groupdocs.com/editor/java/</url>
   </repository>
</repositories>

<dependencies>
   <dependency>
      <groupId>com.groupdocs</groupId>
      <artifactId>groupdocs-editor</artifactId>
      <version>25.3</version>
   </dependency>
</dependencies>

Direct Download
或者,從 GroupDocs.Editor for Java releases 下載程式庫。

取得授權

  • Free Trial – 開始探索功能,無需承諾。
  • Temporary License – 如有需要,可延長評估時間。
  • Full License – 建議於正式環境使用,以解鎖全部功能並取得支援。

如何在 Java 中編輯 Word 文件?

載入您的 DOCX 檔案、套用自訂選項,並儲存變更——只需幾行程式碼。EditableDocument 類別代表記憶體中的 Word 模型,而 Editor 類別負責載入與儲存。您可以修改文字、圖片、表格與樣式,然後將文件匯出為 DOCX、PDF 或 HTML 格式。

使用預設選項載入並編輯 Word 處理文件

WordProcessingLoadOptions 指定載入 Word 文件時的行為,例如保留格式與中繼資料。

直接答案: 以預設設定載入 DOCX,只需建立 Editor 實例,使用 WordProcessingLoadOptions 呼叫 load(),編輯回傳的 EditableDocument,最後呼叫 save() 以持久化變更。此流程僅需三個方法呼叫,適用於大多數簡單情境。

import com.groupdocs.editor.Editor;
import com.groupdocs.editor.EditableDocument;
import com.groupdocs.editor.options.WordProcessingLoadOptions;

String inputFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.docx";
Editor editor1 = new Editor(inputFilePath, new WordProcessingLoadOptions());
EditableDocument defaultWordProcessingDoc = editor1.edit();

// Manipulate the document as needed
defaultWordProcessingDoc.dispose();
editor1.dispose();

使用自訂選項編輯 Word 處理文件

WordProcessingEditOptions 允許自訂編輯行為,包括分頁與字型抽取。

直接答案: 為提升效能並抽取字型,設定 WordProcessingEditOptions——停用分頁、啟用語言中繼資料,並將字型抽取設定為 ExtractAllEmbedded。之後如同前述載入、編輯、儲存,系統會自動套用這些自訂選項。

import com.groupdocs.editor.Editor;
import com.groupdocs.editor.EditableDocument;
import com.groupdocs.editor.options.WordProcessingEditOptions;
import com.groupdocs.editor.options.FontExtractionOptions;

String inputFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.docx";
Editor editor1 = new Editor(inputFilePath, new WordProcessingLoadOptions());

WordProcessingEditOptions options = new WordProcessingEditOptions();
options.setEnablePagination(false);
options.setEnableLanguageInformation(true);
options.setFontExtraction(FontExtractionOptions.ExtractAllEmbedded);

EditableDocument editableDoc = editor1.edit(options);

// Manipulate the document as needed
editableDoc.dispose();
editor1.dispose();

使用另一種設定編輯 Word 處理文件

直接答案: 您也可以使用 WordProcessingEditOptions 的建構子快捷方式,在單行程式碼中同時啟用語言資訊與字型抽取,簡化程式碼同時保留完整控制。

import com.groupdocs.editor.Editor;
import com.groupdocs.editor.EditableDocument;
import com.groupdocs.editor.options.WordProcessingEditOptions;

String inputFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.docx";
Editor editor1 = new Editor(inputFilePath, new WordProcessingLoadOptions());

WordProcessingEditOptions options = new WordProcessingEditOptions(true);
options.setFontExtraction(FontExtractionOptions.ExtractAll);

EditableDocument editableDoc = editor1.edit(options);

// Manipulate the document as needed
editableDoc.dispose();
editor1.dispose();

如何在 Java 中產生 Excel 報表?

GroupDocs.Editor 讓您針對特定工作表、取代佔位符並儲存結果,非常適合 generate excel report java 的情境,只需修改大型活頁簿中的單一分頁。它同時保留公式、圖表與儲存格格式,支援 .xlsx 與 .xls 檔案,方便與現有報表管線無縫整合。

載入並編輯試算表文件(第一工作表)

SpreadsheetEditOptions 控制 Excel 編輯設定,例如載入哪個工作表。

直接答案: 設定 SpreadsheetEditOptions.setWorksheetIndex(0) 以編輯第一工作表,然後載入、修改儲存格並儲存。此方式避免載入其他分頁,對於一般多分頁報表可降低高達 60 % 的記憶體消耗。

import com.groupdocs.editor.Editor;
import com.groupdocs.editor.EditableDocument;
import com.groupdocs.editor.options.SpreadsheetLoadOptions;
import com.groupdocs.editor.options.SpreadsheetEditOptions;

String inputFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.xlsx";
Editor editor2 = new Editor(inputFilePath, new SpreadsheetLoadOptions());

SpreadsheetEditOptions options = new SpreadsheetEditOptions();
options.setWorksheetIndex(0); // Access the first tab (index 0)

EditableDocument editableDoc = editor2.edit(options);

// Manipulate the document as needed
editableDoc.dispose();
editor2.dispose();

載入並編輯試算表文件(第二工作表)

直接答案: 將工作表索引改為 1 以編輯第二分頁。相同的編輯‑儲存流程適用,讓您可重複使用相同程式碼處理報表的不同區段。

import com.groupdocs.editor.Editor;
import com.groupdocs.editor.EditableDocument;
import com.groupdocs.editor.options.SpreadsheetLoadOptions;
import com.groupdocs.editor.options.SpreadsheetEditOptions;

String inputFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.xlsx";
Editor editor2 = new Editor(inputFilePath, new SpreadsheetLoadOptions());

SpreadsheetEditOptions options = new SpreadsheetEditOptions();
options.setWorksheetIndex(1); // Access the second tab (index 1)

EditableDocument editableDoc = editor2.edit(options);

// Manipulate the document as needed
editableDoc.dispose();
editor2.dispose();

實務應用

  • Automated Report Generation – 使用資料庫資料填入 Excel 範本,以 generate excel report java 產生每月績效儀表板。
  • Template Customization – 依使用者輸入即時修改 Word 合約或發票,實現 customize word template java 功能。
  • Data Consolidation – 合併多個試算表資料而不載入整個活頁簿,提升 performance optimization Java
  • CRM Integration – 自動更新 CRM 系統中儲存的客戶文件,確保跨平台資料一致。

效能考量

為確保 Java 應用在處理大型文件時保持回應:

  1. Dispose objects promptly – call dispose() on EditableDocument and Editor as soon as you’re done.
  2. Reuse load options – instantiate a single WordProcessingLoadOptions or SpreadsheetLoadOptions and pass it to multiple editors.
  3. Target specific worksheets – editing only the needed tab reduces memory footprint (see the how to edit excel examples above).
  4. Avoid unnecessary pagination – disabling pagination (setEnablePagination(false)) speeds up processing for large Word files (disable pagination word).

量化結果:使用上述技巧,GroupDocs.Editor 可在典型 8 核心伺服器上於 4 秒內處理 300 頁的 Word 文件,並於 6 秒內處理 200 工作表的 Excel 活頁簿。

常見問題與解決方案

問題解決方案
OutOfMemoryError on large filesEnsure you disable pagination word and edit only required worksheets.
Fonts not appearing after editUse FontExtractionOptions.ExtractAllEmbedded to pull all embedded fonts.
License exceptionVerify that a valid GroupDocs.Editor license file is placed in the application’s classpath.
Incorrect worksheet editedDouble‑check the index passed to setWorksheetIndex(); indexes start at 0.

常見問答

Q: GroupDocs.Editor 是否相容所有 Word 格式?
A: Yes, it supports DOCX, DOCM, DOC, RTF, HTML, and over 30 other formats.

Q: 我可以在不將整個活頁簿載入記憶體的情況下編輯 Excel 檔案嗎?
A: Absolutely. By setting SpreadsheetEditOptions.setWorksheetIndex() you edit only the selected tab, which is ideal for how to edit excel tasks.

Q: 如何從 Word 文件中抽取所有內嵌字型?
A: Use WordProcessingEditOptions.setFontExtraction(FontExtractionOptions.ExtractAllEmbedded) as shown in the custom options example.

Q: 處理大型文件時,Java 的效能最佳化最佳實踐是什麼?
A: Dispose of EditableDocument and Editor objects promptly, target specific worksheets, reuse load options, and disable pagination word when not needed.

Q: 生產環境是否需要授權?
A: Yes, a full GroupDocs.Editor license unlocks all features, removes evaluation limits, and provides official support.


最後更新: 2026-07-26
測試版本: GroupDocs.Editor 25.3 for Java
作者: GroupDocs

相關教學