将datagrid表格数据导出为Excel(动态列)

需求】:当datagrid表格显示的每列的字段名称并不固定,如要求每列字段为日期,这样的话,我们不能从新定义实体来接收这个字段,因为不同的条件,后台返回的数据的时间也不会固定,而这时用常用的导出Excel的工具就会遇到些麻烦。

解决】:(直接上代码)

  @RequestMapping(value={"/scheduleExport"}, method={org.springframework.web.bind.annotation.RequestMethod.POST, org.springframework.web.bind.annotation.RequestMethod.GET})
  public void scheduleExport(Integer rowCount, Integer current, String keyword, Pagination page, Model model, HttpServletRequest request, HttpServletResponse response)
  {
    page.clearSorts();
    Map params = new HashMap();
    params.put("param1", this.strScript);
    params.put("param2", page);
    List listResult = this.zHGLGLOBALSEARCHService.searchSqlList(params);
    List list1key = new ArrayList();
    list1key = GetMapKey(listResult);

    WritableWorkbook wwb = null;
    OutputStream os = null;
    try {
      os = response.getOutputStream();
      wwb = Workbook.createWorkbook(os);
      WritableSheet ws = wwb.createSheet("全局搜索", 0);

      int size = list1key.size();
      String[] heads = (String[])list1key.toArray(new String[size]);
      insertHead(ws, heads);

      int rownum = 1;
      if ((listResult != null) && (listResult.size() > 0)) {
        for (int i = 0; i < listResult.size(); i++) {
          Map schedule = (Map)listResult.get(i);
          for (int j = 0; j < heads.length; j++) {
            insertCell(ws, null, rownum, j, schedule.get(list1key.get(j)).toString());
          }

          rownum++;
        }

      }

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      response.setContentType("application/msexcel");
      response.addHeader("Content-disposition", "attachment;filename=schedule_" + sdf.format(new Date()) + ".xls");
      wwb.write();
    } catch (Exception e) {
      e.printStackTrace();
      try
      {
        if (wwb != null) wwb.close();
        if (os != null) os.close(); 
      }
      catch (Exception e) { e.printStackTrace(); }

    }
    finally
    {
      try
      {
        if (wwb != null) wwb.close();
        if (os != null) os.close(); 
      }
      catch (Exception e) { e.printStackTrace(); }
    }
  }

  private static void insertHead(WritableSheet ws, String[] heads) throws Exception
  {
    WritableFont font1 = new WritableFont(WritableFont.TIMES, 
      12, WritableFont.BOLD);
    WritableCellFormat format1 = new WritableCellFormat(font1);
    format1.setAlignment(Alignment.CENTRE);

    for (int i = 0; i < heads.length; i++)
    {
      WritableCell cell = new Label(i, 0, heads[i]);
      cell.setCellFormat(format1);
      ws.addCell(cell);
    }
  }

  private static void insertCell(WritableSheet ws, WritableCellFormat format1, int row, int column, String name)
    throws WriteException, RowsExceededException
  {
    WritableCell cell = new Label(column, row, name);
    if (format1 != null) {
      cell.setCellFormat(format1);
    }
    ws.addCell(cell);
  }


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值