POI解析excel表格数据例子
来源:互联网 发布:列宾美院知乎 编辑:程序博客网 时间:2024/06/05 07:47
例子包含解析2003及之前版本和2007及之后版本。
/** * @author Yuansheng.Lei * excel表格导入工具类 */public class PoiExcel { public static List<Dto> getDataByPoi(String filePath){ Workbook wb = null; InputStream stream = null; Sheet sheet = null; try { stream = new FileInputStream(filePath); if (filePath.endsWith(".xls")) { wb = (Workbook)new HSSFWorkbook(stream); }else if (filePath.endsWith(".xlsx")) { wb = (Workbook)new XSSFWorkbook(stream); } sheet = wb.getSheetAt(0); }catch (Exception e) { e.printStackTrace(); }finally{ if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } List<Dto> list = null; if( sheet != null ){ int count_row = sheet.getLastRowNum();//获取最后一行行数 int count_cell= sheet.getRow(1).getPhysicalNumberOfCells();//获取第一行单元格数 Row row = null; Cell cell = null; list = new ArrayList<Dto>(); for (int i = 1; i <= count_row; i++) { Dto dto = Dtos.newDto(); row = sheet.getRow((short)i); for (int j = 0; j < count_cell; j++) { cell = row.getCell((short)j); dto.put("a"+(j+1), getValue(cell)); } list.add(dto); } } return list; } @SuppressWarnings("deprecation") public static String getValue(Cell cell){ String cellvalue = ""; if(cell != null){ switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: cellvalue = cell.getRichStringCellValue().getString(); //获取字符串 break; case Cell.CELL_TYPE_FORMULA: case Cell.CELL_TYPE_NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); cellvalue = sdf.format(cell.getDateCellValue()); //获取时间 }else if(cell.getCellStyle().getDataFormatString().indexOf("%") != -1){ ellvalue = (new DecimalFormat("0").format(cell.getNumericCellValue()*100))+"%"; //获取带%的百分比 }else { cellvalue = String.valueOf(new DecimalFormat("0").format(cell.getNumericCellValue())); //获取数字 } break; case Cell.CELL_TYPE_BOOLEAN: cellvalue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_BLANK: cellvalue = ""; break; default : cellvalue = ""; break; } } return cellvalue; }}
相关jar包如下:
0 0
- POI解析excel表格数据例子
- POI解析excel表格
- POI导出Excel表格例子
- 使用poi解析excel表格
- POI 解析Excel表格实例
- Poi解析对比excel表格
- POI解析excel表格导入数据库
- java通过POI解析Excel表格内容
- java poi 解析excel数据
- 使用 POI 读取 Excel 表格数据
- java poi读取excel表格数据
- 基于POI的Excel表格数据导出
- POI生成excel表格——简单例子
- Java 通过Poi解析数据到EXCEL
- POI操作Excel表格
- POI操作Excel表格
- poi导出excel表格
- POI读取excel表格
- maven相关
- 静下心来写点东西
- 《JS高程(3)》DOM节点层次Document类型-第10章笔记(12)
- SqlServer 统计 MVC 每个 Action 的 相应时间
- angular学习(九)—— Interpolation
- POI解析excel表格数据例子
- nova组件-launch,shut off,start,lock,reboot,terminate虚拟机
- Array类型中的栈方法
- java io
- window下node提示Error: Cannot find module "***"的解决办法
- 学习笔记-【Quick-Cocos2d-x初学者游戏教程】
- curl模拟post请求提交
- ArrayBlockingQueue和LinkedBlockingQueue的区别及使用
- Hadoop运维工程师专家之路--第二章Hadoop架构简介