最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)Word、Excel、PDF文件格式互轉的幾種實現(xiàn)方式

 更新時間:2025年12月10日 09:42:25   作者:Full?Stack?Developme  
在Java中實現(xiàn)文檔格式轉換通常需要使用專門的庫,下面我將介紹如何使用Apache?POI、iText和Aspose等庫來實現(xiàn)這些轉換,有需要的小伙伴可以了解下

方案一:使用開源庫(免費)

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. 實現(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轉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)) {
            
            // 簡單實現(xiàn):提取文本內容并寫入PDF
            document.getParagraphs().forEach(paragraph -> {
                pdfDoc.add(new Paragraph(paragraph.getText()));
            });
        }
    }
    
    // Excel轉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轉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轉Excel (簡單實現(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(功能更強大)

Aspose提供了更完整和專業(yè)的文檔轉換解決方案,但需要購買許可證。

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. 實現(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轉PDF
    public static void convertWordToPdf(String inputPath, String outputPath) throws Exception {
        Document doc = new Document(inputPath);
        doc.save(outputPath, SaveFormat.PDF);
    }
    
    // Excel轉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轉Word
    public static void convertPdfToWord(String inputPath, String outputPath) throws Exception {
        Document doc = new Document(inputPath);
        doc.save(outputPath, SaveFormat.DOCX);
    }
    
    // PDF轉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轉Excel
    public static void convertWordToExcel(String inputPath, String outputPath) throws Exception {
        // 先將Word轉為PDF,再轉為Excel
        String tempPdf = "temp.pdf";
        convertWordToPdf(inputPath, tempPdf);
        convertPdfToExcel(tempPdf, outputPath);
        new java.io.File(tempPdf).delete();
    }
    
    // Excel轉Word
    public static void convertExcelToWord(String inputPath, String outputPath) throws Exception {
        // 先將Excel轉為PDF,再轉為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(); // 設置許可證
            AsposeDocumentConverter.convertWordToPdf("input.docx", "output.pdf");
            AsposeDocumentConverter.convertExcelToPdf("input.xlsx", "output.pdf");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

注意事項

  • 開源方案限制:使用Apache POI和iText的開源方案可能無法完美處理復雜的文檔格式和布局
  • 商業(yè)方案:Aspose等商業(yè)庫提供更完整的解決方案,但需要購買許可證
  • 字體處理:確保系統(tǒng)中安裝了文檔中使用的字體,以避免轉換時的字體問題
  • 復雜格式:表格、圖片、特殊格式等在轉換時可能需要特殊處理

根據(jù)您的具體需求和預算,可以選擇適合的方案。對于簡單的文檔轉換,開源方案可能足夠;對于企業(yè)級應用,商業(yè)庫可能是更好的選擇。

到此這篇關于Java實現(xiàn)Word、Excel、PDF文件格式互轉的幾種實現(xiàn)方式的文章就介紹到這了,更多相關Java文件格式互轉內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java中l(wèi)ambda表達式的基本運用

    Java中l(wèi)ambda表達式的基本運用

    大家好,本篇文章主要講的是Java中l(wèi)ambda表達式的基本運用,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • 淺談@FeignClient中name和value屬性的區(qū)別

    淺談@FeignClient中name和value屬性的區(qū)別

    這篇文章主要介紹了@FeignClient中name和value屬性的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • SpringCloud feign微服務調用之間的異常處理方式

    SpringCloud feign微服務調用之間的異常處理方式

    這篇文章主要介紹了SpringCloud feign微服務調用之間的異常處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java使用jacob將微軟office中word、excel、ppt轉成pdf

    Java使用jacob將微軟office中word、excel、ppt轉成pdf

    這篇文章主要為大家詳細介紹了Java使用jacob將微軟office中word、excel、ppt轉成pdf,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Java多線程異步調用性能調優(yōu)方法詳解

    Java多線程異步調用性能調優(yōu)方法詳解

    這篇文章主要為大家詳細介紹了Java多線程異步調用性能調優(yōu),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 解決@Autowired報錯Could not autowire. No beans of ‘XXX‘ type found問題

    解決@Autowired報錯Could not autowire. No bea

    介紹了在IDEA中使用@Autowired報錯Couldnot autowire. No beans of 'XXX' type found的解決方法,原因是@Autowired在注入service時,由于service接口沒有實現(xiàn)類,而mybatis僅需提供Dao接口,導致@Autowired無法識別
    2024-12-12
  • java反射簡單實例

    java反射簡單實例

    這篇文章主要介紹了java反射機制,以一個簡單實例形式分析了Java反射的原理與實現(xiàn)技巧,需要的朋友可以參考下
    2015-02-02
  • java并發(fā)編程專題(一)----線程基礎知識

    java并發(fā)編程專題(一)----線程基礎知識

    這篇文章主要介紹了java并發(fā)編程線程的基礎知識,文中講解非常詳細,幫助大家更好的學習JAVA并發(fā)編程,感興趣想學習JAVA的可以了解下
    2020-06-06
  • Intellij IDEA導入eclipse web項目的操作步驟詳解

    Intellij IDEA導入eclipse web項目的操作步驟詳解

    Eclipse當中的web項目都會有這兩個文件,但是idea當中應該是沒有的,所以導入會出現(xiàn)兼容問題,但是本篇文章會教大家如何導入,并且導入過后還能使用tomcat運行,需要的朋友可以參考下
    2023-08-08
  • Java之NoClassDefFoundError的原因及分析

    Java之NoClassDefFoundError的原因及分析

    在Java開發(fā)中,經(jīng)常會遇到ClassNotFoundException和NoClassDefFoundError異常,ClassNotFoundException發(fā)生在編譯時JVM無法找到類,而NoClassDefFoundError則發(fā)生在運行時JVM無法加載類,這兩個異常雖然原因相似,但有本質區(qū)別
    2024-09-09

最新評論

贺兰县| 衡山县| 苗栗县| 广宗县| 洮南市| 湖北省| 元江| 团风县| 公主岭市| 山东省| 黄平县| 阜新| 洛浦县| 曲阳县| 盘锦市| 牙克石市| 明溪县| 伊金霍洛旗| 西畴县| 来宾市| 漳州市| 邵武市| 西藏| 封丘县| 盐山县| 西乡县| 越西县| 上栗县| 景东| 将乐县| 昌都县| 鲁山县| 朝阳区| 镇平县| 行唐县| 阳信县| 吐鲁番市| 龙门县| 迭部县| 马边| 漳浦县|