问题描述:在创建 HSSFWorkbook wb = new HSSFWorkbook(fs); 抛出异常代码如下:
POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream(add));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0); String Strcell = ""; for(int i=0;i<sheet.getLastRowNum();i++){ HSSFRow row=sheet.getRow(i);
for(short j=0;j<row.getLastCellNum();j++){ HSSFCell cell=row.getCell(j);
switch (cell.getCellType()) {
// 如果当前Cell的Type为NUMERIC
case HSSFCell.CELL_TYPE_NUMERIC:
// 判断当前的cell是否为Date
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 如果是Date类型则,取得该Cell的Date值
Date date = cell.getDateCellValue();
// 把Date转换成本地格式的字符串
Strcell = cell.getDateCellValue().toLocaleString();
}
// 如果是纯数字
else {
// 取得当前Cell的数值
Integer num = new Integer((int) cell
.getNumericCellValue());
Strcell = String.valueOf(num);
}
break;
case Cell.CELL_TYPE_BOOLEAN:
//得到Boolean对象的方法
//cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
//读取公式
Strcell = this.readformula(cell);
break;
case Cell.CELL_TYPE_STRING:
//读取String
Strcell = cell.getStringCellValue().toString();
break;
case 3:
Strcell = "";
break;
}
System.out.println(Strcell);
}
// System.out.println(Strcell);
}如下异常:
java.lang.NegativeArraySizeException
at org.apache.poi.hssf.record.SSTDeserializer.readStringRemainder(SSTDeserializer.java:335)
at org.apache.poi.hssf.record.SSTDeserializer.processContinueRecord(SSTDeserializer.java:320)
at org.apache.poi.hssf.record.SSTRecord.processContinueRecord(SSTRecord.java:539)
at org.apache.poi.hssf.record.RecordFactory.createRecords(RecordFactory.java:218)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:187)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:154)
at com.allsided.workers.web.action.SocialScoreAction.scoreUpload(SocialScoreAction.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.opensymphony.xwork.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:353)
at com.opensymphony.xwork.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:208)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:182)
初步怀疑是由于jar包之间起因的冲突,但是不知道是那些包之间会引起以上错误。求有经验的分享您的经验,谢谢!
POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream(add));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0); String Strcell = ""; for(int i=0;i<sheet.getLastRowNum();i++){ HSSFRow row=sheet.getRow(i);
for(short j=0;j<row.getLastCellNum();j++){ HSSFCell cell=row.getCell(j);
switch (cell.getCellType()) {
// 如果当前Cell的Type为NUMERIC
case HSSFCell.CELL_TYPE_NUMERIC:
// 判断当前的cell是否为Date
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 如果是Date类型则,取得该Cell的Date值
Date date = cell.getDateCellValue();
// 把Date转换成本地格式的字符串
Strcell = cell.getDateCellValue().toLocaleString();
}
// 如果是纯数字
else {
// 取得当前Cell的数值
Integer num = new Integer((int) cell
.getNumericCellValue());
Strcell = String.valueOf(num);
}
break;
case Cell.CELL_TYPE_BOOLEAN:
//得到Boolean对象的方法
//cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
//读取公式
Strcell = this.readformula(cell);
break;
case Cell.CELL_TYPE_STRING:
//读取String
Strcell = cell.getStringCellValue().toString();
break;
case 3:
Strcell = "";
break;
}
System.out.println(Strcell);
}
// System.out.println(Strcell);
}如下异常:
java.lang.NegativeArraySizeException
at org.apache.poi.hssf.record.SSTDeserializer.readStringRemainder(SSTDeserializer.java:335)
at org.apache.poi.hssf.record.SSTDeserializer.processContinueRecord(SSTDeserializer.java:320)
at org.apache.poi.hssf.record.SSTRecord.processContinueRecord(SSTRecord.java:539)
at org.apache.poi.hssf.record.RecordFactory.createRecords(RecordFactory.java:218)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:187)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:154)
at com.allsided.workers.web.action.SocialScoreAction.scoreUpload(SocialScoreAction.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.opensymphony.xwork.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:353)
at com.opensymphony.xwork.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:208)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:182)
初步怀疑是由于jar包之间起因的冲突,但是不知道是那些包之间会引起以上错误。求有经验的分享您的经验,谢谢!
解决方案 »
- |.NET转Java|在C#中给对像添加扩展方法,那JAVA有没有办法添加 谢谢
- 加密 总结
- 求助高手:Spring +MSSQL2000下,使用jdbcTemplate.update()插入数据失败
- JBuide2005不能查看XML配置文件原碼
- WEB—INf文件夹下的spring配置文件如何读取
- 菜鸟提问:使用ajax调用xml加载表格的问题
- xsl-fo问题
- 关于s:submit的问题
- 请教lunece分词Analyzer luceneAnalyzer = new IKAnalyzer()的问题
- 请问如何实现网上支付?那里可以找到这方面的资料?谢谢
- hibernate一对多问题
- appfuse的tab功能
我也遇到了这个问题,现在发现是可能这个excel是从2007和2003的版本问题引起的.
我的代码是只对2003有效,结果一个excel文件是把2007转存为2003兼容格式的,结果就可能出现这个问题..不懂..换了jar包为3.0以上版本的就可以了.原因还是没有弄懂..
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1"); HSSFRow row = sheet.createRow(6);
HSSFCell cell=null;
HSSFCellStyle style = wb.createCellStyle();
String[] str={"序号","姓","名","年龄"};
for(int i=0;i<str.length;i++){
cell = row.createCell((short)i);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(str[i]);
}
/* HSSFCell cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("序号"); cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("姓"); cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("名"); cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("年龄");
*/ List<User> list = this.findAll();
System.out.println("# "+list); for (int i = 0; i < list.size(); ++i)
{
User user = list.get(i);
row = sheet.createRow(i + 7); cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(i + 1); cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(user.getFirstname()); cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(user.getLastname()); cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(user.getAge());
}
//利用流体得到内存中数据的方法一
/* String fileName = MakeRandomString.RandomString1(10);
fileName = new StringBuffer(fileName).append(".xls").toString();
final File file = new File(fileName); try
{
OutputStream os = new FileOutputStream(file);
wb.write(os);
os.close();
}
catch (Exception e)
{
e.printStackTrace();
} InputStream is = null;
try
{
is = new FileInputStream(file);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
} new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(15000);
file.delete();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
).start();
return is;
*/
//利用流体得到内存中数据的方法二
ByteArrayOutputStream os=new ByteArrayOutputStream(1024);
try {
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
}
/* byte[] b=wb.getBytes();//此方法仅仅只获得HSSFWorkbook中部分的字符
System.out.println(b);
InputStream is=new ByteArrayInputStream(b);
*/
byte[] b=os.toByteArray();
System.out.println(b);
InputStream is=new ByteArrayInputStream(b);
return is;
}
借鉴下我的构造,我在这是想活得一个输入流,用于文件下载,HSSFWorkbook里面构造的数据就是我下载文件中的数据。