使用POI解析excel文件方法总结
来源:互联网 发布:阿里数据平台有哪些 编辑:程序博客网 时间:2024/06/04 19:29
在做项目时 ,用到了 POI 解析 excel的 功能,项目中只是对导入的第一列的数据做了处理,也就是代码中的
rows.getCell(0);如果需要对确定列数进行解析时,可以依次递增0,1,2,3,4......
</pre><pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">private List<Model> parseExcel(InputStream is)</span>
throws IReadException { List<PromotionBlackBatchModel> batchInfoList = null; if (null != is) { try { try { batchInfoList = new ArrayList<PromotionBlackBatchModel>(); POIFSFileSystem pss = new POIFSFileSystem(is); HSSFWorkbook workbook = new HSSFWorkbook(pss); HSSFSheet sheet = workbook.getSheetAt(0); Iterator<?> rit = sheet.rowIterator(); int flag = 0; while (rit.hasNext()) { HSSFRow rows = (HSSFRow)rit.next(); // 读取第一列字段名 if (rows.getCell(0) != null) { String blContent = null; HSSFCell title = rows.getCell(0); blContent = String.valueOf(title).trim(); if (0 == flag) { flag++; continue; } DecimalFormat df = new DecimalFormat("#"); // 如果单元格是数字格式 if (rows.getCell(0).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { blContent = df.format(rows.getCell(0).getNumericCellValue()); } else if (rows.getCell(0).getCellType() == HSSFCell.CELL_TYPE_STRING) { blContent = rows.getCell(0).getRichStringCellValue().getString(); } <pre name="code" class="java"><span style="white-space:pre"></span> Model <span style="font-family: Arial, Helvetica, sans-serif;">model = new Model();</span>model.setBlContent(blContent); batchInfoList.add(model); } } } finally { is.close(); } } catch (IOException e) { logger.error(e); } } logger.info("Parse Excel Data End... Import Data=" + batchInfoList.size()); return batchInfoList; }
POI常用的方法:
1.得到总行数:
int rowNum = sheet.getLastRowNum();
2.标题总列数
// 标题总列数 int colNum = row.getPhysicalNumberOfCells();
3.获取单元格数据内容为字符串类型:(对excel中不同数据类型做统一处理成为String类型)
/** * 获取单元格数据内容为字符串类型的数据 * * @param cell Excel单元格 * @return String 单元格数据内容 */ private String getStringCellValue(HSSFCell cell) { String strCell = ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: strCell = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: strCell = String.valueOf(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN: strCell = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_BLANK: strCell = ""; break; default: strCell = ""; break; } if (strCell.equals("") || strCell == null) { return ""; } if (cell == null) { return ""; } return strCell; }
4.根据HSSFCell类型设置数据
/** * 根据HSSFCell类型设置数据 * @param cell * @return */ private String getCellFormatValue(HSSFCell cell) { String cellvalue = ""; if (cell != null) { // 判断当前Cell的Type switch (cell.getCellType()) { // 如果当前Cell的Type为NUMERIC case HSSFCell.CELL_TYPE_NUMERIC: case HSSFCell.CELL_TYPE_FORMULA: { // 判断当前的cell是否为Date if (HSSFDateUtil.isCellDateFormatted(cell)) { // 如果是Date类型则,转化为Data格式 //方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00 //cellvalue = cell.getDateCellValue().toLocaleString(); //方法2:这样子的data格式是不带带时分秒的:2011-10-12 Date date = cell.getDateCellValue(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); cellvalue = sdf.format(date); } // 如果是纯数字 else { // 取得当前Cell的数值 cellvalue = String.valueOf(cell.getNumericCellValue()); } break; } // 如果当前Cell的Type为STRIN case HSSFCell.CELL_TYPE_STRING: // 取得当前的Cell字符串 cellvalue = cell.getRichStringCellValue().getString(); break; // 默认的Cell值 default: cellvalue = " "; } } else { cellvalue = ""; } return cellvalue; }
5.获取单元格数据内容为日期类型的数据
/** * 获取单元格数据内容为日期类型的数据 * * @param cell * Excel单元格 * @return String 单元格数据内容 */ private String getDateCellValue(HSSFCell cell) { String result = ""; try { int cellType = cell.getCellType(); if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { Date date = cell.getDateCellValue(); result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate(); } else if (cellType == HSSFCell.CELL_TYPE_STRING) { String date = getStringCellValue(cell); result = date.replaceAll("[年月]", "-").replace("日", "").trim(); } else if (cellType == HSSFCell.CELL_TYPE_BLANK) { result = ""; } } catch (Exception e) { System.out.println("日期格式不正确!"); e.printStackTrace(); } return result; }
0 0
- 使用POI解析excel文件方法总结
- 使用POI生成excel文件方法总结
- java使用poi解析Excel文件
- POI解析excel文件
- poi解析excel文件
- 使用poi解析Excel
- 使用poi解析Excel
- 使用poi解析Excel
- 使用poi解析Excel
- 使用poi解析Excel
- 使用poi解析Excel
- 使用POI解析Excel
- Java中使用Apache POI使用解析excel文件
- 用POI解析EXCEL文件
- POI包解析Excel文件
- POI API 解析Excel文件
- java+poi解析excel文件
- Apache POI解析Excel文件
- bat批处理笔记——修改指定类型文件内指定内容
- Android照片墙完整版,完美结合LruCache和DiskLruCache
- Page Rendering - Main Frame and Impl Frame
- 多线程——线程同步互斥(synchronized)
- python学习之--ubuntu下 如何安装第三方库的简要方法
- 使用POI解析excel文件方法总结
- 记录下首次blog开通的想法
- 2016/01/28 Head First Java 第二节
- iOS解析xml
- bat批处理笔记——删除指定类型文件指定前缀名
- js对象转json字符串,并保留js对象中的javascript脚本
- 二分图匹配
- Commando War uva11729(贪心)
- [转载]深入浅出UML类图