Javaexcel转pdf

maven依赖:

<dependency>
    <groupId>net.sf.jacob-project</groupId>
    <artifactId>jacob</artifactId>
    <version>1.14.3</version>
</dependency>

测试代码:

import java.io.File;

        import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordToPdf
{
    public static void main(String[]args){
        String s="d:\\马小琳.docx";
        String ss="d:\\琳琳.pdf";
        wordToPDF(s,ss);
    }
    public static void wordToPDF(String sfileName, String toFileName) {
        System.out.println("启动 Word...");
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.call(docs, "Open", sfileName).toDispatch();
            System.out.println("打开文档..." + sfileName);
            System.out.println("转换文档到 PDF..." + toFileName);
            File tofile = new File(toFileName);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "SaveAs", toFileName, // FileName
                    17);
            long end = System.currentTimeMillis();
            System.out.println("转换完成..用时:" + (end - start) + "ms.");

        } catch (Exception e) {
            System.out.println("========Error:文档转换失败:" + e.getMessage());
        } finally {
            Dispatch.call(doc, "Close", false);
            System.out.println("关闭文档");
            if (app != null) {
                app.invoke("Quit", new Variant[]{});
            }
        }
        // winword.exe进程关闭
        ComThread.Release();
    }
}

最后需要下载jacob-1.14.3-x64.dll文件,放在jdk的bin目录下。

Java中,将Excel文件换成PDF通常需要使用一些库来处理电子表格和生成PDF文档。Apache POI是一个常用的Java库,用于操作Microsoft Office格式(包括Excel),而iText或Flying Saucer等库则可以用来创建PDF文件。 以下是一个简单的步骤示例: 1. 引入所需的库依赖: - poi:`<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>最新版本</version> </dependency>` - iText或itextpdf:`<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>最新版本</version> </dependency>` 2. 使用POI读取Excel数据: ```java FileInputStream fis = new FileInputStream("input.xlsx"); HSSFWorkbook workbook = new HSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表 ``` 3. 将数据换为iText元素(如果使用iText): ```java PdfDocument pdfDoc = new PdfDocument(); for (Row row : sheet) { List<Cell> cells = row.Cells; for (Cell cell : cells) { String cellValue = cell.getStringCellValue(); // 添加到PDF的相应位置 } } ``` 4. 创建并写入PDF: ```java PdfPTable table = new PdfPTable(numberOfColumns); for (Row row : sheet) { PdfPCell cell = createTableCell(row); table.addCell(cell); } pdfDoc.add(table); try (OutputStream os = new FileOutputStream("output.pdf")) { pdfDoc.write(os); } ``` 5. 关闭资源: ```java workbook.close(); pdfDoc.close(); fis.close(); ``` 注意:这个过程可能会比较复杂,特别是处理表格样式和合并单元格等问题,实际操作时可能需要根据具体需求调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Miki_souls

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值