Java POI Excel的导入
来源:互联网 发布:室内定位技术 知乎 编辑:程序博客网 时间:2024/06/11 13:07
相信很多人之前都做过POI excel的导入 那么我就把我做Excel导入跟大家分享一下
public Map insertData(String path, String clazz) {@SuppressWarnings("rawtypes")List list = ReadExcelUtil.getInstance().readExcel(path);@SuppressWarnings("rawtypes")Map result = new HashMap();for (int i = 0; i < list.size(); i++) {@SuppressWarnings({ "rawtypes", "unchecked" })Map<Integer, HashMap> map = (Map) list.get(i);// 遍历for (Entry<Integer, HashMap> entry : map.entrySet()) {Map imbean = entry.getValue();List param = dealData(imbean,entry.getKey());// 数据处理result.put(entry.getKey(), param);//System.out.println("key = " + entry.getKey() + " and value = "//+ entry.getValue());}}return result;}
readExcel方法在这
POIFSFileSystem fs = null;; HSSFWorkbook wb = null;public List<Map<Integer,HashMap>> readExcel(String path) {List excelRowList = new ArrayList(); try { //同时支持Excel 2003、2007 // File excelFile = new File("/home/zht/test.xls"); //创建文件对象 FileInputStream is = new FileInputStream(path); //文件流 Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的 int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量 //遍历每个Sheet for (int s = 0; s < sheetCount; s++) { Sheet sheet = workbook.getSheetAt(s); int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数 //遍历每一行 for (int r = 3; r < rowCount; r++) { Map<Integer,HashMap> content =new LinkedHashMap<Integer,HashMap>(); Row row = sheet.getRow(r); int cellCount = row.getPhysicalNumberOfCells(); //获取总列数 //遍历每一列 HashMap rowMap = new HashMap(); for (int c = 0; c < cellCount; c++){ Cell cell = row.getCell(c); Integer cellType = cell.getCellType(); if(cellType == null){ break; } String cellValue = null; switch(cellType) { case Cell.CELL_TYPE_STRING: //文本 cellValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: //数字、日期 if(DateUtil.isCellDateFormatted(cell)) { cellValue = fmt.format(cell.getDateCellValue()); //日期型 } else { cellValue = String.valueOf(cell.getNumericCellValue()).replace(".0", ""); //数字 } break; case Cell.CELL_TYPE_BOOLEAN: //布尔型 cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_BLANK: //空白 cellValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_ERROR: //错误 cellValue = "错误"; break; case Cell.CELL_TYPE_FORMULA: //公式 cellValue = "错误"; break; default: cellValue = "错误"; } System.out.print(cellValue + " "); rowMap.put(c, cellValue); } content.put(r, (HashMap) rowMap.clone()); rowMap.clear(); excelRowList.add(content); } } } catch (Exception e) { e.printStackTrace(); } return excelRowList; }可以选择从第几行开始导入 代码中注释已经写的很清楚
阅读全文
0 0
- Java POI Excel的导入
- java poi 导入excel
- Java poi+excel导入
- java poi导入EXCEL
- java poi 导入Excel
- Java POI导入Excel
- Java POI 导入Excel
- java poi 导入excel
- Java poi+excel导入
- java poi组件Excel的导入导出
- Java之POI的excel导入导出
- java excel poi 导入导出
- JAVA导入导出EXCEL(POI)
- Java POI导入导出excel
- java poi实现excel导入
- java poi Excel文件导入
- Java POI 导入Excel以及解析Excel
- POI实现Java程序进行Excel的读取和导入
- Erlang高阶函数——递归
- 36. Valid Sudoku
- 如何用chrome调试android应用
- R语言自动提取新闻摘要的简单实现
- H5基础知识第十一课时(className属性)
- Java POI Excel的导入
- 项目中加入redis
- js数组排序
- Python---字符串的内置方法
- Qt学习笔记:QByteArray Class
- easyDialog 显示弹出框
- 多年iOS开发经验总结
- adb连接出现device not found问题解决
- gvim的配色插件设置