使用 com.Lowagie.itext 导出html的内容到word
生活随笔
收集整理的這篇文章主要介紹了
使用 com.Lowagie.itext 导出html的内容到word
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用 com.Lowagie.itext 導出html的內容到word
在導出的時候,使用com.Lowagie.itext 將html字符串的內容導出到word上,html里面常常會有圖片等內容,改如何處理呢?
處理:
1,引入itext:
<dependency><groupId>com.lowagie</groupId><artifactId>itext</artifactId><version>2.0.8</version> </dependency>2,代碼:
public static void main(String[] args) throws Exception {OutputStream out = new FileOutputStream("d://exportFile//img"+System.currentTimeMillis()+".doc");Document document = new Document(PageSize.A4);RtfWriter2.getInstance(document, out);document.open();String content = "<p>圖片導出</p>\n<p> </p>\n<p>啦啦</p>\n<p> </p>\n<p> </p>\n<p><img src=\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn%2F20170105%2Fb01b-fxzkfuk2266724.png&refer=http%3A%2F%2Fn.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623917122&t=0eef227b3c5f25073c174523fd5410c7\" alt=\"\" width=\"640\" height=\"640\" /></p>\n<p> </p>\n<p>發放</p>\n<p> </p>\n<p><img src=\"F://temp//back2.jpg\" alt=\"\" width=\"300\" height=\"108\" /></p>\n<p>第三方</p>\n<p> </p>";System.out.println(content);Paragraph context = new Paragraph();StyleSheet styleSheet = new StyleSheet();List htmlList = HTMLWorker.parseToList(new StringReader(content), styleSheet);for (Object aHtmlList : htmlList) {Element e = (Element) aHtmlList;context.add(e);}document.add(context);document.close();System.out.println("ok"); }? 如果html帶有<table></table>的標簽的話,會報錯,無法直接識別。需要拎出來,再對table里面的內容一一處理。這個比較復雜,后面有時間再整理。
總結:
? 使用 com.Lowagie.itext 可以較好的導出html的內容到word上,處理上也比較方便,不過帶表格就得額外處理了,需要手動拼接。如果是使用其導出html內容到pdf上,可以直接用嗎?
? ?關聯文章:? 《Lowagie 導出html的內容到 pdf》
總結
以上是生活随笔為你收集整理的使用 com.Lowagie.itext 导出html的内容到word的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 时间处理总结(二)oracle
- 下一篇: Hi3518编译器安装