poi XWPFDocument文档转换成io流

使用Poi生成DOC文档之后并没有结束,JAVA中涉及到文件的传输就需要吧文档转换为IO流.直接上代码

XWPFDocument document = new XWPFDocument ();//新建文档 后面NEW方法可以忽略
ByteArrayOutputStream baos = new ByteArrayOutputStream();//二进制OutputStream
document.write(baos);//文档写入流 
ByteArrayInputStream in = new ByteArrayInputStream(baos.toByteArray());//OutputStream写入InputStream二进制流

搞定收工.

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
可以使用Apache POI来将HTML转换成Word文档。下面是一个简单的示例代码: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.HtmlToWrdConverter; import org.apache.poi.hwpf.usermodel.Range; public class HtmlToWordConverter { public static void main(String[] args) { try { // 读取HTML文件 FileInputStream in = new FileInputStream("input.html"); // 创建Word文档对象 HWPFDocument document = new HWPFDocument(); // 创建Range对象 Range range = document.getRange(); // 将HTML转换成Word HtmlToWrdConverter converter = new HtmlToWrdConverter(document); converter.processDocument(in); // 输出Word文档 FileOutputStream out = new FileOutputStream("output.doc"); document.write(out); // 关闭流 out.close(); in.close(); System.out.println("HTMLWord成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先读取了一个HTML文件,并创建了一个空的Word文档对象。然后,我们通过调用HtmlToWrdConverter类的processDocument方法将HTML文件转换成Word文档。最后,我们将Word文档写入到输出流中,然后关闭流。 需要注意的是,上述示例代码中使用的是HWPFDocument类,这个类只能处理.doc格式的Word文档,如果要处理.docx格式的Word文档,需要使用XWPFDocument类。另外,Apache POI还提供了另外一种将HTML转换成Word的方式,即使用XHTML换器。如果你对这种方式感兴趣,可以参考Apache POI的官方文档
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值