java实现在线预览----poi操作excel转html及03、07版本兼容问题
来源:互联网 发布:高中文言文朗读软件 编辑:程序博客网 时间:2024/06/03 12:45
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转html Document 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()); } }这里我没贴读取图片的代码,因为我只能读取到所有图片但不能让图片按指定位置在页面显示,如果谁有简洁好用的代码可以分享给我,感激不尽
阅读全文
0 0
- java实现在线预览----poi操作excel转html及03、07版本兼容问题
- java实现在线预览----poi操作word转html及03、07版本兼容问题
- java实现在线预览---poi操作ppt转html及03、07版本兼容问题
- java实现在线预览--poi实现word、excel、ppt转html
- .net 实现Office文件预览,word文件在线预览、excel文件在线预览、ppt文件在线预览,excel转html,office格式转换,
- poi实现Excel转html
- java poi- 实现 word Excel pdf ppt 转 HTML
- POI 读取 Excel 转 HTML 支持 03xls 和 07xlsx 版本 包含样式,03xls07xlsx
- 【转】 Java 操作 Excel (读取Excel2007,Poi实现)
- 利用POI实现JAVA操作EXCEL表
- Java 操作 Excel (读取Excel2007,Poi实现)
- Java 操作 Excel (读取Excel2007,Poi实现)
- Java 操作 Excel (读取Excel2007,Poi实现)
- Java 操作 Excel (读取Excel2007,Poi实现)
- JAVA基于poi实现操作Excel
- poi 解析excel 03与07不兼容问题
- poi 解析excel 03与07不兼容问题
- poi 解析excel 03与07不兼容问题
- Redis配置文件解析
- Android给布局设置阴影
- 斐波那契数列的递归算法与非递归算法
- 项目记录
- eXtremeDb solution to Invalid multibyte string
- java实现在线预览----poi操作excel转html及03、07版本兼容问题
- Structured Streaming 之窗口事件时间聚合操作
- 欢迎使用CSDN-markdown编辑器
- Laravel验证简记
- mysql mapper 参数判断
- mybatis使用pagehelper分页报错java.lang.ClassCastException: com.github.pagehelper.PageHelper cannot be cast
- ce
- How to lookup django session for a particular user?
- 设计模式 外观模式 一键电影模式