开心最大

将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);
  }


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012581322/article/details/76457711
个人分类: ********@java实战
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭