有朋友想知道excel 轉html實作,安排!于是我在原文的基礎的基礎上新增加了excel轉html的實作方式,可是在送出修改時被告知"修改篇幅過大 請減少修改篇幅",不讓送出,無奈隻能重新再發一篇文章了
修改篇幅過大 請減少修改篇幅
實作文檔線上預覽的方式除了上篇文章《文檔線上預覽(一)通過将txt、word、pdf轉成圖檔實作線上預覽功能》說的将文檔轉成圖檔的實作方式外,還有轉成pdf,前端通過pdf.js、pdfobject.js等插件來實作線上預覽,以及本文将要說到的将文檔轉成html的方式來實作線上預覽。代碼基于 aspose-words(用于word轉html),pdfbox(用于pdf轉html),是以事先需要在項目裡下面兩個依賴:
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-words</artifactId>
<version>23.1</version></dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
一、将檔案轉換成html字元串
1、将word檔案轉成html字元串
public static String wordToHtmlStr(String wordPath) {
try {
Document doc = new Document(wordPath); // Address是将要被轉化的word文檔
String htmlStr = doc.toString();
return htmlStr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
驗證結果:
2、将pdf檔案轉成html字元串
public static String pdfToHtmlStr(String pdfPath) throws IOException, ParserConfigurationException {
PDDocument document = PDDocument.load(new File(pdfPath));
Writer writer = new StringWriter();
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
return writer.toString();
}
驗證結果:
3、将excel檔案轉成html字元串
public static String excelToHtmlStr(String excelPath) throws Exception {
FileInputStream fileInputStream = new FileInputStream(excelPath);
Workbook workbook = new XSSFWorkbook(fileInputStream);
DataFormatter dataFormatter = new DataFormatter();
FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
Sheet sheet = workbook.getSheetAt(0);
StringBuilder htmlStringBuilder = new StringBuilder();
htmlStringBuilder.append("<html><head><title>Excel to HTML using Java and POI library</title>");
htmlStringBuilder.append("<style>table, th, td { border: 1px solid black; }</style>");
htmlStringBuilder.append("</head><body><table>");
for (Row row : sheet) {
htmlStringBuilder.append("<tr>");
for (Cell cell : row) {
CellType cellType = cell.getCellType();
if (cellType == CellType.FORMULA) {
formulaEvaluator.evaluateFormulaCell(cell);
cellType = cell.getCachedFormulaResultType();
}
String cellValue = dataFormatter.formatCellValue(cell, formulaEvaluator);
htmlStringBuilder.append("<td>").append(cellValue).append("</td>");
}
htmlStringBuilder.append("</tr>");
}
htmlStringBuilder.append("</table></body></html>");
return htmlStringBuilder.toString();
}
傳回的html字元串:
<html><head><title>Excel to HTML using Java and POI library</title><style>table, th, td { border: 1px solid black; }</style></head><body><table><tr><td>序号</td><td>姓名</td><td>性别</td><td>聯系方式</td><td>位址</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>1</td><td>張曉玲</td><td>女</td><td>11111111111</td><td>上海市浦東新區xx路xx弄xx号</td></tr><tr><td>2</td><td>王小二</td><td>男</td><td>1222222</td><td>上海市浦東新區xx路xx弄xx号</td></tr></table></body></html>
二、将檔案轉換成html,并生成html檔案
有時我們是需要的不僅僅傳回html字元串,而是需要生成一個html檔案這時應該怎麼做呢?一個改動量小的做法就是使用org.apache.commons.io包下的FileUtils工具類寫入目标位址:
FileUtils類将html字元串生成html檔案示例:
首先需要引入pom:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
相關代碼:
String htmlStr = FileConvertUtil.pdfToHtmlStr("D:\\書籍\\電子書\\小說\\曆史小說\\最後的可汗.doc");
FileUtils.write(new File("D:\\test\\doc.html"), htmlStr, "utf-8");
除此之外,還可以對上面的代碼進行一些調整,已實作生成html檔案,代碼調整如下:
1、将word檔案轉換成html檔案
public static void wordToHtml(String wordPath, String htmlPath) {
try {
File sourceFile = new File(wordPath);
String path = htmlPath + File.separator + sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf(".")) + ".html";
File file = new File(path); // 建立一個空白pdf文檔
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(wordPath); // Address是将要被轉化的word文檔
HtmlSaveOptions options = new HtmlSaveOptions();
options.setExportImagesAsBase64(true);
options.setExportRelativeFontSize(true);
doc.save(os, options);
} catch (Exception e) {
e.printStackTrace();
}
}
word原檔案效果:
word檔案轉換成html效果:
2、将pdf檔案轉換成html檔案
public static void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException {
File file = new File(pdfPath);
String path = htmlPath + File.separator + file.getName().substring(0, file.getName().lastIndexOf(".")) + ".html";
PDDocument document = PDDocument.load(new File(pdfPath));
Writer writer = new PrintWriter(path, "UTF-8");
new PDFDomTree().writeText(document, writer);
writer.close();
document.close();
}
圖檔版PDF檔案驗證結果:
文字版PDF原檔案效果:
文字版PDF檔案驗證結果:
3、将excel檔案轉換成html檔案
public static void excelToHtml(String excelPath, String htmlPath) throws Exception {
String path = FileUtil.getNewFileFullPath(excelPath, htmlPath, "html");
try(FileOutputStream fileOutputStream = new FileOutputStream(path)){
String htmlStr = excelToHtmlStr(excelPath);
byte[] bytes = htmlStr.getBytes();
fileOutputStream.write(bytes);
}
}
excel原檔案效果:
excel檔案轉換成html檔案驗證效果:
總結
從上述的效果展示我們可以發現其實轉成html效果不是太理想,很多細節樣式沒有還原,這其實是因為這類轉換往往都是追求目标是通過使用文檔中的語義資訊并忽略其他細節來生成簡單幹淨的 HTML,是以在轉換過程中複雜樣式被忽略,比如居中、首行縮進、字型,文本大小,顔色。舉個例子在轉換是 會将應用标題 1 樣式的任何段落轉換為 h1 元素,而不是嘗試完全複制标題的樣式。是以轉成html的顯示效果往往和原文檔不太一樣。這意味着對于較複雜的文檔而言,這種轉換不太可能是完美的。但如果都是隻使用簡單樣式文檔或者對文檔樣式不太關心的這種方式也不妨一試。