在这里需要你的帮助......我最近一直致力于基于xls或xlsx选项生成excel文档。早些时候默认是xls。但是,如果以xlsx格式生成excel工作表,我将面临以下错误。
Caused by: **java.lang.reflect.InvocationTargetException**
SystemErr R at sun.reflect.GeneratedConstructorAccessor193.newInstance(Unknown Source)
SystemErr R at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
SystemErr R at java.lang.reflect.Constructor.newInstance(Constructor.java:539)
SystemErr R at **org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)**
SystemErr R ... 17 more
SystemErr R **Caused by: java.io.IOException: error: </sst> does not close tag <t>
SystemErr R** at org.apache.poi.xssf.model.SharedStringsTable.readFrom(SharedStringsTable.java:125)
SystemErr R **at org.apache.poi.xssf.model.SharedStringsTable.<init>**(SharedStringsTable.java:102)
SystemErr R ... 21 more
代码段:
public String getFilename(String formatType){
Workbook workBook = null;
if((".xlsx").equalsIgnoreCase(formatType))
{
workBook = new XSSFWorkbook();
}
else
{
workBook = new HSSFWorkbook();
}
CreationHelper createHelper = workBook.getCreationHelper();
String sb= FormDetails.getFileNm();
if(formatType.equalsIgnoreCase(".xls"))
{
sb=sb.replace(".xlsx",".xls" );
}
FileInputStream file= new FileInputStream(path+"/"+sb);
workBook = WorkbookFactory.create(file);
Sheet sheet = workBook.getSheetAt(0);
File tempFile = new File(path + fileNameSb.toString());
FileOutputStream fos;
fos = new FileOutputStream(tempFile);
workBook.write(fos);
fos.close();
FilePath=path + fileNameSb.toString();
return FilePath;
}----creating a temporary file and storing it in a location
public void createExcel(String formatType)
{
Workbook workBook = null;
if((".xlsx").equalsIgnoreCase(formatType))
{
workBook = new XSSFWorkbook();
}
else
{
workBook = new HSSFWorkbook();
}
String FilePath = getFilename();----retreiving the file and creating a workbook using the inputstream
FileInputStream fileP= new FileInputStream(FilePath);
workBook = WorkbookFactory.create(fileP);---- Exception here!!!!!
}
此外,我不会在excel生成过程中始终收到此错误。
使用的POI版本:
POI-3.9-20121203.jar
POI-excelant-3.9-20121203.jar
的 POI-OOXML-3.9-20121203.jar
POI-OOXML-架构 - 3.9-20121203.jar
poi-scratchpad-3.9-20121203.jar