简体   繁体   English

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

[英]Convert .xls to .xlsx format using java code

I Need to convert xml spread sheet 2003 to .xlsx format. 我需要将xml电子表格2003转换为.xlsx格式。

in my application one link is there, if i click that link it will download the file in download folder. 在我的应用程序中有一个链接,如果我单击该链接,它将下载文件到下载文件夹中。 when i try to open that excel file it gives a message 当我尝试打开该Excel文件时,它会显示一条消息

"The file format and extension of don't match. the file could be corrupted or unsafe. unless you trust its source, don't open it. Do you want open it anyway?" “文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任它的来源,否则请不要打开它。是否仍要打开它?”

and it gives the option Yes, No , and Help. 并提供选项Yes,No和Help。

If i click Yes, then i can see the excel sheet content. 如果单击“是”,则可以看到Excel工作表的内容。 The excel sheet extension is .xls but when i go to File in Excel sheet and click on 'save as' it shows the save as type is XML spread sheet 2003. Excel工作表扩展名是.xls,但是当我转到Excel工作表中的“文件”并单击“另存为”时,它显示另存为XML表单2003。

I can do it manually save as .xlsx but every time i can't do it. 我可以手动将其另存为.xlsx,但是每次我都做不到。 Even i tried with below code 即使我尝试了下面的代码

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();
    }
}
}

Error 错误

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)

From the Excel-message I suspect that the file you try to open is already in .xslx -Format but is saved using the wrong .xls -extension. 从Excel消息中,我怀疑您尝试打开的文件已经是.xslx -Format,但是使用错误的.xls -extension保存了。

This is supported by the poi-error-message which also appears when trying to open a .xlsx -file as a HSSFWorkbook instead of as a XSSFWorkbook - btw you seem to be using a quite old version of poi (version 2.x?), current is 3.13 and with all 3.x-versions the message yould be much more clear: 这是由POI错误消息试图打开时也出现支持.xlsx -file作为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;dr there is no need to do any conversion, just make sure the file gets saved with the correct extension. tl; dr不需要进行任何转换,只需确保以正确的扩展名保存文件即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM