zoukankan      html  css  js  c++  java
  • 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 = "<p><strong>第一行要加粗</strong></p> " +
    "<p><em><strong>第二行要倾斜</strong></em></p> " +
    "<p style="text-align: center;"><em><strong>第三行要居中</strong></em></p>";
    StringBuffer sbf = new StringBuffer();
    sbf.append("<html " +
    "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("<head>" +
    "<!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val="Cambria Math"/><m:brkBin m:val="before"/><m:brkBinSub m:val="--"/><m:smallFrac m:val="off"/><m:dispDef/><m:lMargin m:val="0"/> <m:rMargin m:val="0"/><m:defJc m:val="centerGroup"/><m:wrapIndent m:val="1440"/><m:intLim m:val="subSup"/><m:naryLim m:val="undOvr"/></m:mathPr></w:WordDocument></xml><![endif]-->" +
    "</head>");//将版式从web版式改成页面试图
    sbf.append("<body>");//缺失的首标签
    sbf.append(s);//富文本内容
    sbf.append("</body></html>");//缺失的尾标签

    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();}
    }
    }
    }
  • 相关阅读:
    【LeetCode OJ】Remove Element
    【LeetCode OJ】Remove Duplicates from Sorted Array
    【LeetCode OJ】Swap Nodes in Pairs
    【LeetCode OJ】Merge Two Sorted Lists
    【LeetCode OJ】Remove Nth Node From End of List
    【LeetCode OJ】Two Sum
    【LeetCode OJ】Majority Element
    最长公共子序列问题
    php fopen与file_get_contents的区别
    PHP 技巧集合
  • 原文地址:https://www.cnblogs.com/xing-nb/p/15233043.html
Copyright © 2011-2022 走看看