读取EXCEL ,EXCEL值转化,单元格样式
//读取excel public static Workbook readExcel(String filePath){ Workbook wb = null; if(filePath==null){ return null; } String extString = filePath.substring(filePath.lastIndexOf(".")); InputStream is = null; try { is = new FileInputStream(filePath); if(".xls".equals(extString)){ return wb = new HSSFWorkbook(is); }else if(".xlsx".equals(extString)){ return wb = new XSSFWorkbook(is); }else{ return wb = null; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return wb; }
//excel值转化 public static Object getCellFormatValue(Cell cell){ Object cellValue = null; if(cell!=null){ //判断cell类型 switch(cell.getCellType()){ case Cell.CELL_TYPE_NUMERIC:{ if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) { Date theDate = cell.getDateCellValue(); SimpleDateFormat dff = new SimpleDateFormat("yyyy/MM/dd"); cellValue = dff.format(theDate); }else{ DecimalFormat df=new DecimalFormat("0"); cellValue=df.format(cell.getNumericCellValue()); // cellValue = String.valueOf(cell.getNumericCellValue()); } break; } case Cell.CELL_TYPE_FORMULA:{ //判断cell是否为日期格式 if(DateUtil.isCellDateFormatted(cell)){ //转换为日期格式YYYY-mm-dd cellValue = cell.getDateCellValue(); }else{ //数字 cellValue = String.valueOf(cell.getNumericCellValue()); } break; } case Cell.CELL_TYPE_STRING:{ cellValue = cell.getRichStringCellValue().getString(); break; } default: cellValue = ""; } }else{ cellValue = ""; } return cellValue; }
/** * @功能描述 设置excel文档(多表单) * @param tName * excel表名集 * @param tHeader * excel表头数据集 * @param tValue * excel表单数据集(除表头) * @param tHeaderStyle * excel表头单元格样式 * @param tValueStyle * excel表单数据单元格样式(除表头) * @param filePath * excel文件地址 * @throws Exception * 异常往上抛出 */ public void exportExcel(ArrayList<String> tName, ArrayList<ArrayList<String>> tHeader, ArrayList<ArrayList<ArrayList<Object>>> tValue, ArrayList<Map<String, Short>> tHeaderStyle, ArrayList<Map<String, Short>> tValueStyle, OutputStream fileOutput) { try { // 当excel文档不存在时创建 workbook = new HSSFWorkbook(); // for循环完成文档各个表单的赋值和样式 for (int i = 0; i < tName.size(); i++) { this.setSheet(tName.get(i), tHeader.get(i), tValue.get(i), tHeaderStyle.get(i), tValueStyle.get(i)); // 单个表单赋值和样式 } // 导出excel文件 this.export(workbook, fileOutput); } catch (Exception e) { log.error ( "\n=========异常信息为:==========\n{}",e ); } }
/** * @功能描述 设置excel表单 * @param tName * excel表名 * @param tHeader * excel表头数据集 * @param tValue * excel表单数据集(除表头) * @param tHeaderStyle * excel表头单元格样式 * @param tValueStyle * excel表单数据单元格样式(除表头) * @throws Exception * 异常往上抛出 */ private void setSheet(String tName, ArrayList<String> tHeader, ArrayList<ArrayList<Object>> tValue, Map<String, Short> tHeaderStyle, Map<String, Short> tValueStyle) throws Exception { try { // 创建表单并设置其表名 sheet = workbook.createSheet(tName); // 创建表单行 HSSFRow tRow = sheet.createRow(0); // 赋值和样式(此时,为表头行) tRow = this.setTRow(tRow, tHeader, tHeaderStyle); // for循环完成表单数据的赋值和样式(除表头) for (int i = 0; i < tValue.size(); i++) { tRow = sheet.createRow(i + 1); // 获取表单行 tRow = this.setTRow(tRow, tValue.get(i), tValueStyle); // 设置当前行的数据和样式 } tRow = sheet.createRow(sheet.getLastRowNum() + 5); HSSFCellStyle cellStyle = workbook.createCellStyle(); HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.RED.index); cellStyle.setFont(font); HSSFCell cell = tRow.createCell(0); cell.setCellStyle(cellStyle); cell.setCellValue("注:方案基本信息无要求或无限制为-1"); } catch (Exception e) { log.error ( "\n=========异常信息为:==========\n{}",e ); } }
/** * @功能描述 设置excel表单行数据 */ @SuppressWarnings("unchecked") private HSSFRow setTRow(HSSFRow row, ArrayList tRow, Map<String, Short> tHeaderStyle) throws Exception { try { // 获取单元格样式 HSSFCellStyle cellStyle = this.setCellStyle(tHeaderStyle); // 声明单元格 HSSFCell cell = null; // for循环完成该表单某行各个列赋值和样式 for (int i = 0; i < tRow.size(); i++) { cell = row.createCell(i); // 获取每列单元格 cell.setCellStyle(cellStyle); // 设置样式 sheet.autoSizeColumn((short) i); // 设置单元格自适应 Object obj = tRow.get(i); // 获取当前列的值 // 判断对象所属类型, 并强转 if (obj instanceof Integer) { // 当数字时 cell.setCellValue((Integer) obj); } if (obj instanceof String) { // 当为字符串时 cell.setCellValue((String) obj); } if (obj instanceof Boolean) { // 当为布尔时 cell.setCellValue((Boolean) obj); } if (obj instanceof Date) { // 当为时间时 cell.setCellValue((Date) obj); } if (obj instanceof Calendar) { // 当为时间时 cell.setCellValue((Calendar) obj); } if (obj instanceof Double) { // 当为小数时 cell.setCellValue((Double) obj); } } } catch (Exception e) { log.error ( "\n=========异常信息为:==========\n{}",e ); } return row; // 返回 }
/** * @功能描述 设置单元格样式 */ private HSSFCellStyle setCellStyle(Map<String, Short> fontStyle) throws Exception { // 声明单元格样式 HSSFCellStyle cellStyle = null; // 创建单元格样式 cellStyle = workbook.createCellStyle(); try { cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框 cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框 // 创建字体 HSSFFont font = workbook.createFont(); // 设置字体颜色(红色为:HSSFFont.COLOR_RED 这里表示short类型 10) font.setColor(HSSFColor.BLACK.index); // 设置字体形体(宽体为:HSSFFont.BOLDWEIGHT_BOLD 700) -- 粗体 font.setBoldweight(fontStyle.get("weight")); // 添加字体样式 cellStyle.setFont(font); } catch (Exception e) { log.error ( "\n=========异常信息为:==========\n{}",e ); } return cellStyle; // 返回 }
/** * @功能描述 导出Excel */ private void export(HSSFWorkbook workbook, OutputStream fileOutput) throws Exception { try { workbook.write(fileOutput); } catch (FileNotFoundException e) { log.error ( "\n=========异常信息为:==========\n{}",e ); } catch (Exception e) { log.error ( "\n=========异常信息为:==========\n{}",e ); } finally { try { // 关闭流, 释放资源 fileOutput.close(); } catch (IOException e) { log.error ( "\n=========异常信息为:==========\n{}",e ); } } }