将xls转换为html

时间:2016-10-27 14:35:47

标签: html excel

我们使用BufferedWriter和XSSFWorkbook使用以下代码将xls文件转换为Html

        final String NEW_LINE = "\n";
        final String HTML_FILE_EXTENSION = ".html";
        final String TEMP_FILE_EXTENSION = ".tmp";
        final String HTML_SNNIPET_2 = "<table>";
        final String HTML_SNNIPET_3 = "</table>";
        final String HTML_TR_S = "<tr>";
        final String HTML_TR_E = "</tr>";
        final String HTML_TD_S = "<td>";
        final String HTML_TD_E = "</td>";
           BufferedWriter writer;
        Workbook workbook;
        String fileName = file.getName();
        if (fileName.toLowerCase().endsWith(FILE_TYPES[0])) {
            workbook = new HSSFWorkbook(new FileInputStream(file));
        } else {
            workbook = new XSSFWorkbook(new FileInputStream(file));
        }

        File tempFile = File.createTempFile(fileName + '-', HTML_FILE_EXTENSION
                + TEMP_FILE_EXTENSION, new File(folderName));
        writer = new BufferedWriter(new FileWriter(tempFile));
        writer.write(fileName);
        writer.write(HTML_SNNIPET_2);
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rows = sheet.rowIterator();
        Iterator<Cell> cells = null;
        while (rows.hasNext()) {
            Row row = rows.next();
            cells = row.cellIterator();
            writer.write(NEW_LINE);
            writer.write(HTML_TR_S);
            while (cells.hasNext()) {
                Cell cell = cells.next();
                writer.write(HTML_TD_S);
                writer.write(cell.toString());
                writer.write(HTML_TD_E);
            }
            writer.write(HTML_TR_E);
        }
        writer.write(NEW_LINE);
        writer.write(HTML_SNNIPET_3);
        writer.close();

此代码工作正常。问题是xls文件有Graphs的时候。 当我们将xls文件转换为HTML时,它不会转换图形enter image description here

请参阅附带的xls文件,这是我们要转换为HTML的xls文件。

任何帮助将不胜感激

由于 拉玛

0 个答案:

没有答案