生成excel表格并下载

工作需要生成excel并下载。

jsp:<a href="${ctx}/device/dvbg/outExcelbg" class="data_input_bg">导出</a>

controler:

/*

* 生成excel

*/

@RequestMapping(value = "/outExcelbg", method = RequestMethod.GET)

public ModelAndView outExcelbg(HttpServletRequest request,HttpServletResponse response) throws IOException{

JSONObject ret=new JSONObject();

List<Dvbgdata> list=bgService.getBgdata((Ps) getSession().getAttribute(CURRENT_USER));

ExcelUtil.outExcelbg(request.getRealPath("/upload").toString(),list);

response.setContentType("text/html;charset=utf-8");  

       request.setCharacterEncoding("UTF-8");  

       java.io.BufferedInputStream bis = null;  

       java.io.BufferedOutputStream bos = null;  

 

       String ctxPath = request.getSession().getServletContext().getRealPath("/upload")

               + "\\" + "xls\\";  

       String downLoadPath = ctxPath + "bg_temp.xls";  

       System.out.println(downLoadPath);  

       try {  

           long fileLength = new File(downLoadPath).length();  

           response.setContentType("application/x-msdownload;");  

           response.setHeader("Content-disposition", "attachment; filename="  

                   + new String("bg_temp.xls".getBytes("utf-8"), "ISO8859-1"));  

           response.setHeader("Content-Length", String.valueOf(fileLength));  

           bis = new BufferedInputStream(new FileInputStream(downLoadPath));  

           bos = new BufferedOutputStream(response.getOutputStream());  

           byte[] buff = new byte[2048];  

           int bytesRead;  

           while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  

               bos.write(buff, 0, bytesRead);  

           }  

       } catch (Exception e) {  

           e.printStackTrace();  

       } finally {  

           if (bis != null)  

               bis.close();  

           if (bos != null)  

               bos.close();  

       }  

return null;

}

util:

public static void outExcelbg(String pathString,List<Dvbgdata> bgdatas){

try{

        WritableWorkbook book  =  Workbook.createWorkbook(new  File(pathString+"/xls/bg_temp.xls"));

        //  生成名为“第一页”的工作表,参数0表示这是第一页 

        WritableSheet sheet  =  book.createSheet( "血糖表" ,  0 );

        //  在Label对象的构造子中指名单元格位置是第一列第一行(0,0)

        WritableCell cell=null;

        //  将定义好的单元格添加到工作表中 

        Label label1=new Label(0,0,"时间");

        Label label2=new Label(1,0,"血糖(mmol/l)");

        Label label3=new Label(2,0,"设备号");

        Label label4=new Label(3,0,"详情");

        sheet.addCell(label1);

        sheet.addCell(label2);

        sheet.addCell(label3);

        sheet.addCell(label4);

        int length=bgdatas.size();

        int i=1;

        for(Dvbgdata dvbgdata : bgdatas){

           Label label01=new Label(0, i, DateUtil.dateToString(dvbgdata.getMeterdate()));

        Label label02=new Label(1, i, dvbgdata.getBg().toString());

        Label label03=new Label(2, i, dvbgdata.getDevicesn());

        Label label04=new Label(3, i,WarningsUtil.getBg(Double.parseDouble(dvbgdata.getBg().toString())));

        sheet.addCell(label01);

        sheet.addCell(label02);

        sheet.addCell(label03);

        sheet.addCell(label04);

        System.out.println(dvbgdata.getBg());

        i++;

        if(i>length){

        break;

        }

        }

       

        //  写入数据并关闭文件 

       book.write();

       book.close();


   }catch(Exception e)  {

       System.out.println(e);

   } 

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值