1. 程式人生 > 其它 >java 將html轉為word匯出 (富文字內容匯出word)

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>\n" +
"<p><em><strong>第二行要傾斜</strong></em></p>\n" +
"<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();}
}
}
}