Java POI API:从 *.xlsx 转换为 *.xls

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20049922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:41:25  来源:igfitidea点击:

Java POI API: Convert from *.xlsx to *.xls

javaexcelapache-poiconverter

提问by Lycann H. Faye

I have a small problem. Wanna convert the new excel files (.xlsx) into the old one (.xls) with the POI API on Java.

我有一个小问题。想使用 Java 上的 POI API将新的 excel 文件 ( .xlsx) 转换为旧的 (.xls)。

I think it is a mind problem, but I don't know which fault exist.

我觉得是脑子问题,不知道是哪个毛病。

I used these code here:

我在这里使用了这些代码:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XLSX2XLS{
    private String outFn;
    private File inpFn;

    public XLSX2XLS(File inpFn){
        this.outFn = inpFn + ".xls";
        this.inpFn = inpFn;
    }

    public void xlsx2xls_progress() throws InvalidFormatException,IOException {
        InputStream in = new FileInputStream(inpFn);
        try {
            XSSFWorkbook wbIn = new XSSFWorkbook(in);
            File outF = new File(outFn);
            if (outF.exists()) {
                outF.delete();
            }

            Workbook wbOut = new HSSFWorkbook();
            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());

                        }
                }
            }
            OutputStream out = new BufferedOutputStream(new FileOutputStream(outF));
            try {
                wbOut.write(out);
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }
}

And Java tells me that here:

Java 在这里告诉我:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at XLSX2XLS.xlsx2xls_progress(XLSX2XLS.java:35)
    at Workflow.main(Workflow.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    at java.net.URLClassLoader.run(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more

I test these class with POI 3.9. and 3.10, on both the same error calls. Java: JDK 7 OS: Win 8.1 x64

我用 POI 3.9 测试这些课程。和 3.10,在两个相同的错误调用上。Java:JDK 7 操作系统:Win 8.1 x64

I hope get enough information about my problem. Thanks for your helps.

我希望得到关于我的问题的足够信息。感谢您的帮助。

Greetings

你好

采纳答案by MouseLearnJava

Please be aware that as the new XSSF supported Excel 2007 OOXML (.xlsx) files are XML based.

请注意,由于新的 XSSF 支持 Excel 2007 OOXML (.xlsx) 文件是基于 XML 的。

You need to add extra 2 jars to make POI work on (.xlsx) Excel file.

您需要添加额外的 2 个 jar 以使 POI 在 (.xlsx) Excel 文件上工作。

Please add xmlbeans2.3.0.jarand dom4j-1.6.jarto your classpath. These 2 jars are the dependency jars for handling .xlsx Excel file in POI Library.

请将xmlbeans2.3.0.jar和添加dom4j-1.6.jar到您的类路径中。这两个 jar 是处理 POI 库中 .xlsx Excel 文件的依赖 jar。

If you have download POI source code, You can find these 2 jars under the following folder:

如果你下载了 POI 源代码,你可以在以下文件夹下找到这 2 个 jar:

\poi-bin-3.9-20121203\poi-3.9\ooxml-lib\

\poi-bin-3.9-20121203\poi-3.9\ooxml-lib\

If not, you can download them from the following site:

如果没有,您可以从以下站点下载它们:

xmlBean2.3.0.jar

xmlBean2.3.0.jar

dom4j-1.6.jar

dom4j-1.6.jar