poi解析Excel
来源:互联网 发布:淘宝运费险能赔付多少 编辑:程序博客网 时间:2024/05/21 11:28
/** * 对外提供读取excel 的方法 * */ public static List<List<Object>> readExcel(File file) throws IOException { String fileName = file.getName(); String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName .substring(fileName.lastIndexOf(".") + 1); if ("xls".equals(extension)) { return read2003Excel(file); } else if ("xlsx".equals(extension)) { return read2007Excel(file); } else { throw new IOException("不支持的文件类型"); } } /** * 读取 office 2003 excel * * @throws IOException * @throws FileNotFoundException */ private static List<List<Object>> read2003Excel(File file) { List<List<Object>> list = new LinkedList<List<Object>>(); try{ HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file)); HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(hwb); //HSSFSheet sheet = null; HSSFSheet sheet = hwb.getSheetAt(0); Object value = null; HSSFRow row = null; HSSFCell cell = null; int counter = 0;// for (int m = 0; m< hwb.getNumberOfSheets() ;m++){// sheet=hwb.getSheetAt(m);// if(sheet == null){// continue;// } for (int i = sheet.getFirstRowNum(); counter < sheet .getPhysicalNumberOfRows(); i++) { row = sheet.getRow(i); if (row == null) { continue; } else { counter++; } List<Object> linked = new LinkedList<Object>();// for (int j = row.getFirstCellNum(); j <= 17; j++) { for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) { cell = row.getCell(j); if (cell == null) { continue; } DecimalFormat df = new DecimalFormat("0");// 格式化 number String // 字符 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd");// 格式化日期字符串 switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if ("@".equals(cell.getCellStyle().getDataFormatString())) { value = df.format(cell.getNumericCellValue()); } else if ("General".equals(cell.getCellStyle() .getDataFormatString())) { value = cell.getNumericCellValue(); } else { value = sdf.format(HSSFDateUtil.getJavaDate(cell .getNumericCellValue())); } break; case XSSFCell.CELL_TYPE_BOOLEAN: value = cell.getBooleanCellValue(); break; case XSSFCell.CELL_TYPE_BLANK: value = ""; break; case XSSFCell.CELL_TYPE_FORMULA: //公式型 try { CellValue cellValue; cellValue = evaluator.evaluate(cell); switch (cellValue.getCellType()) { //判断公式类型 case XSSFCell.CELL_TYPE_BOOLEAN: value = String.valueOf(cellValue.getBooleanValue()); break; case XSSFCell.CELL_TYPE_NUMERIC: // 处理日期 if (DateUtil.isCellDateFormatted(cell)) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = cell.getDateCellValue(); value = format.format(date); } else { value = String.valueOf(cellValue.getNumberValue()); } break; case XSSFCell.CELL_TYPE_STRING: value = cellValue.getStringValue(); break; case XSSFCell.CELL_TYPE_BLANK: value = ""; break; case XSSFCell.CELL_TYPE_ERROR: value = ""; break; case XSSFCell.CELL_TYPE_FORMULA: value = ""; break; } } catch (Exception e) { value = cell.getStringCellValue().toString(); cell.getCellFormula(); } break; default: value = cell.toString(); } if (value == null || "".equals(value)) { value = ""; } linked.add(value); } list.add(linked); } }catch (IOException e) { e.printStackTrace(); } return list; } /** * 读取Office 2007 excel * */ private static List<List<Object>> read2007Excel(File file) { List<List<Object>> list = new LinkedList<List<Object>>(); // 构造 XSSFWorkbook 对象,strPath 传入文件路径 try{ XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file)); XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(xwb); // 读取第一章表格内容 XSSFSheet sheet = xwb.getSheetAt(0); Object value = null; XSSFRow row = null; XSSFCell cell = null; int counter = 0; for (int i = sheet.getFirstRowNum(); counter < sheet .getPhysicalNumberOfRows(); i++) { row = sheet.getRow(i); if (row == null) { continue; } else { counter++; } List<Object> linked = new LinkedList<Object>();// for (int j = row.getFirstCellNum(); j <= 17; j++) { for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) { cell = row.getCell(j); if (cell == null) { continue; } DecimalFormat df = new DecimalFormat("0");// 格式化 number String // 字符 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd");// 格式化日期字符串 switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if ("@".equals(cell.getCellStyle().getDataFormatString())) { value = df.format(cell.getNumericCellValue()); } else if ("General".equals(cell.getCellStyle() .getDataFormatString())) { value = cell.getNumericCellValue(); } else { value = sdf.format(HSSFDateUtil.getJavaDate(cell .getNumericCellValue())); } break; case XSSFCell.CELL_TYPE_BOOLEAN: value = cell.getBooleanCellValue(); break; case XSSFCell.CELL_TYPE_BLANK: value = ""; break; case XSSFCell.CELL_TYPE_FORMULA: //公式型 try { CellValue cellValue; cellValue = evaluator.evaluate(cell); switch (cellValue.getCellType()) { //判断公式类型 case XSSFCell.CELL_TYPE_BOOLEAN: value = String.valueOf(cellValue.getBooleanValue()); break; case XSSFCell.CELL_TYPE_NUMERIC: // 处理日期 if (DateUtil.isCellDateFormatted(cell)) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = cell.getDateCellValue(); value = format.format(date); } else { value = String.valueOf(cellValue.getNumberValue()); } break; case XSSFCell.CELL_TYPE_STRING: value = cellValue.getStringValue(); break; case XSSFCell.CELL_TYPE_BLANK: value = ""; break; case XSSFCell.CELL_TYPE_ERROR: value = ""; break; case XSSFCell.CELL_TYPE_FORMULA: value = ""; break; } } catch (Exception e) { value = cell.getStringCellValue().toString(); cell.getCellFormula(); } break; default: value = cell.toString(); } if (value == null || "".equals(value)) { value = ""; } linked.add(value); } list.add(linked); } }catch (IOException e) { e.printStackTrace(); } return list; }
0 0
- POI解析Excel
- POI 解析Excel
- 使用poi解析Excel
- 使用poi解析Excel
- 使用poi解析Excel
- 使用poi解析Excel
- java poi 解析excel
- POI解析Excel
- 《POI解析复杂Excel》
- poi 解析excel
- 使用poi解析Excel
- 使用poi解析Excel
- apache poi解析excel
- POI解析Excel文档
- POI解析excel
- poi解析excel
- POI解析excel文件
- POI解析Excel
- 华为(HuaWei)虚拟按键的判断和监听
- 区块链 入门 基础知识
- 面向对象三大特征之一封装性
- android记录用户是否第一次进入apk
- Android Studio导入第三方jar包或依赖工程的方法
- poi解析Excel
- spring框架学习(二)依赖注入
- VS开发C++代码格式化设置(AStyle)
- 使用hexo+github免费搭建个人博客网站超详细教程
- 不带头结点的链表大例题
- UVA 1590 IP Networks 简单模拟
- CSDN博客如何转载
- [生存志] 第117节 九章算术均徭赋
- 1.18