gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:28 25 4
gpt4 key购买 nike

我有一个小问题。想使用 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 测试这些类(class)。和 3.10,在两个相同的错误调用上。Java:JDK 7操作系统:Win 8.1 x64

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

问候

最佳答案

请注意,由于新的 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 源代码,您可以在以下文件夹下找到这两个 jar:

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

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

xmlBean2.3.0.jar

dom4j-1.6.jar

关于Java POI API : Convert from *. xlsx 到 *.xls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20049922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com