将百度富⽂本编辑器(ueditor)中的内容转化为word⽂档格式
业务场景
需求:根据富⽂本中的内容⽣成对应的word⽂档进⾏预览和下载功能。
实现: 采⽤ POIFSFileSystem 类相关实现,能够准确的将⽂字、格式相关内容转换成功,但是对于在线的⽹络图⽚,⽆法离线浏览或打开。因此最后采⽤Spire.doc中的⼯具进⾏转换(免费版本)。
官⽹⽹址:
实现步骤
1. 引⼊依赖
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>2.7.3</version>
</dependency>
2. ⼯具类,导出word
public static void exportWord(HttpServletRequest request, HttpServletResponse response, String content, String fileName){
二级造价师报名的
try{
//新建Document对象
Document document =new Document();
//添加section
Section sec = document.addSection();
ByteArrayOutputStream os =new ByteArrayOutputStream();
//添加段落并写⼊HTML⽂本
sec.addParagraph().appendHTML(content);春季高考招生网
2019年国考行测真题
document.saveToStream(os,FileFormat.Docx);
InputStream input =new ByteArray());
//输出⽂件
request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");//导出word格式2022年浙江省公务员考试
response.addHeader("Content-Disposition","attachment;filename="+
ServletOutputStream ostream = OutputStream();
int len =-1;
byte[]by =new byte[1024];
while((len = ad(by))!=-1){
ostream.write(by,0,len);
}23考研国家线什么时候公布
ostream.close();
input.close();
}catch(Exception e){
e.printStackTrace();
}
巴中党建网}
在线⽹络图⽚,在离线情况下也能够正常预览。