/** * 将Excel样式及数据一并导入数据库 * @param param * @return 是否成功标示 * 创建人:王炫皓 * 创建时间:2016/7/26 */ @Override public HashMap ExeclReportImportData(HashMap param) { int sheetNumber = Integer.parseInt(param.get("sheetNumber")+""); //excel 页号 int reportNumber = Integer.parseInt(param.get("reportNumber")+""); // 报表内码 HashMap rowHashMap ; //封装结果集 List excelDatalist = new ArrayList(); String iyea = param.get("iyear")+""; //年度 String filepath = "";//完整路径 String filename = param.get("filename") + "";//文件名称 String url = this.getClass().getResource("").getFile(); url = url.replaceAll("%20", " "); // File fpath = new File(url.substring(0, url.indexOf("webapps")) + "webapps/importdata/"); File fpath = new File("E:/apache-tomcat-7.0.69/webapps/importdata"); if (fpath.exists() == false) { fpath.mkdir(); System.out.println("路径不存在,但是已经成功创建了:->" + fpath); return null; } filepath = fpath + "/" + filename; Workbook workbook = null;// 以文件流的方式读取文件 try { //获取工作簿 workbook = new HSSFWorkbook(new POIFSFileSystem( new FileInputStream(filepath))); //根据下标获取 工作表 Sheet sheet = workbook.getSheetAt(sheetNumber); //总行数 int rowNum = sheet.getLastRowNum(); for (int i = 0; i <= rowNum; i++) { rowHashMap = new HashMap(); //根据下标获取行 Row row = sheet.getRow(i); rowHashMap.put("icellrow", row.getRowNum()); //单元格行号 rowHashMap.put("icellrowheight",row.getHeightInPoints()); //遍历所有列 for (int j = 1; j<=row.getLastCellNum(); j++){ //根据下标获取列 Cell cl = row.getCell(j); if(cl != null){ CellStyle cs = cl.getCellStyle(); /* *背景色 */ Color bgcolor = cs.getFillBackgroundColorColor(); rowHashMap.put("icellbackcolor", ToolUtil.renderColor(bgcolor)) ; /* * 字体颜色 */ Color fontcolor = cs.getFillForegroundColorColor(); rowHashMap.put("icellfontcolor", ToolUtil.renderColor(fontcolor)) ; /* * 字体样式 */ Font font = workbook.getFontAt( cs.getFontIndex()); rowHashMap.put("icellfont", font.getFontName()); //行高 rowHashMap.put("icellfontsize",font.getFontHeightInPoints()); //单元格式 rowHashMap.put("icelltype",cl.getCellType()); //单元格值 rowHashMap.put("icellvalue",cl.getStringCellValue()); //单元格列号 rowHashMap.put("icellcol", cl.getColumnIndex()); //列名 rowHashMap.put("icellname", CellReference.convertNumToColString(j)); //列宽 sheet.getColumnWidth(in units of 1/256th of a character width ) rowHashMap.put("icellcolwidth",sheet.getColumnWidth(cl.getColumnIndex())/256); excelDatalist.add(rowHashMap); } } } } catch (IOException e) { e.printStackTrace(); } return null; }
转载于:https://my.oschina.net/wxh1989/blog/715406