java使用poi导出 将html转换为word

@ 将html转换为word
这几天在一个新项目中做了一个java导出word的功能,但是使用到的内容是富文本编辑框,所以在保存的时候有保存的是html便签,包括一些文字的样式,那样需要在导出的时候按在页面显示的原样式导出,那么就用到了将html便签转换为word,具体的代码如下:

在controller中调用工具类:
xwpfUtil.exportWord(request,response,"<html><body>"+nr+"</body></html>");
在工具类中的实现方法:
public void exportWord(HttpServletRequest request, HttpServletResponse response, String content) throws Exception {
    try {
        byte b[] = content.getBytes("utf-8");  //这里是必须要设置编码的,不然导出中文就会乱码。
        ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
            //生成word
        POIFSFileSystem poifs = new POIFSFileSystem();
        DirectoryEntry directory = poifs.getRoot();
        DocumentEntry documentEntry = directory.createDocument("exportWord", bais);
        //输出文件
        response.setCharacterEncoding("utf-8");
        //设置word格式
        response.setContentType("application/msword");
        response.setHeader("Content-disposition", "attachment;filename=exportWord.docx");
        OutputStream ostream = response.getOutputStream();
        poifs.writeFilesystem(ostream);
        bais.close();
        ostream.close();
    }catch(Exception e){
        //异常处理
        logger.error("文件导出错误", e);
    }
}
pom文件中关于poi的依赖
<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>
            poi</artifactId>
        <version>3.10-beta2</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.10-beta2</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.10-beta2</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>3.0.2-FINAL</version>
    </dependency>
  • 1
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
Java使用HTML导出Word并设置Word行间距的方法如下: 1. 首先,你需要使用Apache POI库来操作Word文档。确保你已经在你的项目中添加了POI的依赖。 2. 创建一个空的Word文档对象,并获取其段落对象。 3. 使用HTML标签和样式来设置行间距。在段落对象中添加HTML内容,并使用`<p>`标签来表示段落,使用`<br>`标签来表示换行,使用`style`属性来设置行间距。 4. 将HTML内容转换为字节数组,并将其写入Word文档。 下面是一个示例代码: ```java import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.*; public class WordExporter { public static void main(String[] args) { try { // 创建一个空的Word文档对象 XWPFDocument document = new XWPFDocument(); // 获取段落对象 XWPFParagraph paragraph = document.createParagraph(); // 使用HTML标签和样式来设置行间距 String htmlContent = "<p style='line-height: 1.5;'>This is a sample text with line spacing.</p>"; // 将HTML内容转换为字节数组 byte[] byteArray = htmlContent.getBytes(); // 将字节数组写入Word文档 paragraph.getDocument().getBody().addNewP().addNewR().addNewT().setStringValue(new String(byteArray)); // 保存Word文档 FileOutputStream out = new FileOutputStream("output.docx"); document.write(out); out.close(); System.out.println("Word文档导出成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,上述代码中的`htmlContent`变量是一个包含HTML标签和样式的字符串,你可以根据需要自定义行间距和其他样式。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值