java 导出一行多列的excel表格

@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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值