导入依赖
<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();
}
}
}