阅读背景:

Java poi导出带html标签内容的word文档_yilia_jia的博客

来源:互联网 
public static void exportWord(HttpServletResponse response, String content, String fileName) throws Exception {
        try {
            // 设置导出的word为页面视图
            // 不设置直接使用content word打开为web视图
            String str = " <!--[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]-->";
            // 其中content为富文本编辑器生成的内容
            String h = " <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='https://schemas.microsoft.com/office/2004/12/omml'xmlns='https://www.w3.org/TR/REC-html40'  ";
            content =h+"<head>"+"<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"+str+"</head><body>"+content+"</body> </html>";

            // 设置编码
            byte bytes[] = content.getBytes("utf-8");
            //将字节数组包装到流中
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

            // 设置word格式
            POIFSFileSystem poifsFileSystem = new POIFSFileSystem();
            DirectoryEntry directory = poifsFileSystem.getRoot();
            DocumentEntry documentEntry = directory.createDocument("WordDocument", byteArrayInputStream);

            // 输出文件
            // 导出word格式
            // docx -- application/vnd.openxmlformats-officedocument.wordprocessingml.document
            // doc -- application/msword
            response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            response.addHeader("Content-Disposition", "attachment;filename=" +
                    new String( (fileName + ".docx").getBytes(),
                            "iso-8859-1"));

            OutputStream outputStream = response.getOutputStream();
            poifsFileSystem.writeFilesystem(outputStream);
            byteArrayInputStream.close();
            outputStream.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }public static void exportWord(HttpServletRespon



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: