js代码
function ajaxFileUpload(){
$.ajaxFileUpload({
url:'importExcel',
fileElementId:'fileUp',
dataType: 'xml',
success: function (data, status)
{
setTimeout('loadre()',1500);
},
error: function (data, status, e)
{
alert('上传失败!!');
}
});
};
function loadre(){
$('#dg').datagrid('load','json/excel.json');
}
action代码
public String readExcel(File file){
FileInputStream is;
Sheet sheet;
Workbook wb=null;
Row row;
ArrayList<Student> stus=new ArrayList<Student>();
Map<String,Object> content = new HashMap<String,Object>();
String json="";
try {
System.out.println("file="+file);
is=new FileInputStream(file);
wb=WorkbookFactory.create(file);
// try {
// System.out.println("2003");
// wb=new HSSFWorkbook(is);
// } catch (Exception e) {
// System.out.println("2007");
// wb=new XSSFWorkbook(is);
// }
System.out.println("wb="+wb);
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 标题总列数
int colNum = row.getPhysicalNumberOfCells();
String[] title = new String[colNum];
for (int i = 0; i < colNum; i++) {
title[i] = getCellFormatValue(row.getCell((short) i));
}
int rowNum = sheet.getLastRowNum();
String str = "";
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < colNum) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + "-";
j++;
}
Student stu=new Student();
String[] s=str.split("-");
stu.setId(Integer.parseInt(s[0]));
stu.setSex(s[1]);
stu.setName(s[2]);
stus.add(stu);
str = "";
}
content.put("rows",stus);
content.put("total",stus.size());
json=JSONObject.toJSONString(content);
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
API
jar
程序现在读取2003版本能正常运行 2007版本运行到WorkbookFactory.create(file)
就不再网下走 打印WorkbookFactory.create(file) 也打印不出任何东西 网上找了很多资料了 POI的博客什么的也看了很多 都解决不了 哪位大神帮忙看看
随便建一个2007和2003的文件,作为打开的模板。
http://bbs.csdn.net/topics/390389724?page=1