Lyc*_*aye 3 java excel converter apache-poi
我有一个小问题.想要使用Java上的POI API 将新的excel文件(.xlsx)转换为旧的(.xlsx).
我认为这是一个心灵问题,但我不知道存在哪个错误.
我在这里使用这些代码:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我用POI 3.9测试这些课程.和3.10,两个相同的错误调用.Java:JDK 7 OS:Win 8.1 x64
我希望得到关于我的问题的足够信息.谢谢你的帮助.
问候
请注意,由于新的XSSF支持Excel 2007 OOXML(.xlsx)文件是基于XML的.
您需要添加额外的2个罐子才能使POI在(.xlsx)Excel文件上运行.
请新增xmlbeans2.3.0.jar并dom4j-1.6.jar到类路径.这两个jar是用于处理POI库中的.xlsx Excel文件的依赖jar.
如果您有下载POI源代码,您可以在以下文件夹下找到这两个jar:
\poi-bin-3.9-20121203\poi-3.9\ooxml-lib\
如果没有,您可以从以下站点下载它们:
| 归档时间: |
|
| 查看次数: |
13455 次 |
| 最近记录: |