【问题标题】:Java POI API: Convert from *.xlsx to *.xlsJava POI API:从 *.xlsx 转换为 *.xls
【发布时间】:2013-12-01 17:30:10
【问题描述】:

我有一个小问题。 想要使用 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,在两个相同的错误调用上。 爪哇:JDK 7 操作系统:Win 8.1 x64

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

问候

【问题讨论】:

  • 我想你只需要this jar
  • 类名应该像'XlsxConverter'一样大写。
  • 将 *.xlsx 转换为 *.xls 的更好方法是使用 MSO 或 LibreOffice 命令行和(可能是?)宏。使用POI,您会遇到一些风格问题。

标签: java excel apache-poi converter


【解决方案1】:

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

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

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

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

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

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

xmlBean2.3.0.jar

dom4j-1.6.jar

【讨论】:

  • 感谢您的帮助,它现在可以工作了(在我将 xml shema 也包含在路径中之后)对于其他帮助/提示,​​它们是很好的解决方案,但我编写了一个 java 工具,可以单独管理,无需任何外部软件;)非常感谢!
猜你喜欢
  • 2013-01-10
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 2016-07-11
相关资源
最近更新 更多