excle和word转pdf

导入依赖

		<dependency>
			<groupId>com.luhuiguo</groupId>
			<artifactId>aspose-words</artifactId>
			<version>22.10</version>
		</dependency>
		<dependency>
			<groupId>com.luhuiguo</groupId>
			<artifactId>aspose-cells</artifactId>
			<version>22.10</version>
		</dependency>

导出工具类

根据需求自己改改

package com.wanwei.dcloud2.provider.dataReport.utils;

import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @author: yxfeng
 * @createTime: 2022/11/16 11:36
 */
public class ExportUtil {
    private static final Configuration CONFIGURATION = new Configuration(Configuration.VERSION_2_3_28);
    private static final String CHARSET = "UTF-8";

    static {
        CONFIGURATION.setDefaultEncoding(CHARSET);
        CONFIGURATION.setClassForTemplateLoading(ExportUtil.class, "/templates");
    }
    /**
     * 加载并充模板
     * @author yxfeng
     * @dateTime 2022/12/2 17:15
     */
    public static InputStream loadTemplate(Map<String, Object> dataMap,  String templateName) throws Exception {
        Template template = CONFIGURATION.getTemplate(templateName);
        StringWriter out1 = new StringWriter();
        Writer out = new BufferedWriter(out1, 10240);
        template.process(dataMap, out);
        out.close();
        out1.close();
        return new ByteArrayResource(out1.toString().getBytes(StandardCharsets.UTF_8)).getInputStream();
    }
    /**
     * excle转pdf
     * @author yxfeng
     * @dateTime 2022/12/2 17:15
     */
    public static void excel2pdf(HttpServletResponse response, InputStream in) {
        try (ServletOutputStream out = response.getOutputStream()) {
            response.setContentType("application/pdf");
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attchment; filename=name.pdf");
            Workbook wb = new Workbook(in);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);
            wb.save(out, pdfSaveOptions);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * word转pdf
     * @author yxfeng
     * @dateTime 2022/12/2 17:15
     */
    public static void word2Pdf(HttpServletResponse response, InputStream in){
        try (ServletOutputStream out = response.getOutputStream()) {
            response.setContentType("application/pdf");
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attchment; filename=name.pdf");
            Document doc = new Document(in);
            doc.save(out, com.aspose.words.SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 导出
     * @author yxfeng
     * @dateTime 2022/12/2 17:15
     */
    public static void export(HttpServletResponse response, InputStream in) throws IOException {
        response.setCharacterEncoding(CHARSET);
        response.setContentType("application/xml");
        ServletOutputStream out = response.getOutputStream();
        // 缓冲区
        byte[] buffer = new byte[512];
        int index;
        // 通过循环将读入的Word文件的内容输出到浏览器中
        while ((index = in.read(buffer)) != -1) {
            out.write(buffer, 0, index);
        }
        in.close();
        if (out != null) {
            out.close();
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中有多种库可以用来实现Word文档PDF的功能,其中比较常用的是iText和Apache POI。下面我会分别介绍一下这两个库的使用方法。 1. iText: iText是一个开源的Java库,可以用来创建和操作PDF文档。它提供了丰富的API,可以实现对PDF文档的各种操作,包括创建、编辑、合并、拆分、加密等。要将Word文档换为PDF,可以使用iText的功能来读取Word文档内容,并将其换为PDF格式。 首先,你需要在项目中引入iText的依赖。可以在Maven项目中的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> ``` 然后,你可以使用以下代码将Word文档换为PDF: ```java import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import java.io.FileInputStream; import java.io.FileOutputStream; public class WordToPdfConverter { public static void main(String[] args) { try { // 读取Word文档 FileInputStream fis = new FileInputStream("input.docx"); XWPFDocument document = new XWPFDocument(fis); // 创建PDF文档 Document pdfDocument = new Document(); PdfWriter.getInstance(pdfDocument, new FileOutputStream("output.pdf")); pdfDocument.open(); // 逐段读取Word文档内容,并写入PDF文档 for (XWPFParagraph paragraph : document.getParagraphs()) { String text = paragraph.getText(); pdfDocument.add(new Paragraph(text)); } // 关闭文档 pdfDocument.close(); document.close(); System.out.println("WordPDF成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码中,我们首先使用Apache POI库的XWPFDocument类来读取Word文档的内容,然后使用iText库的Document类和PdfWriter类来创建和写入PDF文档。最后,我们关闭文档并输出成功信息。 2. Apache POI: Apache POI是一个用于读写Microsoft Office格式文件的Java库。它提供了对Word、Excel和PowerPoint等文件格式的支持。要将Word文档换为PDF,可以使用Apache POI的功能来读取Word文档内容,并将其写入PDF格式。 首先,你需要在项目中引入Apache POI的依赖。可以在Maven项目中的pom.xml文件中添加以下依赖: ```xml <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> ``` 然后,你可以使用以下代码将Word文档换为PDF: ```java import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.FileInputStream; import java.io.FileOutputStream; public class WordToPdfConverter { public static void main(String[] args) { try { // 读取Word文档 FileInputStream fis = new FileInputStream("input.docx"); XWPFDocument document = new XWPFDocument(fis); // 创建PDF选项 PdfOptions options = PdfOptions.create(); // 将Word文档换为PDF FileOutputStream fos = new FileOutputStream("output.pdf"); PdfConverter.getInstance().convert(document, fos, options); // 关闭流 fos.close(); document.close(); System.out.println("WordPDF成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码中,我们使用Apache POI库的XWPFDocument类来读取Word文档的内容,然后使用Apache POI提供的PdfConverter类将其换为PDF格式。最后,我们关闭流并输出成功信息。 希望以上代码可以帮助到你。如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值