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

使用java代码将.xls转换为.xlsx格式,java,Java,我需要将xml电子表格2003转换为.xlsx格式 在我的应用程序中有一个链接,如果我单击该链接,它将下载下载文件夹中的文件。当我试图打开excel文件时,它会显示一条消息 的文件格式和扩展名不匹配。该文件可能已损坏或不安全。除非您信任其来源,否则不要打开它。是否仍要打开它 它给出了是、否和帮助的选项 如果单击“是”,则可以看到excel工作表内容。 excel工作表扩展名是.xls,但当我转到excel工作表中的文件并单击“另存为”时,它显示的另存为类型是XML spread sheet 20

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

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

的文件格式和扩展名不匹配。该文件可能已损坏或不安全。除非您信任其来源,否则不要打开它。是否仍要打开它

它给出了是、否和帮助的选项

如果单击“是”,则可以看到excel工作表内容。 excel工作表扩展名是.xls,但当我转到excel工作表中的文件并单击“另存为”时,它显示的另存为类型是XML spread sheet 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();
    }
}
}
公共类xls2xlsx{
公共静态void main(字符串[]args)引发InvalidFormatException,
IOException{
String inpFn=“F:\\Users\\Downloads\\Report.xls”;
String outFn=“F:\\Users\\Downloads\\Report.xlsx”;
InputStream in=new BufferedInputStream(new FileInputStream(inpFn));
试一试{
工作手册wbIn=新的HSSF工作手册(in);
文件输出=新文件(输出);
如果(outp.exists())
删除();
工作簿wbOut=新XSSFWORKING();
int sheetCnt=wbIn.getNumberOfSheets();
对于(int i=0;i
错误

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)
线程“main”java.io.IOException中的异常:头签名无效;读取0x6576206C6D783F3C,应为0xE11AB1A1E011CFD0 位于org.apache.poi.poifs.storage.HeaderBlock(HeaderBlock.java:140) 位于org.apache.poi.poifs.storage.HeaderBlock(HeaderBlock.java:104) 位于org.apache.poi.poifs.filesystem.poifsffilesystem.(poifsffilesystem.java:138) 在org.apache.poi.hssf.usermodel.HSSFWorkbook上。(HSSFWorkbook.java:322) 在org.apache.poi.hssf.usermodel.HSSFWorkbook上(HSSFWorkbook.java:303) 在excelSheet.xls2xlsx.main上(xls2xlsx.java:42)
从Excel消息中,我怀疑您尝试打开的文件已经是
.xslx
-格式,但使用错误的
.xls
-扩展名保存

poi错误消息支持这一点,该消息在尝试将
.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)
线程“main”org.apache.poi.poifs.filesystem.OfficeXmlFileException中的异常:提供的数据似乎位于Office 2007+XML中。POI仅支持OLE2 Office文档 位于org.apache.poi.poifs.storage.HeaderBlockReader。(HeaderBlockReader.java:96) 位于org.apache.poi.poifs.filesystem.poifsffilesystem.(poifsffilesystem.java:84) 在org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:257) 在org.apache.poi.hssf.usermodel.HSSFWorkbook上(HSSFWorkbook.java:238) 位于de.nuernberger.zebra.poitest.poitest.main(poitest.java:10) tl;博士
无需进行任何转换,只需确保文件以正确的扩展名保存。

欢迎使用SO,在询问问题时请更具体一点:您尝试了什么,希望得到什么,等等。请参阅。在你试过的东西上贴一些代码。现在我清楚地解释了我的问题。请仔细阅读上面的内容。你还没有说明你的问题是什么。你发布的代码有什么问题?我想告诉你,@Zia显然Prasad已经在使用poi。。。