public void exportExcel(List<Rule> ruleList) { String[] title = { "名字", "规则组Id", "渠道", "规则版本号", "规则备注", "规则内容", "操作人", "创建时间", "更新时间" }; // 创建一个工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个工作表sheet HSSFSheet sheet = workbook.createSheet(); // 设置列宽 setColumnWidth(sheet, 9); // 创建第一行 HSSFRow row = sheet.createRow(0); // 创建一个单元格 HSSFCell cell = null; // 创建表头 for (int i = 0; i < title.length; i++) { cell = row.createCell(i); // 设置样式 HSSFCellStyle cellStyle = workbook.createCellStyle(); // cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 设置字体居中 cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中 // 设置字体 HSSFFont font = workbook.createFont(); font.setFontName("宋体"); // font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗 // font.setFontHeight((short)12); font.setFontHeightInPoints((short) 13); cellStyle.setFont(font); cell.setCellStyle(cellStyle); cell.setCellValue(title[i]); } for (int i = 1; i < (ruleList.size() + 1); i++) { // 创建第i行 HSSFRow nextRow = sheet.createRow(i); for (int j = 0; j < 9; j++) { Rule rule= ruleList.get(i-1); //获得查出来的集合中的对象 HSSFCell cell2 = nextRow.createCell(j); if (j == 0) { cell2.setCellValue((rule.getName())); //获取字段 } if (j == 1) { cell2.setCellValue((rule.getGroupId()!=null)?rule.getGroupId():1); } if (j == 2) { cell2.setCellValue((rule.getChannel()!=null)?rule.getChannel():null); } if (j == 3) { cell2.setCellValue(rule.getVersion()); } if (j == 4) { cell2.setCellValue((rule.getRemarks()!=null)?rule.getRemarks():null); } if (j == 5) { cell2.setCellValue((rule.getContentList()!=null&&rule.getContentList().size()>0)? JSONArray.toJSONString(rule.getContentList()):null); } if (j == 6) { cell2.setCellValue((rule.getOperator()!=null)?rule.getOperator():null); } if (j == 7) { cell2.setCellValue((rule.getCreateTime()!=null)?rule.getCreateTime():null); } if (j == 8) { cell2.setCellValue(rule.getUpdateTime()); } } } // 创建一个文件 File file = new File("E:/1.xls"); try { file.createNewFile(); // 打开文件流 FileOutputStream outputStream = FileUtils.openOutputStream(file); workbook.write(outputStream); outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 设置列宽() private static void setColumnWidth(HSSFSheet sheet, int colNum) { for (int i = 0; i < colNum; i++) { int v = 0; v = Math.round(Float.parseFloat("15.0") * 37F); v = Math.round(Float.parseFloat("20.0") * 267.5F); sheet.setColumnWidth(i, v); } }