柚子快報(bào)邀請(qǐng)碼778899分享:java實(shí)現(xiàn)html轉(zhuǎn)word
柚子快報(bào)邀請(qǐng)碼778899分享:java實(shí)現(xiàn)html轉(zhuǎn)word
使用poi實(shí)現(xiàn)html字符串生成word文檔,和導(dǎo)出word文檔,
優(yōu)點(diǎn):對(duì)于html不需要特殊處理,直接帶完整樣式生成到word文檔中
引入依賴:
1. 將html字符串生成doc文件
代碼如下:
// 組裝html(替換為自己的html)
String html = hostPatrolMessageContentToHtml2(dataVo.getReportDetail(), dataVo.getAlarmMap(), 0);
if (StringUtils.isNotEmpty(html)) {
// 生成臨時(shí)文件(doc)
try {
// 生成doc格式的word文檔,需要手動(dòng)改為docx
byte by[] = html.getBytes("UTF-8");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(by);
POIFSFileSystem poifsFileSystem = new POIFSFileSystem();
DirectoryEntry directoryEntry = poifsFileSystem.getRoot();
directoryEntry.createDocument("WordDocument", byteArrayInputStream);
// 臨時(shí)文件夾
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 + "主機(jī)巡檢報(bào)告告警內(nèi)容詳情.doc";
// 保存doc文檔
FileOutputStream outputStream = new FileOutputStream(fileUrl);
poifsFileSystem.writeFilesystem(outputStream);
byteArrayInputStream.close();
outputStream.close();
file = new File(fileUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
例如組裝的html為:
巡檢報(bào)告告警詳情:
- 主機(jī)(zyf主機(jī))發(fā)生嚴(yán)重警告
- 服務(wù)器探針(zyf主機(jī))發(fā)生嚴(yán)重警告
- 指標(biāo)名稱(內(nèi)存使用率),發(fā)生嚴(yán)重警告,閾值:> 25 %,采集值:77.84 %;
- 指標(biāo)名稱(CPU占用百分比),發(fā)生嚴(yán)重警告,閾值:> 30 %,采集值:65.64 %;
- 中間件探針(tomcat測(cè)試)采集失敗,返回信息: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]
- 數(shù)據(jù)庫(kù)探針(98MySQL庫(kù))采集失敗,返回信息:Failed to initialize pool: Access denied for user 'root'@'10.2.6.159' (using password: YES)
- 數(shù)據(jù)庫(kù)探針(本地Oracle測(cè)試庫(kù)19c)采集失敗,返回信息:Failed to initialize pool: ORA-28001: 口令已經(jīng)失效
- 數(shù)據(jù)庫(kù)探針(MySQL測(cè)試數(shù)據(jù)庫(kù))采集失敗,返回信息:Failed to initialize pool: Access denied for user 'root'@'10.2.6.159' (using password: YES)
- 服務(wù)器探針(zyf主機(jī))發(fā)生嚴(yán)重警告
生成的doc文件:
2. 將html字符串導(dǎo)出word文檔
代碼:
/**
* @param request
* @param response
* @param content 富文本內(nèi)容(html)
* @param fileName 生成word文件
* @throws Exception
*/
public static void exportWord(HttpServletRequest request, HttpServletResponse response, String content, String fileName) throws Exception {
ByteArrayInputStream byteArrayInputStream = null;
POIFSFileSystem poifsFileSystem = null;
ServletOutputStream servletOutputStream = null;
try {
byte b[] = content.getBytes("GBK"); //這里是必須要設(shè)置編碼的,不然導(dǎo)出中文就會(huì)亂碼。
byteArrayInputStream = new ByteArrayInputStream(b);//將字節(jié)數(shù)組包裝到流中
poifsFileSystem = new POIFSFileSystem();
DirectoryEntry directory = poifsFileSystem.getRoot();
directory.createDocument("WordDocument", byteArrayInputStream); //該步驟不可省略,否則會(huì)出現(xiàn)亂碼。
//輸出文件
request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");//導(dǎo)出word格式
response.addHeader("Content-Disposition", "attachment;filename=" + FileUtils.setFileDownloadHeader(request, fileName) + ".doc");
servletOutputStream = response.getOutputStream();
poifsFileSystem.writeFilesystem(servletOutputStream);
} catch (Exception e) {
throw e;
} finally {
byteArrayInputStream.close();
servletOutputStream.close();
poifsFileSystem.close();
}
}
// 適配瀏覽器
public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
String agent = request.getHeader("USER-AGENT");
String filename;
if (agent.contains("MSIE")) {
filename = URLEncoder.encode(fileName, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
filename = new String(fileName.getBytes(), "ISO8859-1");
} else if (agent.contains("Chrome")) {
filename = URLEncoder.encode(fileName, "utf-8");
} else {
filename = URLEncoder.encode(fileName, "utf-8");
}
return filename;
}
導(dǎo)出文件內(nèi)容:
對(duì)于富文本插件生成的html代碼也可以通過(guò)此方法生成word文件。
柚子快報(bào)邀請(qǐng)碼778899分享:java實(shí)現(xiàn)html轉(zhuǎn)word
相關(guān)閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。