java 将html转为word导出 (富文本内容导出word)


业务:

将富文本内容取出生成本地word文件

参考百度的方法

word本身是可以识别html标签,所以通过poi写入html内容即可

import com.util.WordUtil;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SysAnnouncementController {

@PostMapping(value = "/exportAccidentExampleWord")
public void exportAccidentExampleWord(HttpServletRequest request, HttpServletResponse response) throws Exception {
String s = "

第一行要加粗

\n" +
"

第二行要倾斜

\n" +
"

第三行要居中

";
StringBuffer sbf = new StringBuffer();
sbf.append(" "xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\" xmlns=\"http://www.w3.org/TR/REC-html40\"" + //将版式从web版式改成页面试图
">");//缺失的首标签
sbf.append("" +
"" +
"");//将版式从web版式改成页面试图
sbf.append("");//缺失的首标签
sbf.append(s);//富文本内容
sbf.append("");//缺失的尾标签

try{
WordUtil.exportWord(request,response,sbf.toString(),"wordName");
}catch (Exception e){
System.out.println(e.getMessage());
}
}
}

工具类
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.OutputStream;

public class WordUtil {
public static void exportWord(HttpServletRequest request, HttpServletResponse response, String content, String fileName) throws Exception {

byte[] b = content.getBytes("GBK"); //这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); //该步骤不可省略,否则会出现乱码。
//输出文件
request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");//导出word格式
response.addHeader("Content-Disposition", "attachment;filename=" +
new String(fileName.getBytes("GB2312"),"iso8859-1") + ".doc");
ServletOutputStream ostream = response.getOutputStream();
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
poifs.close();
}

public static void downloadWord( byte[] b, OutputStream out)
throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
try {

POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
poifs.writeFilesystem(out);
bais.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}finally {
if(bais!=null){bais.close();}
if(out!=null) {out.flush();out.close();}
}
}
}