/** * * @param out 输出流 * @param maplist 数据 * @param title 标题 * @param headers 表头 * @param keys 表头对应的字段名 * @return */ public static boolean getExcelDao(OutputStream out, List<Map<String, Object>> maplist, String title, String[] headers,String keys[]) { try { // 创建一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个带有名称的工作页 HSSFSheet sheet = workbook.createSheet(title); sheet.setDefaultColumnWidth(25); // 生成一个样式 HSSFCellStyle style = workbook.createCellStyle(); // 设置这些样式 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 生成一个字体 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 style.setFont(font); // 生成并设置另一个样式 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一个字体 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style2.setFont(font2); // 产生坐标行 // 标题行 HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellStyle(style); cell.setCellValue(title); // 使标题居中,合并单元格 四个参数 起始行列 结束行列 if (headers != null && headers.length > 0) sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headers.length - 1)); else sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1)); // 产生表头 row = sheet.createRow(1); for (int i = 0; i < headers.length; i++) { cell = row.createCell(i); cell.setCellStyle(style); cell.setCellValue(headers[i]); } // 产生内容 for (int i = 0; i < maplist.size(); i++) { Map<String, Object> objectmap = maplist.get(i); row = sheet.createRow(i + 2); int count = 0;//遍历MAP for(String key:keys){ cell = row.createCell(count); cell.setCellStyle(style2); cell.setCellValue(String.valueOf(objectmap.get(key)).equals("null")?"":objectmap.get(key)+""); count++; } /*for (Entry<String, Object> entry : objectmap.entrySet()) { // System.out.println("key= " + entry.getKey() + // " and value= " + entry.getValue()); cell = row.createCell(count); cell.setCellValue(entry.getValue().toString()); count++; }*/ } try { workbook.write(out); return true; } catch (IOException e) { e.printStackTrace(); return false; }finally { workbook.close(); } } catch (Exception e) { e.printStackTrace(); return false; } } 使用
String keys[]={"id","mname","o3title","num","userid","useraddress","Getphone","postname","postage","flagname","buildtime","kjtime"}; String headers[]={"订单号","品类","商品名称","夺宝次数","用户ID","地址","号码","快递","快递单号","订单状态","下单时间","开奖时间"}; //orderServiceImpl.query_queryListAll(query, columns, tablename, sort) //System.out.println(maplist.toString()); OutputStream outputStream = response.getOutputStream(); ExcelUtil.getExcelDao(outputStream, maplist2, title, headers, keys); outputStream.close();
时间: 2024-11-05 18:43:50