验证中...
Languages: Java
Categories: Web开发技术
Latest update 2019-03-25 09:49
因为有多个专家进行评议所以需要每个专家作为一行但是专家前面的项目内容相同的就需要进行数据合并
Raw Copy
public void exportProject (HttpServletResponse response,String name, String code) throws Exception
// 设置表头
String[] title = {"项目名称","编号","介绍","评议状态","评议专家'"};
String[] fileds = {"name","code","introduce","appraisalStatusScrip"};
// 得到数据
ProjectVO projectVO = new ProjectVO();
projectVO.setName(name);
projectVO.setCode(code);
List<ProjectVO> projectVOS = projectService.searchAllProject(projectVO);
// 数据处理
String[][] content = new String[projectVOS.size()][];
for (int m = 0; m < projectVOS.size(); m++) {
content[m] = new String[6];
ProjectVO obj = projectVOS.get(m);
content[m][0] = obj.getName();
content[m][1] = obj.getCode();
content[m][2] = obj.getIntroduce();
content[m][3] = obj.getAppraisalStatusScrip();
content[m][4] = obj.getAppraisalExpert();
content[m][5] = obj.getId();
}
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = hssfWorkbook.createSheet("项目详情");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 创建居中格式
// 声明列对象
HSSFCell cell = null;
// 创建表头
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(cellStyle);
}
// 第五步,创建内容
for (int j = 0; j < content.length; j++) {
row = sheet.createRow(j + 1);
for (int n = 0; n < content[j].length-1; n++) {// 减一避免显示id
row.createCell(n).setCellValue(content[j][n]);
}
}
// 计算单元格合并的位置
List<String> keyList = new ArrayList<>();
Map<String,Integer> sortMap = new HashMap<>();
for (int i = 0; i < content.length; i++) {
String key = content[i][5];
if (keyList.indexOf(key) < 0) {
keyList.add(key);
}
if(sortMap.containsKey(key)){// 判断该值是否存在
sortMap.put(key,(sortMap.get(key) + 1)); // 存在则累加
} else {
sortMap.put(key,1);// 不存在则添加为1
}
}
// 合并单元格
int startRow = 1;
for (int a = 0; a < keyList.size(); a++) {
String fieldNameValue = keyList.get(a);
// map是无序的可能导致取出数据顺序和放入的不一致,影响合并的结果。用一个数组来确保取出顺序和放入的一致
for (Map.Entry<String,Integer> entry: sortMap.entrySet()) {
String key = entry.getKey();
if (fieldNameValue.equals(key)) {
int count = entry.getValue() * 1+startRow-1;
// 合并要合并的字段
for (int b = 0; b < fileds.length; b++) {
/**起始行号,终止行号, 起始列号,终止列号*/
CellRangeAddress rangeAddress = new CellRangeAddress(startRow,count,b,b);
sheet.addMergedRegion(rangeAddress);
}
startRow += count;
break;
}
}
}
// 导出并下载
OutputStream out = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("UTF-8");
String excelName = "项目信息表";
response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gbk"), "ISO8859-1" )+".xls");
hssfWorkbook.write(out);
out.flush();
out.close();
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search