public void addExcelBooks() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
String filepath = request.getParameter("filepath");
String fileType = filepath.substring(filepath.lastIndexOf("."));
InputStream is = null;
Workbook wb = null;
try {
String filedir = ServletActionContext.getServletContext().getRealPath(filepath);
File file = new File(filedir);
is = new FileInputStream(file);
if (fileType.equals(".xls")) {
wb = new HSSFWorkbook(is);
} else if (fileType.equals(".xlsx")) {
wb = new XSSFWorkbook(is);
} else {
throw new Exception("读取的不是Excel文件");
}
JSONArray jsonarr = new JSONArray();
for (int numSheet = 0; numSheet < wb.getNumberOfSheets(); numSheet++) {//excel的sheet
Sheet hf = wb.getSheetAt(numSheet);
if (hf == null) {
continue;
}
JSONObject jsonChildObj = new JSONObject();
for (int rowNum = 1; rowNum <= hf.getLastRowNum(); rowNum++) {
Row hr = hf.getRow(rowNum);
if (hr == null) {
break;
}
Cell hxh = hr.getCell(0);
Cell hca = hr.getCell(1);
Cell hcb = hr.getCell(2);
if (hca == null || hca.toString().equals("") || hcb == null || hcb.toString().equals("") || hxh == null || hxh.toString().equals("")) {
break;
}
if (hxh.getCellType() != Cell.CELL_TYPE_NUMERIC) {判断类型对否excel表的第一列为书序为int类型对应的就是CELL_TYPE_NUMERIC
jsonChildObj.put("fail", "FAIL");
jsonarr.add(jsonChildObj);
// throw new Exception("文件内容格式错误");
} else {
int xnumber = (int) hr.getCell(0).getNumericCellValue();
String name = hr.getCell(1).getStringCellValue();
int revision = (int) hr.getCell(2).getNumericCellValue();

//这里转换为json类型前台通过ajax获得
jsonChildObj.put("xnumber", xnumber);
jsonChildObj.put("name", name);
jsonChildObj.put("revision", revision);
jsonarr.add(jsonChildObj);
}
}
}
jsonPrint(jsonarr);

} catch (FileNotFoundException e) {
throw e;
} finally {
if (wb != null) {
wb = null;
}
if (is != null) {
is.close();
}
}
}

最新文章

  1. 退役&amp;&amp;搬家
  2. PHP 输出缓冲控制(Output Control) 学习
  3. 获取元素在浏览器中的绝对位置(从jquery1.8中抠出来)
  4. The Little Redis Book
  5. VS代码生成工具ReSharper使用手册:配置快捷键
  6. poj 3301 Texas Trip(几何+三分)
  7. PF_RING packet overwrites
  8. ZOJ 3080 ChiBi(spfa)
  9. java 值传递和引用传递。
  10. HDU3371--Connect the Cities(最小生成树)
  11. servlet自动获取前端页面提交数据
  12. SpringMVC架构的项目,js,css等静态文件导入有问题
  13. CENTOS7错误:Cannot find a valid baseurl for repo: base/7/x86_6
  14. ionic 状态栏显示异常 statusBar
  15. [转]解决ssh登录后闲置时间过长而断开连接
  16. Nginx 解析PHP
  17. spark RDD操作的底层实现原理
  18. if嵌套语句 shell脚本实例 测试是否闰年
  19. 关于html5 audio 标签在ios系统上不能正常自动播放的解决办法
  20. Vmware esx/esxi Vlan三种配置模式(VST、EST和VGT)

热门文章

  1. SQL Server存储过程中字符串前加N的含义
  2. phpcms企业站的一些知识
  3. SAP-参数(条件表)配置教程–GS01/GS02/GS03
  4. 阿里P7详细解答JVM性能调优之监控工具
  5. django_rest framework 接口开发(一)
  6. javascript_02-变量
  7. ORA-28547: connection to server failed, probable Oracle Net admin error
  8. Spark foreachpartiton和mappartition的异同
  9. Java基本知识点o(1), o(n), o(logn), o(nlogn)的了解
  10. Bootstrap学习地址