Java把html转成word

java 专栏收录该内容
2 篇文章 0 订阅

一、常用的html转word

  1、工具:word2html、pdf转成word转换器等

  2、Java代码:jacob和poi

</pre><p></p><p></p><p>二、jacob把html转换成word</p><p><span style="margin:0px; padding:0px; color:rgb(57,57,57); font-family:verdana,'ms song',Arial,Helvetica,sans-serif; font-size:14px; line-height:21px"><span style="margin:0px; padding:0px"><span style="margin:0px; padding:0px">Jacob只能用于windows系统,如果你的系统不是windows,建议使用Openoffice.org,这个是跨平台的</span></span></span></p><p></p><p><pre name="code" class="java">/*************  
* JACOB方式  
* notes:需要将jacob.dll拷贝到windows/system32和classpath路径下  
* @param html html静态页面路径  
* @param wordFile 要生成的word文档路径  
*/ 
public static void htmlToWord(String html, String wordFile) {     
	ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word        
	try {            
		app.setProperty("Visible", new Variant(false));            
		Dispatch wordDoc = app.getProperty("Documents").toDispatch();      
		wordDoc = Dispatch.invoke(wordDoc, "Add", Dispatch.Method, new Object[0], new int[1]).toDispatch();  
		Dispatch.invoke(app.getProperty("Selection").toDispatch(), "InsertFile", Dispatch.Method, new Object[] { html, "", new Variant(false), new Variant(false), new Variant(false) }, new int[3]);     
		Dispatch.invoke(wordDoc, "SaveAs", Dispatch.Method, new Object[] {wordFile, new Variant(1)}, new int[1]);      
		Dispatch.call(wordDoc, "Close", new Variant(false));       
	} catch (Exception e) {     
		e.printStackTrace();     
	} finally {          
		app.invoke("Quit", new Variant[] {});    
	} 
}



 

三、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.WordDocument
	fs.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;
}




  • 7
    点赞
  • 11
    评论
  • 15
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值