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

时间:2013-11-18 14:15:40

标签: java excel apache-poi converter

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

我认为这是一个心灵问题,但我不知道存在哪个错误。

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

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

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$1.run(Unknown Source)
    at java.net.URLClassLoader$1.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

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

我希望获得有关我的问题的足够信息。 谢谢你的帮助。

问候

1 个答案:

答案 0 :(得分:5)

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

您需要添加额外的2个罐子才能使POI适用于(.xlsx)Excel文件。

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

如果您有下载POI源代码,您可以在以下文件夹下找到这两个jar:

<强> \poi-bin-3.9-20121203\poi-3.9\ooxml-lib\

如果没有,您可以从以下网站下载:

xmlBean2.3.0.jar

dom4j-1.6.jar