文档在线预览(二)word、pdf、excel文件转html以实现文档在线预览

您所在的位置:网站首页 word文档生成目录 文档在线预览(二)word、pdf、excel文件转html以实现文档在线预览

文档在线预览(二)word、pdf、excel文件转html以实现文档在线预览

2023-06-02 06:29| 来源: 网络整理| 查看: 265

@

目录一、将文件转换成html字符串1、将word文件转成html字符串2、将pdf文件转成html字符串3、将excel文件转成html字符串二、将文件转换成html,并生成html文件FileUtils类将html字符串生成html文件示例:1、将word文件转换成html文件2、将pdf文件转换成html文件3、将excel文件转换成html文件总结 实现文档在线预览的方式除了上篇文章《文档在线预览(一)通过将txt、word、pdf转成图片实现在线预览功能》说的将文档转成图片的实现方式外,还有转成pdf,前端通过pdf.js、pdfobject.js等插件来实现在线预览,以及本文将要说到的将文档转成html的方式来实现在线预览。代码基于 aspose-words(用于word转html),pdfbox(用于pdf转html),所以事先需要在项目里下面两个依赖:

com.luhuiguo aspose-words 23.1 org.apache.pdfbox pdfbox 2.0.4 一、将文件转换成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("Excel to HTML using Java and POI library"); htmlStringBuilder.append("table, th, td { border: 1px solid black; }"); htmlStringBuilder.append(""); for (Row row : sheet) { htmlStringBuilder.append(""); 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("").append(cellValue).append(""); } htmlStringBuilder.append(""); } htmlStringBuilder.append(""); return htmlStringBuilder.toString(); }

返回的html字符串:

Excel to HTML using Java and POI librarytable, th, td { border: 1px solid black; }序号姓名性别联系方式地址1张晓玲女11111111111上海市浦东新区xx路xx弄xx号2王小二男1222222上海市浦东新区xx路xx弄xx号1张晓玲女11111111111上海市浦东新区xx路xx弄xx号2王小二男1222222上海市浦东新区xx路xx弄xx号1张晓玲女11111111111上海市浦东新区xx路xx弄xx号2王小二男1222222上海市浦东新区xx路xx弄xx号1张晓玲女11111111111上海市浦东新区xx路xx弄xx号2王小二男1222222上海市浦东新区xx路xx弄xx号1张晓玲女11111111111上海市浦东新区xx路xx弄xx号2王小二男1222222上海市浦东新区xx路xx弄xx号1张晓玲女11111111111上海市浦东新区xx路xx弄xx号2王小二男1222222上海市浦东新区xx路xx弄xx号1张晓玲女11111111111上海市浦东新区xx路xx弄xx号2王小二男1222222上海市浦东新区xx路xx弄xx号 二、将文件转换成html,并生成html文件

有时我们是需要的不仅仅返回html字符串,而是需要生成一个html文件这时应该怎么做呢?一个改动量小的做法就是使用org.apache.commons.io包下的FileUtils工具类写入目标地址:

FileUtils类将html字符串生成html文件示例:

首先需要引入pom:

commons-io commons-io 2.8.0

相关代码:

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的显示效果往往和原文档不太一样。这意味着对于较复杂的文档而言,这种转换不太可能是完美的。但如果都是只使用简单样式文档或者对文档样式不太关心的这种方式也不妨一试。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3