Java 将xlsx转换为xls时出错
我得到这个错误:Java 将xlsx转换为xls时出错,java,excel,Java,Excel,我得到这个错误: Exception in thread "main" java.lang.ClassCastException: org.apache.poi.xssf.usermodel.XSSFComment cannot be cast to org.apache.poi.hssf.usermodel.HSSFComment 当我试图编译以下代码时 我使用了库POI3.11和dom4j-1.6.1和xmlbeans 2.3.0 我用EclipseLuna在mac上编译了这
Exception in thread "main" java.lang.ClassCastException:
org.apache.poi.xssf.usermodel.XSSFComment cannot be cast to
org.apache.poi.hssf.usermodel.HSSFComment
当我试图编译以下代码时
我使用了库POI3.11
和dom4j-1.6.1
和xmlbeans 2.3.0
我用EclipseLuna在mac上编译了这段代码
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 FormulasExcel {
private String outFn;
private File inpFn;
public FormulasExcel(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();
}
}
public static void main(String[] args) throws InvalidFormatException, IOException{
FormulasExcel doit = new FormulasExcel(new File("/Users/Downloads/Layout.xlsx"));
doit.xlsx2xls_progress();
}
}
import java.io.BufferedOutputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.Iterator;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.openxml4j.exceptions.InvalidFormatException;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.CellStyle;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共类公式{
私有字符串输出;
私有文件inpFn;
公共公式SEXCEL(文件inpFn){
this.outFn=inpFn+“.xls”;
this.inpFn=inpFn;
}
public void xlsx2xls_progress()引发InvalidFormatException,IOException{
InputStream in=新文件InputStream(inpFn);
试一试{
XSSF工作簿wbIn=新XSSF工作簿(in);
文件输出=新文件(输出);
如果(outp.exists()){
删除();
}
工作簿wbOut=新的HSSF工作簿();
int sheetCnt=wbIn.getNumberOfSheets();
对于(int i=0;i
您是否可以包括导致错误的堆栈跟踪?基本上,这个错误告诉您试图以不允许的方式将一个类强制转换为另一个类。但是这样做的原因可能很多。另外,您是否尝试过使用不同的文件作为输入?这可能取决于文件中的意外输入数据。生成异常的是哪一行?Stacktrace:线程“main”java.lang.ClassCastException中的异常:org.apache.poi.xssf.usermodel.XSSFComment无法强制转换为org.apache.poi.hssf.usermodel.HSSFComment(HSSFCell.java:1018)在FormulasExcel.xlsx2xls_progress(FormulasExcel.java:82)在FormulasExcel.main(FormulasExcel.java:101)和您的第82行是???