使用Java實(shí)現(xiàn)Excel轉(zhuǎn)PDF的示例詳解
在實(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)文章
Java網(wǎng)絡(luò)編程基礎(chǔ)教程之Socket入門實(shí)例
這篇文章主要介紹了Java網(wǎng)絡(luò)編程基礎(chǔ)教程之Socket入門實(shí)例,本文講解了創(chuàng)建Socket、Socket發(fā)送數(shù)據(jù)、Socket讀取數(shù)據(jù)、關(guān)閉Socket等內(nèi)容,都是最基礎(chǔ)的知識(shí)點(diǎn),需要的朋友可以參考下2014-09-09用SpringBoot+Vue+uniapp小程序?qū)崿F(xiàn)在線房屋裝修管理系統(tǒng)
這篇文章主要介紹了用SpringBoot+Vue+uniapp實(shí)現(xiàn)在線房屋裝修管理系統(tǒng),針對(duì)裝修樣板信息管理混亂,出錯(cuò)率高,信息安全性差,勞動(dòng)強(qiáng)度大,費(fèi)時(shí)費(fèi)力等問題開發(fā)了這套系統(tǒng),需要的朋友可以參考下2023-03-03詳解Java8新特性之interface中的static方法和default方法
這篇文章主要介紹了Java8新特性之interface中的static方法和default方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08Java實(shí)現(xiàn)自定義Excel數(shù)據(jù)排序的方法詳解
通常,我們可以在Excel中對(duì)指定列數(shù)據(jù)執(zhí)行升序或者降序排序,在需要自定義排序情況下,我們也可以自行根據(jù)排序需要編輯數(shù)據(jù)排列順序。本文將通過Java應(yīng)用程序來實(shí)現(xiàn)如何自定義排序,需要的可以參考一下2022-09-09Java?SimpleDateFormat與System類使用示例詳解
這篇文章主要介紹了Java?SimpleDateFormat與System類使用示例,對(duì)于SimpleDateFormat類,是一個(gè)用來區(qū)分區(qū)域設(shè)置的方式進(jìn)行日期的是指,以及對(duì)日期進(jìn)行處理分析的一個(gè)實(shí)現(xiàn)類2022-11-11@Transactional注解異常報(bào)錯(cuò)之多數(shù)據(jù)源詳解
這篇文章主要介紹了@Transactional注解異常報(bào)錯(cuò)之多數(shù)據(jù)源詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01java發(fā)送HttpClient請(qǐng)求及接收請(qǐng)求結(jié)果過程的簡單實(shí)例
下面小編就為大家?guī)硪黄猨ava發(fā)送HttpClient請(qǐng)求及接收請(qǐng)求結(jié)果過程的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11idea中使用Inputstream流導(dǎo)致中文亂碼解決方法
很多朋友遇到一個(gè)措手不及的問題當(dāng)idea中使用Inputstream流導(dǎo)致中文亂碼及Java FileInputStream讀中文亂碼問題,針對(duì)這兩個(gè)問題很多朋友不知道該如何解決,下面小編把解決方案分享給大家供大家參考2021-05-05