`
juzhibest
  • 浏览: 55270 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

list输出为xls 随笔

阅读更多

 public ActionForward expResultForExcel(ActionMapping mapping, ActionForm form,
                                       HttpServletRequest request,
                                       HttpServletResponse response) {
        String user_dm = authenticator.getUserDM(request.getSession());
        String actid = request.getParameter("actid") != null ?
                       request.getParameter("actid") : "";
        String sid = sort.getSchool_id(user_dm);
        List list = sort.getSortInfoForExcel(actid);
        try {
            File file=new File(System.currentTimeMillis()+".xls");
            WritableWorkbook wwb = Workbook.createWorkbook(file); //此处建立路径
            jxl.write.WritableSheet ws = wwb.createSheet("Sheet1", 0); //建立工作簿
            for (int i = 0; i < list.size(); i++) { //代表行
                Map map=(Map)list.get(i);
                Set set=map.keySet();
                Iterator iterator = set.iterator();
                int j = 0; //j代表列
                while (iterator.hasNext()) {
                    Object test = (Object) iterator.next();
                    String str="";
                    try{
                        str=map.get(test).toString();
                    }catch(Exception e){
                    }
                    jxl.write.Label label1 = new jxl.write.Label(j, i,
                            str); //建立第一列
                    ws.addCell(label1); //放入工作簿
                    j++;
                }
            }
            //写入Exel工作表
            wwb.write();
            //关闭Excel工作薄对象
            wwb.close();
            response.setHeader("Content-disposition","attachment; filename=result.xls");
            response.setContentType("application/msexcel");
            InputStream in=new FileInputStream(file);
            OutputStream out=response.getOutputStream();

            int bytesRead = 0;
            byte[] buffer = new byte[8192]; //设置缓冲大小
            while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            out.close();
            file.delete();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics