您的位置:首页 > 编程语言 > Java开发

java使用poi将html导出word,默认打开页面视图

2017-05-19 11:04 706 查看
<htmlxmlns: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">

  

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<htmlxmlns: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"><head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<!--[ifgtemso9]><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:mathFontm:val="CambriaMath"/><m:brkBinm:val="before"/><m:brkBinSubm:val="--"/><m:smallFracm:val="off"/><m:dispDef/><m:lMarginm:val="0"/><m:rMarginm:val="0"/><m:defJcm:val="centerGroup"/><m:wrapIndentm:val="1440"/><m:intLimm:val="subSup"/><m:naryLimm:val="undOvr"/></m:mathPr></w:WordDocument></xml><![endif]-->


  

html页面中添加以上代码默认打开为页面视图而不是web视图

poi将html导出为word

importjava.io.BufferedReader;
importjava.io.ByteArrayInputStream;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.FileReader;
importjava.io.IOException;

importorg.apache.poi.poifs.filesystem.DirectoryEntry;
importorg.apache.poi.poifs.filesystem.DocumentEntry;
importorg.apache.poi.poifs.filesystem.POIFSFileSystem;
importorg.springframework.stereotype.Service;

/**
*@ClassName:HtmlToWord
*@Description:TODO(将html文件导出为word)
*@authorxsw
*@date2016-12-28上午11:41:19
*
*/
@Service
publicclassHtmlToWord{

/**
*
*(srcPathhtml文件fileName保存的doc文件)
*@paramTODO
*@returnvoid返回类型
*@authorxsw
*@2016-12-28上午11:47:27
*/
publicvoidhtmlToWord(StringsrcPath,StringfileName)throwsException{
ByteArrayInputStreambais=null;
FileOutputStreamfos=null;
try{
if(!"".equals(fileName)){
FilefileDir=newFile(fileName);
if(fileDir.exists()){
Stringcontent=readFile(srcPath);
byteb[]=content.getBytes();
bais=newByteArrayInputStream(b);
POIFSFileSystempoifs=newPOIFSFileSystem();
DirectoryEntrydirectory=poifs.getRoot();
DocumentEntrydocumentEntry=directory.createDocument("WordDocument",bais);
fos=newFileOutputStream(fileName);
poifs.writeFilesystem(fos);
bais.close();
fos.close();
}
}

}catch(IOExceptione){
e.printStackTrace();
}finally{
if(fos!=null)fos.close();
if(bais!=null)bais.close();
}
}

/**
*读取html文件到字符串
*@paramfilename
*@return
*@throwsException
*/
publicStringreadFile(Stringfilename)throwsException{
StringBufferbuffer=newStringBuffer("");
BufferedReaderbr=null;
try{
br=newBufferedReader(newFileReader(filename));
buffer=newStringBuffer();
while(br.ready())
buffer.append((char)br.read());
}catch(Exceptione){
e.printStackTrace();
}finally{
if(br!=null)br.close();
}
returnbuffer.toString();
}
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: