@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);
}
posted on 2019-05-27 14:41  北边那座城  阅读(176)  评论(0编辑  收藏  举报