java下对excel文件的上传
来源:互联网 发布:李世宏dota2 知乎 编辑:程序博客网 时间:2024/05/23 23:34
public class ExcelOperatorClass { private static final String EXCEL2003L = ".xls"; private static final String EXCEL2007U = ".xlsx"; /** * 描述:获取IO流中的数据,组装成List<List<Object>>对象 * * @param fileName * @return * @throws Exception */ public List<List<Object>> getSheetData(String fileName) throws Exception { List<List<Object>> list = null; //创建Excel工作薄 Workbook work = this.getWorkbook2007(fileName); if (null == work) { throw new Exception("创建Excel工作薄为空!"); } Sheet sheet = null; Row row = null; Cell cell = null; list = new ArrayList<List<Object>>(); //遍历Excel中所有的sheet for (int i = 0; i < work.getNumberOfSheets(); i++) { sheet = work.getSheetAt(i); if (sheet == null) { continue; } //遍历当前sheet中的所有行 for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) { row = sheet.getRow(j); if (row == null || row.getFirstCellNum() == j) { continue; } //遍历所有的列 List<Object> li = new ArrayList<Object>(); for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) { cell = row.getCell(y); li.add(this.getCellValue(cell)); } list.add(li); } } work.close(); return list; } /** * 描述:获取IO流中的数据,组装成List<List<Object>>对象 * * @param in,fileName * @return * @throws Exception */ public List<List<Object>> getSheetData(InputStream in, String fileName, int page) throws Exception { List<List<Object>> list = null; //创建Excel工作薄 Workbook work = this.getWorkbook(in, fileName); if (null == work) { throw new Exception("创建Excel工作薄为空!"); } Sheet sheet = null; Row row = null; Cell cell = null; list = new ArrayList<List<Object>>(); if (work.getNumberOfSheets() >= page) { sheet = work.getSheetAt(page - 1); //遍历当前sheet中的所有行 for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) { row = sheet.getRow(j); if(row == null || row.getCell(0) == null || CommonUtils.isEmptyString(row.getCell(0).getStringCellValue())) { break; } //遍历所有的列 List<Object> li = new ArrayList<Object>(); for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) { cell = row.getCell(y); if(cell == null) { return null; } li.add(this.getCellValue(cell)); } list.add(li); } } work.close(); return list; } /** * 描述:根据文件后缀,自适应上传文件的版本 * * @param inStr,fileName * @return * @throws Exception */ public Workbook getWorkbook(InputStream inStr, String fileName) throws Exception { Workbook wb = null; String fileType = fileName.substring(fileName.lastIndexOf(".")); if (EXCEL2003L.equals(fileType)) { wb = new HSSFWorkbook(inStr); //2003- } else if (EXCEL2007U.equals(fileType)) { wb = new XSSFWorkbook(inStr); //2007+ } else { throw new Exception("解析的文件格式有误!"); } return wb; } /** * 描述:根据文件后缀,自适应上传文件的版本 * * @param fileName * @return * @throws Exception */ public Workbook getWorkbook2007(String fileName) throws Exception { return new XSSFWorkbook(fileName); //2007+; } /** * 描述:对表格中数值进行格式化 * * @param cell * @return */ public Object getCellValue(Cell cell) { Object value = null; DecimalFormat df = new DecimalFormat("0"); //格式化number String字符 SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd"); //日期格式化 DecimalFormat df2 = new DecimalFormat("0.00"); //格式化数字 switch (cell.getCellTypeEnum()) { case STRING: value = cell.getRichStringCellValue().getString(); break; case NUMERIC: if ("General".equals(cell.getCellStyle().getDataFormatString())) { value = df.format(cell.getNumericCellValue()); } else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) { value = sdf.format(cell.getDateCellValue()); } else { value = df2.format(cell.getNumericCellValue()); } break; case BOOLEAN: value = cell.getBooleanCellValue(); break; case BLANK: value = ""; break; default: break; } return value; }}
阅读全文
0 0
- java下对excel文件的上传
- JAVA上传EXCEL文件
- Java里的下载Excel模板上传Excel文件
- java对excel文件的读写操作
- Java对Excel文件的读取
- Java对Excel文件的读取--后续
- Java对excel文件的处理
- JAVA对excel文件的处理方式
- java对excel文件的解析操作
- java操作上传Excel文件
- Java对excel文件操作
- Java SSI框架下的文件上传
- SSM下的Java上传文件
- 朝花夕拾:Java中实现对EXCEL文件的读取
- Java使用jxl对excel文件进行简单的读写
- [java后台]servlet对上传文件的处理
- java中excel的上传
- 使用maven+springmvc+POI对上传的Excel文件进行解析并操作
- linux系统下安装chrome
- 经验:《王者荣耀》技术总监分享背后技术
- VS2013搭建OpenGL环境
- axios 中文文档 翻译
- MergeSort分治实现代码(java)
- java下对excel文件的上传
- Python学习[01]
- Datatable 函数
- 转载自http://www.cnblogs.com/whgk/p/6074930.html 本文hibernate教程
- oracle 触发器
- 马云纽约演讲全文:再不改革教育,年轻人都要失业
- 【安全牛学习笔记】SQLMAP自动注入-INHECTION、DETECTION、TECHNIQUES、FINGERPRINT
- 录音机和录像机的输入通路SPELayer
- 最短路径及最大树的应用