欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Java實(shí)現(xiàn)Excel轉(zhuǎn)PDF的示例詳解

 更新時(shí)間:2025年02月10日 10:19:10   作者:五行星辰  
在實(shí)際的開發(fā)過程中,我們常常會(huì)遇到需要將 Excel 文件轉(zhuǎn)換為 PDF 文件的需求,本文為大家介紹一種Java中的常見實(shí)現(xiàn)方式,需要的可以參考一下

在實(shí)際的開發(fā)過程中,我們常常會(huì)遇到需要將 Excel 文件轉(zhuǎn)換為 PDF 文件的需求。Java 提供了多種庫和工具來實(shí)現(xiàn)這個(gè)功能,下面我將為你介紹一種常見的實(shí)現(xiàn)方式,使用 Apache POI 讀取 Excel 文件,再使用 iText 生成 PDF 文件。

1. 引入所需的庫

首先,我們需要在項(xiàng)目中引入 Apache POI 和 iText 相關(guān)的庫。如果你使用的是 Maven 項(xiàng)目,就在 pom.xml 文件里添加以下依賴:

<!-- Apache POI 用于讀取 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 用于生成 PDF 文件 -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

2. 實(shí)現(xiàn) Excel 轉(zhuǎn) PDF 的代碼

下面是具體的 Java 代碼示例:

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
import java.io.*;
 
public class ExcelToPdfConverter {
    public static void main(String[] args) {
        try {
            // 讀取 Excel 文件
            FileInputStream excelFile = new FileInputStream("input.xlsx");
            Workbook workbook = new XSSFWorkbook(excelFile);
            Sheet sheet = workbook.getSheetAt(0);
 
            // 創(chuàng)建 PDF 文檔
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
            document.open();
 
            // 創(chuàng)建 PDF 表格,列數(shù)與 Excel 表格的列數(shù)相同
            int columnCount = sheet.getRow(0).getLastCellNum();
            PdfPTable pdfTable = new PdfPTable(columnCount);
 
            // 遍歷 Excel 表格的每一行
            for (Row row : sheet) {
                // 遍歷當(dāng)前行的每一個(gè)單元格
                for (Cell cell : row) {
                    // 獲取單元格的值
                    String cellValue = getCellValueAsString(cell);
                    // 創(chuàng)建 PDF 表格的單元格
                    PdfPCell pdfCell = new PdfPCell(new Phrase(cellValue));
                    // 將單元格添加到 PDF 表格中
                    pdfTable.addCell(pdfCell);
                }
            }
 
            // 將 PDF 表格添加到 PDF 文檔中
            document.add(pdfTable);
 
            // 關(guān)閉文檔和 Excel 文件
            document.close();
            workbook.close();
            excelFile.close();
 
            System.out.println("Excel 轉(zhuǎn) PDF 成功!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Excel 轉(zhuǎn) PDF 失?。? + e.getMessage());
        }
    }
 
    // 將單元格的值轉(zhuǎn)換為字符串
    private static String getCellValueAsString(Cell cell) {
        if (cell == null) {
            return "";
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                } else {
                    return String.valueOf(cell.getNumericCellValue());
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            default:
                return "";
        }
    }
}

3. 代碼詳細(xì)解釋

1.讀取 Excel 文件:

  • FileInputStream excelFile = new FileInputStream("input.xlsx");:創(chuàng)建一個(gè) FileInputStream 對(duì)象,用于讀取 input.xlsx 文件。
  • Workbook workbook = new XSSFWorkbook(excelFile);:使用 XSSFWorkbook 類創(chuàng)建一個(gè) Workbook 對(duì)象,用于表示 Excel 文件。
  • Sheet sheet = workbook.getSheetAt(0);:獲取 Excel 文件的第一個(gè)工作表。

2.創(chuàng)建 PDF 文檔:

  • Document document = new Document();:創(chuàng)建一個(gè) Document 對(duì)象,用于表示 PDF 文檔。
  • PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));:使用 PdfWriter 將 Document 對(duì)象與 FileOutputStream 關(guān)聯(lián)起來,指定生成的 PDF 文件名為 output.pdf。
  • document.open();:打開 PDF 文檔,準(zhǔn)備寫入內(nèi)容。

3.創(chuàng)建 PDF 表格:

  • int columnCount = sheet.getRow(0).getLastCellNum();:獲取 Excel 表格第一行的列數(shù)。
  • PdfPTable pdfTable = new PdfPTable(columnCount);:創(chuàng)建一個(gè) PdfPTable 對(duì)象,用于表示 PDF 表格,列數(shù)與 Excel 表格的列數(shù)相同。

4.遍歷 Excel 表格并填充 PDF 表格:

  • for (Row row : sheet) {... }:遍歷 Excel 表格的每一行。
  • for (Cell cell : row) {... }:遍歷當(dāng)前行的每一個(gè)單元格。
  • String cellValue = getCellValueAsString(cell);:調(diào)用 getCellValueAsString 方法將單元格的值轉(zhuǎn)換為字符串。
  • PdfPCell pdfCell = new PdfPCell(new Phrase(cellValue));:創(chuàng)建一個(gè) PdfPCell 對(duì)象,用于表示 PDF 表格的單元格。
  • pdfTable.addCell(pdfCell);:將 PdfPCell 對(duì)象添加到 PdfPTable 中。

5.將 PDF 表格添加到 PDF 文檔中:

document.add(pdfTable);:將 PdfPTable 對(duì)象添加到 Document 中。

6.關(guān)閉文檔和 Excel 文件:

document.close(); workbook.close(); excelFile.close();:關(guān)閉 PDF 文檔、Excel 文件和輸入流,釋放資源。

4. 注意事項(xiàng)

上述代碼僅處理了 Excel 文件的第一個(gè)工作表,如果需要處理多個(gè)工作表,可以使用 workbook.getNumberOfSheets() 方法獲取工作表的數(shù)量,然后遍歷每個(gè)工作表進(jìn)行處理。

代碼中的 getCellValueAsString 方法用于將不同類型的單元格值轉(zhuǎn)換為字符串,確保在處理不同類型的單元格時(shí)不會(huì)出現(xiàn)異常。

到此這篇關(guān)于使用Java實(shí)現(xiàn)Excel轉(zhuǎn)PDF的示例詳解的文章就介紹到這了,更多相關(guān)Java Excel轉(zhuǎn)PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論