java实现在线预览----poi操作excel转html及03、07版本兼容问题
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版本兼容问题相关推荐
- java实现在线预览word,excel,ppt文档
项目中遇到的word文档在线预览需求,查阅很多资料决定利用openoffice转换word文档为pdf进行预览实现. 1.下载openoffice4安装 www.openoffice.org: 2.导 ...
- 使用永中文档实现java在线预览Word,Excel,Pptx,Pdf
使用永中文档实现java在线预览Word,Excel,Pptx,Pdf 永中文档提供了在线预览的功能 永中开发者文档 如果需要直接运行,请直接修改代码中的两个参数 转换类型在下方,根据传入以及输出类型 ...
- Java实现在线预览功能
java实现在线预览功能,需要用到 jacob.dll jacob.jar 预览pdf所需js pdfobject.min.js 将上传文件转为pdf保存. <divclass=&quo ...
- mvc直接在html页面预览pdf,Asp.net MVC 实现在线预览word、excel、ppt、pdf文件
在线预览word.excel.ppt 原理:主要是引用第三方Dll使本地word.excel.ppt文件转换成Html 需要引用 : Aspose.Cells.dll Aspose.Slides.dl ...
- 前端页面预览word_详解html实现在线预览word、excel、pdf等文件的功能(附代码)_WEB前端开发...
JavaScript判断"字典"为空的方法_WEB前端开发 字典是一种存储键值对的数据结构,Javascript中的Object类内部即实现为一个字典,本文就来为大家介绍一下判断字 ...
- java实现在线预览txt转pdf
java实现在线预览txt转pdf 首先引入itext的jar包 <dependency><groupId>com.itextpdf</groupId><ar ...
- 在线预览word、excel、pdf、txt、png等功能实现(附简单源码)
项目过程中,有时候我们需要对一些附件进行预览的操作,这里讲述一下实现过程,首先如标题所写的预览格式,预览word和excel是利用POI生成临时文件即把word或者excel生成html文件然后再通过 ...
- java 文件在线预览_java 在线预览doc,pdf
先说一说如何实现在线预览doc 网上查了很多资料,基本思路就是将 doc 转为 pdf,由于低版本浏览器不支持预览 pdf,所以基本是再将 pdf 转为 swf (使用FlexPaper + swft ...
- java word在线预览_java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)...
背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...
最新文章
- Ubuntu使用QCustomPlot简介
- Java Remote Debug(远程调试)
- Windows 查看所有进程命令tasklist
- backlight misc驱动范例 及应用程序范例
- 尚硅谷李老师笔记3DQL
- linux shell和配置文件的执行顺序
- dojo动态创建widget
- 游戏服务器数据库踩过的坑
- java循环隔行变色_c:foreach标签详解----(隔行换背景颜色的问题)
- mysql+sqlplus命令找不到_oracle sqlplus命令报command not found
- 【愚公系列】2022年09月 微信小程序-电商项目-UI框架的选型
- 最近开发了一个向QQ好友自动发送消息的程序
- matlab 密文搜索 实验,基于B^+树的多关键字密文排序检索方法.pdf
- 《MIT科技评论》“35位35岁以下科技创新青年”名单出炉!AI领域有5人入选 | 2020中国区...
- 自平衡自行车本质就是一个惯性飞轮,本质就是一样的!!!
- 文本分析--NLTK自然语言处理
- OKEx货币对价格数量长度及精度
- Word2016写论文之题注功能——公式自动编号右对齐等操作
- 云原生监控报警可视化
- |与|| ,与 分别表示什么意思?