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);
    }
  • 相关阅读:
    Win7系统下搭建Tomcat服务器
    转:在线框架引用 bootstrap/jq/jqmobile/css框架
    转:Web.config配置文件详解
    转:HTML和Web窗体的区别
    在VS2010中创建网站并发布
    nohup 借助cronolog进行日志分割 Linux
    Linux (centos 8) 安装nginx1.20
    Ubuntu 安装使用yum--转载
    C# 字符串插值内部工作原理
    Linux 安装MySQL
  • 原文地址:https://www.cnblogs.com/liuna369-4369/p/10930629.html
Copyright © 2011-2022 走看看