java通过POI技术将HTML文件转成Word
生活随笔
收集整理的這篇文章主要介紹了
java通过POI技术将HTML文件转成Word
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public void htmlToWord2() throws Exception {InputStream bodyIs = new FileInputStream("f:\\1.html");InputStream cssIs = new FileInputStream("f:\\1.css");String body = this.getContent(bodyIs);String css = this.getContent(cssIs);//拼一個標準的HTML格式文檔String content = "<html><head><style>" + css + "</style></head><body>" + body + "</body></html>";InputStream is = new ByteArrayInputStream(content.getBytes("GBK"));OutputStream os = new FileOutputStream("f:\\1.doc");this.inputStreamToWord(is, os);
}/*** 把is寫入到對應的word輸出流os中* 不考慮異常的捕獲,直接拋出* @param is* @param os* @throws IOException*/
private void inputStreamToWord(InputStream is, OutputStream os) throws IOException {POIFSFileSystem fs = new POIFSFileSystem();//對應于org.apache.poi.hdf.extractor.WordDocumentfs.createDocument(is, "WordDocument");fs.writeFilesystem(os);os.close();is.close();
}/*** 把輸入流里面的內容以UTF-8編碼當文本取出。* 不考慮異常,直接拋出* @param ises* @return* @throws IOException*/
private String getContent(InputStream... ises) throws IOException {if (ises != null) {StringBuilder result = new StringBuilder();BufferedReader br;String line;for (InputStream is : ises) {br = new BufferedReader(new InputStreamReader(is, "UTF-8"));while ((line=br.readLine()) != null) {result.append(line);}}return result.toString();}return null;
}
總結
以上是生活随笔為你收集整理的java通过POI技术将HTML文件转成Word的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2 字符串求交集_PostGIS教程十八
- 下一篇: mysql 备份 php_PHP备份/还