excel转pdf代码

package cn.digitaltest.util;
import java.io.*; 

import com.aspose.cells.*;      //引入aspose-cells-8.5.2.jar包

public class Pdf {
    public static boolean getLicense() {       

        boolean result = false;       

        try {           

        InputStream is = Test.class.getClassLoader().getResourceAsStream("xlsxlicense.xml"); //  license.xml应放在..\WebRoot\WEB-INF\classes路径下           

        License aposeLic = new License();           

        aposeLic.setLicense(is);           

        result = true;       

        }

        catch (Exception e) {                          

        e.printStackTrace();       

        }       

        return result;   

        }  

        public static void excel2pdf(String Address) {               

        if (!getLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生           

        return;       

        }       

        try {           

        File pdfFile = new File("C:/inetpub/wwwroot/web/file/pdf1.pdf");// 输出路径           

        Workbook wb = new Workbook(Address);// 原始excel路径                        FileOutputStream fileOS = new FileOutputStream(pdfFile);           

        wb.save(fileOS, SaveFormat.PDF);             

        fileOS.close();                 

        }

        catch (Exception e) {           

        e.printStackTrace();       

        }   

        }
}
 

要实现 Spring Boot 中的 Excel PDF,你可以使用 Apache POI 和 iText 这两个库。以下是一个简单的示例代码: 1. 添加 Maven 依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependencies> <!-- Apache POI --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.0.0</version> </dependency> <!-- iText --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> </dependencies> ``` 2. 编写代码 ```java import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfWriter; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class ExcelToPdfConverter { public void convert(InputStream excelInputStream, OutputStream pdfOutputStream) throws Exception { // 创建 Excel Workbook Workbook workbook = new XSSFWorkbook(excelInputStream); // 创建 PDF Document Document document = new Document(PageSize.LETTER); PdfWriter.getInstance(document, pdfOutputStream); document.open(); // 将每个 Sheet 换为 PDF for (int i = 0; i < workbook.getNumberOfSheets(); i++) { SheetToPdfConverter sheetToPdfConverter = new SheetToPdfConverter(); sheetToPdfConverter.convert(workbook.getSheetAt(i), document); } document.close(); workbook.close(); } private static class SheetToPdfConverter { public void convert(Sheet sheet, Document document) throws Exception { // 获取行数和列数 int rowCount = sheet.getPhysicalNumberOfRows(); int columnCount = sheet.getRow(0).getPhysicalNumberOfCells(); // 创建 PDF 表格 PdfPTable table = new PdfPTable(columnCount); // 填充 PDF 表格 for (int i = 0; i < rowCount; i++) { Row row = sheet.getRow(i); for (int j = 0; j < columnCount; j++) { Cell cell = row.getCell(j); if (cell != null) { table.addCell(cell.toString()); } else { table.addCell(""); } } } // 添加 PDF 表格到 PDF Document document.add(table); } } } ``` 3. 调用换方法 ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; import java.io.OutputStream; @Controller public class ExcelToPdfController { @PostMapping("/convert") public void convert(@RequestParam("file") MultipartFile excelFile, HttpServletResponse response) throws Exception { InputStream excelInputStream = excelFile.getInputStream(); OutputStream pdfOutputStream = response.getOutputStream(); response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "attachment;filename=output.pdf"); ExcelToPdfConverter converter = new ExcelToPdfConverter(); converter.convert(excelInputStream, pdfOutputStream); } } ``` 这个示例代码Excel 文件换为 PDF 并将其发送给客户端下载。你可以根据自己的需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值