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