zoukankan      html  css  js  c++  java
  • 前台下载数据保存在excel中

    @RequestMapping("/download.do")
    public void download(String years, String months, HttpServletResponse response) throws IOException {
    //System.out.println(years+"-"+months);
    List<ProductList> products=adminService.findProductByDate(years,months);//查询到的结果放在一个无实例ProductList类,getter和setter查询到的数值
    String fileName=years+"年"+ months +"月销售榜单";
    String sheetName=months + "月销售榜单";
    String titleName=years+"年"+ months +"月销售榜单";
    String [] columeName={"商品名称","商品销量"};
    String [][] comment=new String[products.size()][2];
    for(int i=0;i<products.size();i++){
    comment[i][0]=products.get(i).getName();
    comment[i][1]= String.valueOf(products.get(i).getTotalnum());
    }
    //创建excel文件
    HSSFWorkbook hwb=new HSSFWorkbook();
    //在hwb中创建一个sheet脚本
    HSSFSheet sheet=hwb.createSheet(sheetName);
    //创建第一行
    HSSFRow row1=sheet.createRow(0);
    //创建第一行的第一个单元格
    HSSFCell cell=row1.createCell(0);
    //合并单元格
    sheet.addMergedRegion(new CellRangeAddress(0,0,0,1));
    cell.setCellValue(titleName);
    //创建第二行
    HSSFRow row2=sheet.createRow(1);
    //给第二行的两个单元格赋值
    for( int i=0;i<2;i++){
    row2.createCell(i).setCellValue(columeName[i]);
    }
    //创建数据行
    for(int i=0;i<comment.length;i++){
    row2=sheet.createRow(i+2);
    for(int j=0;j<2;j++){
    row2.createCell(j).setCellValue(comment[i][j]);
    }
    }
    String filename=fileName+".xls";
    response.setContentType("application/ms-excel;charset=UTF-8");
    response.setHeader("content-Disposition","attachment;filename="+filename);
    OutputStream out =response.getOutputStream();
    hwb.write(out);
    }
  • 相关阅读:
    【poj2761】 Feed the dogs
    【bzoj1086】 scoi2005—王室联邦
    学堂在线
    【bzoj3757】 苹果树
    【uoj58】 WC2013—糖果公园
    博弈论学习笔记
    【poj2960】 S-Nim
    【poj2234】 Matches Game
    【poj1740】 A New Stone Game
    【bzoj1853】 Scoi2010—幸运数字
  • 原文地址:https://www.cnblogs.com/liuna369-4369/p/10930629.html
Copyright © 2011-2022 走看看