list输出为xls 随笔

xiaoxiao2026-08-03  14

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

转载请注明原文地址: https://www.6miu.com/read-5050625.html

最新回复(0)