Java實(shí)現(xiàn)Word、Excel、PDF文件格式互轉(zhuǎn)的幾種實(shí)現(xiàn)方式
方案一:使用開源庫(免費(fèi))
1. 添加Maven依賴
<dependencies>
<!-- Apache POI for Word and Excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<!-- iText for PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.2.5</version>
<type>pom</type>
</dependency>
<!-- Apache PDFBox for PDF handling -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.27</version>
</dependency>
</dependencies>2. 實(shí)現(xiàn)代碼示例
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.*;
public class DocumentConverter {
// Word轉(zhuǎn)PDF
public static void convertWordToPdf(String inputPath, String outputPath) throws IOException {
try (XWPFDocument document = new XWPFDocument(new FileInputStream(inputPath));
PdfWriter writer = new PdfWriter(outputPath);
PdfDocument pdf = new PdfDocument(writer);
Document pdfDoc = new Document(pdf)) {
// 簡單實(shí)現(xiàn):提取文本內(nèi)容并寫入PDF
document.getParagraphs().forEach(paragraph -> {
pdfDoc.add(new Paragraph(paragraph.getText()));
});
}
}
// Excel轉(zhuǎn)PDF
public static void convertExcelToPdf(String inputPath, String outputPath) throws IOException {
try (Workbook workbook = new XSSFWorkbook(new FileInputStream(inputPath));
PdfWriter writer = new PdfWriter(outputPath);
PdfDocument pdf = new PdfDocument(writer);
Document pdfDoc = new Document(pdf)) {
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
StringBuilder rowText = new StringBuilder();
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
rowText.append(cell.getStringCellValue()).append("\t");
break;
case NUMERIC:
rowText.append(cell.getNumericCellValue()).append("\t");
break;
default:
rowText.append("\t");
}
}
pdfDoc.add(new Paragraph(rowText.toString()));
}
}
}
// PDF轉(zhuǎn)Word (簡單文本提取)
public static void convertPdfToWord(String inputPath, String outputPath) throws IOException {
try (PDDocument document = PDDocument.load(new File(inputPath));
XWPFWordDocument wordDocument = new XWPFDocument();
FileOutputStream out = new FileOutputStream(outputPath)) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
// 創(chuàng)建段落并添加文本
XWPFParagraph paragraph = wordDocument.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(text);
wordDocument.write(out);
}
}
// PDF轉(zhuǎn)Excel (簡單實(shí)現(xiàn))
public static void convertPdfToExcel(String inputPath, String outputPath) throws IOException {
try (PDDocument document = PDDocument.load(new File(inputPath));
Workbook workbook = new XSSFWorkbook();
FileOutputStream out = new FileOutputStream(outputPath)) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
Sheet sheet = workbook.createSheet("PDF Content");
String[] lines = text.split("\n");
for (int i = 0; i < lines.length; i++) {
Row row = sheet.createRow(i);
Cell cell = row.createCell(0);
cell.setCellValue(lines[i]);
}
workbook.write(out);
}
}
}方案二:使用商業(yè)庫Aspose(功能更強(qiáng)大)
Aspose提供了更完整和專業(yè)的文檔轉(zhuǎn)換解決方案,但需要購買許可證。
1. 添加Maven依賴
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>22.11</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>22.11</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>22.11</version>
</dependency>
</dependencies>2. 實(shí)現(xiàn)代碼示例
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.aspose.cells.Workbook;
import com.aspose.pdf.*;
public class AsposeDocumentConverter {
// Word轉(zhuǎn)PDF
public static void convertWordToPdf(String inputPath, String outputPath) throws Exception {
Document doc = new Document(inputPath);
doc.save(outputPath, SaveFormat.PDF);
}
// Excel轉(zhuǎn)PDF
public static void convertExcelToPdf(String inputPath, String outputPath) throws Exception {
Workbook workbook = new Workbook(inputPath);
workbook.save(outputPath, com.aspose.cells.SaveFormat.PDF);
}
// PDF轉(zhuǎn)Word
public static void convertPdfToWord(String inputPath, String outputPath) throws Exception {
Document doc = new Document(inputPath);
doc.save(outputPath, SaveFormat.DOCX);
}
// PDF轉(zhuǎn)Excel
public static void convertPdfToExcel(String inputPath, String outputPath) throws Exception {
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(inputPath);
ExcelSaveOptions options = new ExcelSaveOptions();
pdfDocument.save(outputPath, options);
}
// Word轉(zhuǎn)Excel
public static void convertWordToExcel(String inputPath, String outputPath) throws Exception {
// 先將Word轉(zhuǎn)為PDF,再轉(zhuǎn)為Excel
String tempPdf = "temp.pdf";
convertWordToPdf(inputPath, tempPdf);
convertPdfToExcel(tempPdf, outputPath);
new java.io.File(tempPdf).delete();
}
// Excel轉(zhuǎn)Word
public static void convertExcelToWord(String inputPath, String outputPath) throws Exception {
// 先將Excel轉(zhuǎn)為PDF,再轉(zhuǎn)為Word
String tempPdf = "temp.pdf";
convertExcelToPdf(inputPath, tempPdf);
convertPdfToWord(tempPdf, outputPath);
new java.io.File(tempPdf).delete();
}
}使用示例
public class Main {
public static void main(String[] args) {
try {
// 使用開源方案
DocumentConverter.convertWordToPdf("input.docx", "output.pdf");
DocumentConverter.convertExcelToPdf("input.xlsx", "output.pdf");
// 使用Aspose方案(需要許可證)
// AsposeDocumentConverter.setLicense(); // 設(shè)置許可證
AsposeDocumentConverter.convertWordToPdf("input.docx", "output.pdf");
AsposeDocumentConverter.convertExcelToPdf("input.xlsx", "output.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
}注意事項(xiàng)
- 開源方案限制:使用Apache POI和iText的開源方案可能無法完美處理復(fù)雜的文檔格式和布局
- 商業(yè)方案:Aspose等商業(yè)庫提供更完整的解決方案,但需要購買許可證
- 字體處理:確保系統(tǒng)中安裝了文檔中使用的字體,以避免轉(zhuǎn)換時(shí)的字體問題
- 復(fù)雜格式:表格、圖片、特殊格式等在轉(zhuǎn)換時(shí)可能需要特殊處理
根據(jù)您的具體需求和預(yù)算,可以選擇適合的方案。對(duì)于簡單的文檔轉(zhuǎn)換,開源方案可能足夠;對(duì)于企業(yè)級(jí)應(yīng)用,商業(yè)庫可能是更好的選擇。
到此這篇關(guān)于Java實(shí)現(xiàn)Word、Excel、PDF文件格式互轉(zhuǎn)的幾種實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)Java文件格式互轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java實(shí)現(xiàn)Word、Excel、PDF文件在線預(yù)覽幾種實(shí)現(xiàn)方式
- Java將Word、Excel、PDF和PPT轉(zhuǎn)換為OFD格式的詳細(xì)步驟
- 基于Java實(shí)現(xiàn)將word,excel文件轉(zhuǎn)換為pdf的工具類
- Java實(shí)現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼
- Java實(shí)現(xiàn)Word/Excel/TXT轉(zhuǎn)PDF的方法
- Java使用jacob將微軟office中word、excel、ppt轉(zhuǎn)成pdf
相關(guān)文章
mac配置Java?web開發(fā)環(huán)境的全過程
對(duì)于開發(fā)人員來說,電腦的性能很重要,所以換了Mac后需要重新配置開發(fā)環(huán)境,這篇文章主要介紹了mac配置Java?web開發(fā)環(huán)境的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2025-09-09
String類下compareTo()與compare()方法比較
這篇文章主要介紹了String類下compareTo()與compare()方法比較的相關(guān)資料,需要的朋友可以參考下2017-05-05
Java實(shí)戰(zhàn)權(quán)限管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+MyBatis+AOP+LayUI+Mysql實(shí)現(xiàn)一個(gè)權(quán)限管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2022-01-01
Java的ThreadContext類加載器的實(shí)現(xiàn)
這篇文章主要介紹了Java的ThreadContext類加載器的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06

