使用java代码将.xls转换为.xlsx格式

时间:2015-12-21 12:41:29

标签: java

我需要将xml电子表格2003转换为.xlsx格式。

在我的应用程序中有一个链接,如果我点击该链接,它将下载文件在下载文件夹中。当我尝试打开该excel文件时,它会给出一条消息

"文件格式和扩展名不匹配。该文件可能已损坏或不安全。除非你相信它的来源,否则不要打开它。你想要打开吗?"

,它提供选项Yes,No和Help。

如果我单击是,那么我可以看到Excel工作表内容。 excel表格扩展名是.xls,但是当我转到Excel表格中的文件并点击“保存为”#39;它显示了保存类型是XML电子表格2003。

我可以手动保存为.xlsx,但每次我都无法做到。即使我尝试使用以下代码

public class xls2xlsx {

 public static void main(String[] args) throws InvalidFormatException,
        IOException {

    String inpFn = "F:\\Users\\Downloads\\Report.xls"; 
    String outFn = "F:\\Users\\Downloads\\Report.xlsx"; 

    InputStream in = new BufferedInputStream(new FileInputStream(inpFn));
    try {
        Workbook wbIn = new HSSFWorkbook(in);
        File outF = new File(outFn);
        if (outF.exists())
            outF.delete();

        Workbook wbOut = new XSSFWorkbook();
        int sheetCnt = wbIn.getNumberOfSheets();
        for (int i = 0; i < sheetCnt; i++) {
            Sheet sIn = wbIn.getSheetAt(0);
            Sheet sOut = wbOut.createSheet(sIn.getSheetName());
            Iterator<Row> rowIt = sIn.rowIterator();
            while (rowIt.hasNext()) {
                Row rowIn = rowIt.next();
                Row rowOut = sOut.createRow(rowIn.getRowNum());

                Iterator<Cell> cellIt = rowIn.cellIterator();
                while (cellIt.hasNext()) {
                    Cell cellIn = cellIt.next();
                    Cell cellOut = rowOut.createCell(
                            cellIn.getColumnIndex(), cellIn.getCellType());

                    switch (cellIn.getCellType()) {
                    case Cell.CELL_TYPE_BLANK:
                        break;

                    case Cell.CELL_TYPE_BOOLEAN:
                        cellOut.setCellValue(cellIn.getBooleanCellValue());
                        break;

                    case Cell.CELL_TYPE_ERROR:
                        cellOut.setCellValue(cellIn.getErrorCellValue());
                        break;

                    case Cell.CELL_TYPE_FORMULA:
                        cellOut.setCellFormula(cellIn.getCellFormula());
                        break;

                    case Cell.CELL_TYPE_NUMERIC:
                        cellOut.setCellValue(cellIn.getNumericCellValue());
                        break;

                    case Cell.CELL_TYPE_STRING:
                        cellOut.setCellValue(cellIn.getStringCellValue());
                        break;
                    }

                    {
                        CellStyle styleIn = cellIn.getCellStyle();
                        CellStyle styleOut = cellOut.getCellStyle();
                        styleOut.setDataFormat(styleIn.getDataFormat());
                    }
                    cellOut.setCellComment(cellIn.getCellComment());

                    // HSSFCellStyle cannot be cast to XSSFCellStyle
                    // cellOut.setCellStyle(cellIn.getCellStyle());
                }
            }
        }
        OutputStream out = new BufferedOutputStream(new FileOutputStream(
                outF));
        try {
            wbOut.write(out);
        } finally {
            out.close();
        }
    } finally {
        in.close();
    }
}
}

错误

Exception in thread "main" java.io.IOException: Invalid header signature; read 0x6576206C6D783F3C, expected 0xE11AB1A1E011CFD0
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:322)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:303)
    at excelSheet.xls2xlsx.main(xls2xlsx.java:42)

1 个答案:

答案 0 :(得分:0)

在Excel消息中,我怀疑您尝试打开的文件已在.xslx - 格式中,但使用错误的.xls - 扩展程序保存。

poi-error-message也支持此功能,当尝试以.xlsx而不是HSSFWorkbook打开XSSFWorkbook文件时也会出现这个错误消息 - 顺便说一下使用相当旧版本的poi(版本2.x?),当前是3.13,所有3.x版本的消息都要清晰得多:

Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. POI only supports OLE2 Office documents
    at org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:96)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:84)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:257)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:238)
    at de.nuernberger.zebra.poitest.Poitest.main(Poitest.java:10)

TL;博士 无需进行任何转换,只需确保使用正确的扩展名保存文件。