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);
    }
  • 相关阅读:
    Xshell添加快捷按钮
    Go语言基础之21--反射
    Jenkins自动化CI CD流水线之7--流水线自动化发布PHP项目
    Jenkins自动化CI CD流水线之6--构建邮件状态通知
    Jenkins自动化CI CD流水线之5--pipeline
    Python练习-函数(方法)的定义和应用
    Python文件操作-文件的增删改查
    Python练习-不知道弄个什么鬼
    Python练习-短小精干版三级"片儿"
    Python练习-三级菜单与"片儿"无关!
  • 原文地址:https://www.cnblogs.com/liuna369-4369/p/10930629.html
Copyright © 2011-2022 走看看