@RequestMapping("export")
public void export(HttpServletRequest request, HttpServletResponse response,Integer areaId ,GoldSendVo filter ,String fafangTimeString) throws IOException {
response.setContentType("octets/stream");
String fileName = "name.xls";
fileName = URLEncoder.encode(fileName, "UTF-8");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
String[] headers = {"区域", "80-90岁","90-94岁","95-99岁","100岁及以上","其中","合计"};
String[] headers1 = {"","人次数", "金额","人次数", "金额","人次数", "金额","人次数", "金额","省财政" ,"市财政", "区县财政","镇财政 ","人次数","金额"};
OutputStream out = response.getOutputStream();
// 声明一个工作薄
SXSSFWorkbook workbook = new SXSSFWorkbook(5000);
// 生成一个表格
Sheet sheet = workbook.createSheet();
workbook.setSheetName(0, "name");
CellStyle cellStyle = workbook.createCellStyle();
// 居中
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
// 居中
cellStyle.setAlignment(CellStyle.VERTICAL_CENTER);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
sheet.setDefaultColumnStyle(0,cellStyle);
// first row
// last row
// first column
// last column
CellRangeAddress region = new CellRangeAddress(0,0,1,2);
sheet.addMergedRegion(region);
CellRangeAddress region1 = new CellRangeAddress(0,0,3,4);
sheet.addMergedRegion(region1);
CellRangeAddress region2 = new CellRangeAddress(0,0,5,6);
sheet.addMergedRegion(region2);
CellRangeAddress region3 = new CellRangeAddress(0,0,7,8);
sheet.addMergedRegion(region3);
CellRangeAddress region4 = new CellRangeAddress(0,0,9,12);
sheet.addMergedRegion(region4);
CellRangeAddress region5 = new CellRangeAddress(0,0,13,14);
sheet.addMergedRegion(region5);
//第一行
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
HSSFRichTextString text = new HSSFRichTextString(headers[0]);
cell.setCellValue(text);
cell = row.createCell(1);
text = new HSSFRichTextString(headers[1]);
cell.setCellValue(text);
cell = row.createCell(3);
text = new HSSFRichTextString(headers[2]);
cell.setCellValue(text);
cell = row.createCell(5);
text = new HSSFRichTextString(headers[3]);
cell.setCellValue(text);
cell = row.createCell(7);
text = new HSSFRichTextString(headers[4]);
cell.setCellValue(text);
cell = row.createCell(9);
text = new HSSFRichTextString(headers[5]);
cell.setCellValue(text);
cell = row.createCell(13);
text = new HSSFRichTextString(headers[6]);
cell.setCellValue(text);
//合并的第二行
row =sheet.createRow(1);
for (short i = 0; i < headers1.length; i++) {
if (i == 0) {
i++;
}
cell = row.createCell(i);
HSSFRichTextString text1 = new HSSFRichTextString(headers1[i]);
cell.setCellValue(text1);
}
List<GoldSendVo> result =statistics(areaId,filter,fafangTimeString);//获取统计的数据
//循环遍历数据,把值添加到表格里
int flag =2;
for(GoldSendVo g : result){
row = sheet.createRow(flag);
cell = row.createCell(0);
text = new HSSFRichTextString(g.getAreaName());
cell.setCellValue(text);
cell = row.createCell(1);
text = new HSSFRichTextString(g.getCountPer1().toString());
cell.setCellValue(text);
cell = row.createCell(2);
text = new HSSFRichTextString(g.getCountMoney1().toString());
cell.setCellValue(text);
cell = row.createCell(3);
text = new HSSFRichTextString(g.getCountPer2().toString());
cell.setCellValue(text);
cell = row.createCell(4);
text = new HSSFRichTextString(g.getCountMoney2().toString());
cell.setCellValue(text);
cell = row.createCell(5);
text = new HSSFRichTextString(g.getCountPer3().toString());
cell.setCellValue(text);
cell = row.createCell(6);
text = new HSSFRichTextString(g.getCountMoney3().toString());
cell.setCellValue(text);
cell = row.createCell(7);
text = new HSSFRichTextString(g.getCountPer4().toString());
cell.setCellValue(text);
cell = row.createCell(8);
text = new HSSFRichTextString(g.getCountMoney4().toString());
cell.setCellValue(text);
cell = row.createCell(9);
text = new HSSFRichTextString("0");
cell.setCellValue(text);
cell = row.createCell(10);
text = new HSSFRichTextString("0");
cell.setCellValue(text);
cell = row.createCell(11);
text = new HSSFRichTextString("0");
cell.setCellValue(text);
cell = row.createCell(12);
text = new HSSFRichTextString("0");
cell.setCellValue(text);
cell = row.createCell(13);
text = new HSSFRichTextString(g.getAllCount().toString());
cell.setCellValue(text);
cell = row.createCell(14);
text = new HSSFRichTextString(g.getAllMoney().toString());
cell.setCellValue(text);
flag++;
}
if (out != null) {
try {
workbook.write(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//统计方法
@RequestMapping(value = "getStatistic", method = RequestMethod.POST)
@ResponseBody
public List<GoldSendVo> statistics(Integer areaId ,GoldSendVo filter ,String fafangTimeString) {
if(fafangTimeString != null){
try {
filter.setFafangTime(df.parse(fafangTimeString));
} catch (ParseException e) {
e.printStackTrace();
}
}
List<GoldSendVo> result = goldSendService.statistics(areaId ,filter);
return result;
}