小工具知识网

当前位置: 首页 > 行业知识 > java实现html转word

行业知识

java实现html转word

2026-12-03 09:50:43 来源:互联网转载

使���poi实现html字符串生成word文档,和导出word文档,

优点:对于html不需要特殊处理,直接带完整样式生成到word文档中

引入依赖:

                                    org.apache.poi                    poi                                                    org.apache.poi                    poi-ooxml                                                    org.apache.poi                    poi-ooxml-schemas                

1. 将html字符串生成doc文件

代码如下:

// 组装html(替换为自己的html)String html = hostPatrolMessageContentToHtml2(dataVo.getReportDetail(), dataVo.getAlarmMap(), 0);            if (StringUtils.isNotEmpty(html)) {                // 生成临时文件(doc)                try {                    // 生成doc格式的word文档,需要手动改为docx                    byte by[] = html.getBytes("UTF-8");                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(by);                    POIFSFileSystem poifsFileSystem = new POIFSFileSystem();                    DirectoryEntry directoryEntry = poifsFileSystem.getRoot();                    directoryEntry.createDocument("WordDocument", byteArrayInputStream);                    // 临时文件夹                    String sqlFilePath = Global.getProfile() + File.separator + "patrolReport" + File.separator + "temp_" + StringUtils.getUUId();                    directory = new File(sqlFilePath);                    if (!directory.exists()) {                        directory.mkdirs();                    }                    // 文件路径                    String fileUrl = sqlFilePath + File.separator + "主机巡检报告告警内容详情.doc";                    // 保存doc文档                    FileOutputStream outputStream = new FileOutputStream(fileUrl);                    poifsFileSystem.writeFilesystem(outputStream);                    byteArrayInputStream.close();                    outputStream.close();                    file = new File(fileUrl);                               } catch (Exception e) {                    e.printStackTrace();                }            }

例如组装的html为:

 

巡检报告告警详情:

  • 主机(zyf主机)发生严重警告
    • 服务器探针(zyf主机)发生严重警告
      • 指标名称(内存使用率),发生严重警告,阈值:> 25 %,采集值:77.84 %;
      • 指标名称(CPU占用百分比),发生严重警告,阈值:> 30 %,采集值:65.64 %;
    • 中间件探针(tomcat测试)采集失败,返回信息:Failed to retrieve RMIServer stub: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: 122.112.174.151; nested exception is: java.net.ConnectException: Connection timed out: connect]
    • 数据库探针(98MySQL库)采集失败,返回信息:Failed to initialize pool: Access denied for user 'root'@'10.2.6.159' (using password: YES)
    • 数据库探针(本地Oracle测试库19c)采集失败,返回信息:Failed to initialize pool: ORA-28001: 口令已经失效
    • 数据库探针(MySQL测试数据库)采集失败,返回信息:Failed to initialize pool: Access denied for user 'root'@'10.2.6.159' (using password: YES)

上一篇:怎么去服务器拷贝东西?

下一篇:新服务器怎么搭环境?