Java 实现Excel转化为PDF

方法一:

  1. 点击链接下载相关jar包
    链接:https://pan.baidu.com/s/1Wz0paGJ4CckkfhKVjpfJwQ
    提取码:6633

  2. 将jar包放到项目中,我是直接创建包放到项目里,也可以放到其他地方,自其定义
    在这里插入图片描述

  3. pom进行引用,将jar包路径完整填写

        <dependency>
            <groupId>aspose-cells.cc</groupId>
            <artifactId>aspose-cells</artifactId>
            <scope>system</scope>
            <version>8.5.2</version>
            <systemPath>jar包绝对路径</systemPath>
        </dependency>
  1. 代码实现
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import java.io.File;
import java.io.FileOutputStream;

public class StyleStudy {

    public static void main(String[] args) {
        try {
            // 读取要转化的Excel文件
            Workbook wb = new Workbook("C:\\Users\\Administrator\\Desktop\\1697506563270.xlsx");
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);

            // 创建PDF输出流
            String pdfFileName = "C:\\Users\\Administrator\\Desktop"+System.currentTimeMillis()+".pdf";
            FileOutputStream fileOutputStream = new FileOutputStream(new File(pdfFileName));
            wb.save(fileOutputStream, pdfSaveOptions);
            System.out.println("PDF转换成功=========");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

方法二(推荐,效率低些):

上代码

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;

public class StockSellTest {
    public static void main(String[] args) {
        InputStream in = null;
        OutputStream out = null;
        XSSFWorkbook workbook = null;
        try {
            //Excel文件数据
            String pdfFileName = "C:\\Users\\Administrator\\Desktop\\01.pdf";
            FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\01.xlsx");
            ZipSecureFile.setMinInflateRatio(-1.0d);// 压缩文件不符合安全限制会抛出异常
            workbook = new XSSFWorkbook(fileInputStream);
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(pdfFileName));
            document.open();
            Sheet sheet = workbook.getSheetAt(0); // Assuming data is on the first sheet
            for (Row row : sheet) {
                for (Cell cell : row) {
                    cell.setCellType(CellType.STRING);// 非字符串类型会转化失败,因此要先将该单元格的类型设置为String
                    String cellValue = cell.getStringCellValue(); // You can customize this based on cell type
                    document.add(new Paragraph(cellValue));
                }
            }
            document.close();
            workbook.close();
            fileInputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
### 回答1: 可以使用Apache POI库,可以使用以下代码将Excel文件换为PDF:XWPFDocument document = new XWPFDocument();FileOutputStream out = new FileOutputStream(new File("result.pdf"));PdfOptions options = PdfOptions.create();PdfConverter.getInstance().convert(document, out, options); ### 回答2: 要实现Excel文件转化PDF并输出,可以使用Java的Apache POI和iText库。 首先,需要在项目中引入Apache POI和iText的相关依赖。在Maven项目中,可以在pom.xml文件中添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> </dependencies> ``` 然后,使用Apache POI读取Excel文件,并将内容输出到PDF中。以下是具体代码示例: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ExcelToPdfConverter { public static void main(String[] args) { try { // 读取Excel文件 FileInputStream input = new FileInputStream("input.xlsx"); Workbook workbook = new XSSFWorkbook(input); Sheet sheet = workbook.getSheetAt(0); // 创建PDF文档 Document document = new Document(PageSize.A4); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); // 遍历Excel表格,将内容写入PDF for (Row row : sheet) { for (Cell cell : row) { String value = cell.getStringCellValue(); // 获取单元格的值 document.add(new com.itextpdf.text.Paragraph(value)); } } // 关闭PDF文档 document.close(); workbook.close(); System.out.println("Excel转化PDF成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码示例中,假设要换的Excel文件名为input.xlsx,将换后的PDF文件输出为output.pdf。使用`FileInputStream`读取Excel文件,`PdfWriter`创建PDF文档,再通过遍历Excel表格将内容写入PDF。 最后,通过运行该Java程序,即可将Excel文件转化PDF并输出到指定位置。 ### 回答3: 要实现Excel文件转化PDF输出,可以使用Java中的POI和iText库来实现。以下是实现的具体代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; 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); // 创建PDF文件 Document document = new Document(PageSize.A4); FileOutputStream pdfFile = new FileOutputStream("output.pdf"); PdfWriter.getInstance(document, pdfFile); document.open(); // 遍历Excel表格中的行和列 for (Row row : sheet) { for (Cell cell : row) { // 创建段落,并将Excel单元格内容添加到段落中 Paragraph paragraph = new Paragraph(cell.toString()); paragraph.setAlignment(Element.ALIGN_LEFT); document.add(paragraph); } } // 关闭PDF文件和Excel文件 document.close(); pdfFile.close(); workbook.close(); excelFile.close(); System.out.println("Excel转化PDF成功!"); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 该代码中,首先使用POI库读取Excel文件,然后使用iText库创建PDF文件。接着使用循环遍历Excel表格中的行和列,将每个单元格的内容添加到PDF文件中。最后关闭PDF文件和Excel文件,并输出成功信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值