Java把html转成word

一、常用的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;
}




  • 8
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
### 回答1: Java可以使用Apache POI库将HTML文件换为Word文档。以下是一个简单的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.HtmlDocumentFacade; import org.apache.poi.hwpf.converter.WordToHtmlConverter; import org.apache.poi.hwpf.usermodel.Range; public class HtmlToWordConverter { public static void main(String[] args) throws Exception { // 读取HTML文件 String htmlFilePath = "input.html"; FileInputStream fis = new FileInputStream(htmlFilePath); // 创建Word文档对象 HWPFDocument wordDoc = new HWPFDocument(); Range range = wordDoc.getRange(); // 将HTML内容换为Word格式 WordToHtmlConverter converter = new WordToHtmlConverter(wordDoc); HtmlDocumentFacade facade = new HtmlDocumentFacade(); converter.processInputFile(fis, facade); converter.processDocument(facade); // 将Word文档保存到文件 String outputFilePath = "output.doc"; FileOutputStream fos = new FileOutputStream(new File(outputFilePath)); wordDoc.write(fos); fos.close(); wordDoc.close(); System.out.println("HTML换为Word成功!"); } } ``` 需要注意的是,此代码使用的是Apache POI 3.10版本。如果使用的是较新的版本,可能会有所差异。同时,换过程中可能会出现一些格式上的问题,需要根据具体情况进行调整。 ### 回答2: 在Java中将HTML换为Word可以使用Apache POI库和Jsoup库进行操作。 首先,使用Jsoup库将HTML文件读取为一个Document对象,然后使用该对象提供的方法从HTML中提取各种标签和内容。可以使用该库的选择器功能选择特定的HTML元素。 接下来,使用Apache POI库来创建一个新的Word文档对象,并添加内容。可以使用Apache POI库的丰富功能来创建表格、插入图片、设置样式等。根据需要,将Jsoup读取到的HTML内容添加到Word文档中。 需要注意的是,HTMLWord是两种不同的文档格式,并且有各自的标记语言和布局方式。因此,在将HTML换为Word时,可能需要手动处理一些特定的样式或布局问题。 最后,将生成的Word文档保存到指定的路径或输出流中,完成将HTML换为Word的过程。 总体而言,使用Jsoup库解析HTML,然后使用Apache POI库创建Word文档,并将HTML内容添加到Word文档中,最后保存换后的Word文档。这是一种在Java中将HTML换为Word的常见方法。 ### 回答3: 在Java中将HTML换为Word可以通过使用Apache POI库来实现。Apache POI是一个用于操作Microsoft Office文件的Java库。 首先,确保已经在项目中引入了Apache POI的相关依赖。 然后,我们需要编写Java代码来进行HTML换为Word的操作。下面是一个简单的示例: ```java import org.apache.poi.xwpf.usermodel.*; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class HTMLToWordConverter { public static void convertHTMLToWord(String htmlFilePath, String wordFilePath) throws IOException { // 加载HTML文件 File htmlFile = new File(htmlFilePath); Document doc = Jsoup.parse(htmlFile, "UTF-8"); // 创建Word文档对象 XWPFDocument document = new XWPFDocument(); // 解析HTML中的内容 Elements elements = doc.body().children(); for (Element element : elements) { // 将HTML中的段落换为Word的段落 if (element.tagName().equals("p")) { XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(element.text()); } // 其他元素换,如表格、图片等 // ... } // 保存Word文档 FileOutputStream out = new FileOutputStream(wordFilePath); document.write(out); out.close(); } public static void main(String[] args) { try { convertHTMLToWord("input.html", "output.docx"); System.out.println("HTML换为Word成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在以上代码中,我们使用了Jsoup库来解析HTML内容,并使用Apache POI的XWPFDocument来创建Word文档对象。通过遍历HTML中的元素,我们可以根据需要进行相应的换。最后,将换后的Word文档保存到指定路径。 以上是一个简单的HTML换为Word的示例,根据实际需求可能还需要进行更多的处理和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值