springboot富⽂本导出word、pdf 1.导出word
添加依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
实现层代码
private void downloadWord(HttpServletResponse response, Resource resource) {
String title = Title();
String text = Content();
try {
//word内容
String content="<html><body>" +
"<p style=\"text-align: center;\"><span style=\"font-family: ⿊体, SimHei; font-size: 24px;\">"
+ title + "</span></p>" + text + "</body></html>";
byte b[] = Bytes("GBK"); //这⾥是必须要设置编码的,不然导出中⽂就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
/*
* 关键地⽅
* ⽣成word格式 */
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = Root();
DocumentEntry documentEntry = ateDocument("WordDocument", bais);
//输出⽂件
// request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");//导出word格式
response.addHeader("Content-Disposition", "attachment;filename=" +
new Bytes("GB2312"),"iso8859-1") + ".doc");
ServletOutputStream ostream = OutputStream();
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
poifs.close();
}catch(Exception e){
e.printStackTrace();
}
}
2.导出pdf
添加依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>l</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.8</version>
</dependency>
压缩并导出实现代码
private void downloadPdf(HttpServletResponse response, Resource resource) {
String content = Content();
content = MyUtil.handleConvertPdfContent(content);
String rootDir = Property("user.dir")+"/";
String pdfDir = rootDir+"temp/pdf/";
String pdfFileName = Title()+".pdf";
try {
String path = pdfDir + pdfFileName;
File pdfFileDir = new File(pdfDir);
if (!ists()) {
pdfFileDir.mkdirs();
}
FileOutputStream pdfOut = new FileOutputStream(path);
// 第⼀步创建⽂档实例⽂章pdf导出+压缩包下载
Document document = new Document();
// 第⼆步获取PdfWriter实例写⼊⽂件到⽬录 pdfDir + responseFileName
PdfWriter writer = Instance(document, pdfOut);
// 第三步打开⽂档
乱码文字生成document.open();
//获取 XMLWorkerHelper实例
XMLWorkerHelper work = Instance();
/
/解析html⽂件,创建pdf⽂档
work.parseXHtml(writer, document, new Bytes()));
// 第五部操作完成后必须执⾏⽂档关闭操作。
document.close();
writer.close();
pdfOut.close();
// 压缩pdf⽂件
String zipDir = rootDir + "temp/zip/";
String zipPath = zipDir + Title() + ".zip";
File zipFile = new File(zipPath);
File pdfFile1 = new File(path);
ZipUtil.zip(zipFile, false, pdfFile1);
// 将zip file写⼊response流
//设置response内容
String zipFileName = Title() + ".zip";
// 这⾥中⽂名字导致了下载报错暂不解决
String responseFileName = new String("download.zip".getBytes(), "utf-8");
ServletOutputStream responseOutputStream = OutputStream();
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + responseFileName);
InputStream in = new FileInputStream(zipPath);
int len = 0;
InputStream in = new FileInputStream(zipPath);
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) {
responseOutputStream.write(buffer, 0, len); }
in.close();
responseOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
FileUtil.delAllFile(rootDir + "/temp");
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论