Excel表格内容导出到页面
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();
}
}
}
最新文章
- 退役&;&;搬家
- PHP 输出缓冲控制(Output Control) 学习
- 获取元素在浏览器中的绝对位置(从jquery1.8中抠出来)
- The Little Redis Book
- VS代码生成工具ReSharper使用手册:配置快捷键
- poj 3301 Texas Trip(几何+三分)
- PF_RING packet overwrites
- ZOJ 3080 ChiBi(spfa)
- java 值传递和引用传递。
- HDU3371--Connect the Cities(最小生成树)
- servlet自动获取前端页面提交数据
- SpringMVC架构的项目,js,css等静态文件导入有问题
- CENTOS7错误:Cannot find a valid baseurl for repo: base/7/x86_6
- ionic 状态栏显示异常 statusBar
- [转]解决ssh登录后闲置时间过长而断开连接
- Nginx 解析PHP
- spark RDD操作的底层实现原理
- if嵌套语句 shell脚本实例 测试是否闰年
- 关于html5 audio 标签在ios系统上不能正常自动播放的解决办法
- Vmware esx/esxi Vlan三种配置模式(VST、EST和VGT)
热门文章
- SQL Server存储过程中字符串前加N的含义
- phpcms企业站的一些知识
- SAP-参数(条件表)配置教程–GS01/GS02/GS03
- 阿里P7详细解答JVM性能调优之监控工具
- django_rest framework 接口开发(一)
- javascript_02-变量
- ORA-28547: connection to server failed, probable Oracle Net admin error
- Spark foreachpartiton和mappartition的异同
- Java基本知识点o(1), o(n), o(logn), o(nlogn)的了解
- Bootstrap学习地址