poi操作excel转html及其兼容问题

excel03是读取文件整个内容转为字符串存进html,excel07是读取文件内容拼成字符串存进html

       /*** excel03转html* filename:要读取的文件所在文件夹* filepath:文件名* htmlname:生成html名称* path:html存放路径* */public static void PoiExcelToHtml (HttpServletRequest request,String filepath,String sourceid) throws Exception {String htmlname="exportExcel"+sourceid+".html";String path=request.getSession().getServletContext().getRealPath("/view/excel");fileExists(path);//此方法是判断目录文件夹是否存在,这里就不贴了String filename=request.getSession().getServletContext().getRealPath("/vod/mp4");InputStream input=new FileInputStream(filename+"/"+filepath);HSSFWorkbook excelBook=new HSSFWorkbook(input);ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter (DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument() );excelToHtmlConverter.processWorkbook(excelBook);//excel转htmlDocument htmlDocument =excelToHtmlConverter.getDocument();ByteArrayOutputStream outStream = new ByteArrayOutputStream();//字节数组输出流DOMSource domSource = new DOMSource (htmlDocument);StreamResult streamResult = new StreamResult (outStream);/** 将document中的内容写入文件中,创建html页面 */TransformerFactory tf = TransformerFactory.newInstance();Transformer serializer = tf.newTransformer();serializer.setOutputProperty (OutputKeys.ENCODING, "utf-8");serializer.setOutputProperty (OutputKeys.INDENT, "yes");serializer.setOutputProperty (OutputKeys.METHOD, "html");serializer.transform (domSource, streamResult);outStream.close();String content = new String (outStream.toString("UTF-8"));FileUtils.writeStringToFile(new File (path, htmlname), content, "utf-8");}/*** excel07转html* filename:要读取的文件所在文件夹* filepath:文件名* htmlname:生成html名称* path:html存放路径* */public static void ExcelToHtml (HttpServletRequest request,String filepath,String sourceid) throws Exception{String htmlname="exportExcel"+sourceid+".html";String path=request.getSession().getServletContext().getRealPath("/view/excel");String filename=request.getSession().getServletContext().getRealPath("/vod/mp4");fileExists(path);Workbook workbook = null;InputStream is = new FileInputStream(filename+"/"+filepath);try {String html="";workbook =  new XSSFWorkbook(is);for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {Sheet sheet = workbook.getSheetAt(numSheet);if (sheet == null) {continue;}html+="=======================" + sheet.getSheetName() + "=========================<br><br>";int firstRowIndex = sheet.getFirstRowNum();int lastRowIndex = sheet.getLastRowNum();html+="<table border='1' align='left'>";Row firstRow = sheet.getRow(firstRowIndex);for (int i = firstRow.getFirstCellNum(); i <= firstRow.getLastCellNum(); i++) {Cell cell = firstRow.getCell(i);String cellValue = getCellValue(cell, true);html+="<th>" + cellValue + "</th>";}//行for (int rowIndex = firstRowIndex + 1; rowIndex <= lastRowIndex; rowIndex++) {Row currentRow = sheet.getRow(rowIndex);html+="<tr>";if(currentRow!=null){int firstColumnIndex = currentRow.getFirstCellNum(); int lastColumnIndex = currentRow.getLastCellNum();//列for (int columnIndex = firstColumnIndex; columnIndex <= lastColumnIndex; columnIndex++) {Cell currentCell = currentRow.getCell(columnIndex);String currentCellValue = getCellValue(currentCell, true);html+="<td>"+currentCellValue + "</td>";}}else{html+=" ";}html+="</tr>";}html+="</table>";ByteArrayOutputStream outStream = new ByteArrayOutputStream();DOMSource domSource = new DOMSource ();StreamResult streamResult = new StreamResult (outStream);TransformerFactory tf = TransformerFactory.newInstance();Transformer serializer = tf.newTransformer();serializer.setOutputProperty (OutputKeys.ENCODING, "utf-8");serializer.setOutputProperty (OutputKeys.INDENT, "yes");serializer.setOutputProperty (OutputKeys.METHOD, "html");serializer.transform (domSource, streamResult);outStream.close();FileUtils.writeStringToFile(new File (path, htmlname), html, "gbk");}} catch (Exception e) {e.printStackTrace();}}/*** 读取单元格* */private static String getCellValue(Cell cell, boolean treatAsStr) {if (cell == null) {return "";}if (treatAsStr) {cell.setCellType(Cell.CELL_TYPE_STRING);}if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {return String.valueOf(cell.getBooleanCellValue());} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {return String.valueOf(cell.getNumericCellValue());} else {return String.valueOf(cell.getStringCellValue());}}

这里我没贴读取图片的代码,因为我只能读取到所有图片但不能让图片按指定位置在页面显示,如果谁有简洁好用的代码可以分享给我,感激不尽

java实现在线预览----poi操作excel转html及03、07版本兼容问题相关推荐

  1. java实现在线预览word,excel,ppt文档

    项目中遇到的word文档在线预览需求,查阅很多资料决定利用openoffice转换word文档为pdf进行预览实现. 1.下载openoffice4安装 www.openoffice.org: 2.导 ...

  2. 使用永中文档实现java在线预览Word,Excel,Pptx,Pdf

    使用永中文档实现java在线预览Word,Excel,Pptx,Pdf 永中文档提供了在线预览的功能 永中开发者文档 如果需要直接运行,请直接修改代码中的两个参数 转换类型在下方,根据传入以及输出类型 ...

  3. Java实现在线预览功能

    java实现在线预览功能,需要用到  jacob.dll jacob.jar   预览pdf所需js  pdfobject.min.js 将上传文件转为pdf保存. <divclass=&quo ...

  4. mvc直接在html页面预览pdf,Asp.net MVC 实现在线预览word、excel、ppt、pdf文件

    在线预览word.excel.ppt 原理:主要是引用第三方Dll使本地word.excel.ppt文件转换成Html 需要引用 : Aspose.Cells.dll Aspose.Slides.dl ...

  5. 前端页面预览word_详解html实现在线预览word、excel、pdf等文件的功能(附代码)_WEB前端开发...

    JavaScript判断"字典"为空的方法_WEB前端开发 字典是一种存储键值对的数据结构,Javascript中的Object类内部即实现为一个字典,本文就来为大家介绍一下判断字 ...

  6. java实现在线预览txt转pdf

    java实现在线预览txt转pdf 首先引入itext的jar包 <dependency><groupId>com.itextpdf</groupId><ar ...

  7. 在线预览word、excel、pdf、txt、png等功能实现(附简单源码)

    项目过程中,有时候我们需要对一些附件进行预览的操作,这里讲述一下实现过程,首先如标题所写的预览格式,预览word和excel是利用POI生成临时文件即把word或者excel生成html文件然后再通过 ...

  8. java 文件在线预览_java 在线预览doc,pdf

    先说一说如何实现在线预览doc 网上查了很多资料,基本思路就是将 doc 转为 pdf,由于低版本浏览器不支持预览 pdf,所以基本是再将 pdf 转为 swf (使用FlexPaper + swft ...

  9. java word在线预览_java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)...

    背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...

最新文章

  1. Ubuntu使用QCustomPlot简介
  2. Java Remote Debug(远程调试)
  3. Windows 查看所有进程命令tasklist
  4. backlight misc驱动范例 及应用程序范例
  5. 尚硅谷李老师笔记3DQL
  6. linux shell和配置文件的执行顺序
  7. dojo动态创建widget
  8. 游戏服务器数据库踩过的坑
  9. java循环隔行变色_c:foreach标签详解----(隔行换背景颜色的问题)
  10. mysql+sqlplus命令找不到_oracle sqlplus命令报command not found
  11. 【愚公系列】2022年09月 微信小程序-电商项目-UI框架的选型
  12. 最近开发了一个向QQ好友自动发送消息的程序
  13. matlab 密文搜索 实验,基于B^+树的多关键字密文排序检索方法.pdf
  14. 《MIT科技评论》“35位35岁以下科技创新青年”名单出炉!AI领域有5人入选 | 2020中国区...
  15. 自平衡自行车本质就是一个惯性飞轮,本质就是一样的!!!
  16. 文本分析--NLTK自然语言处理
  17. OKEx货币对价格数量长度及精度
  18. Word2016写论文之题注功能——公式自动编号右对齐等操作
  19. 云原生监控报警可视化
  20. |与|| ,与 分别表示什么意思?

热门文章

  1. bzoj2122: 工作评估 分块 二分
  2. [Android] 仿小米空气净化器首页旋转UI
  3. 计算机组成原理之总线设计
  4. 基于中台思想的物流系统设计(一)
  5. Jupyter环境下运行R语言、Java
  6. java网络编程 UDP通信协议实现局域网内文件的发送和接收
  7. 测试工程师python常见面试题_测试人员python面试题
  8. 使用 Typora 编辑 Markdown 文件时插入链接的方式
  9. [游戏技术] 传染病 Contagion 文档说明
  10. 运维工程师面试的经历及面试相关问题