java 下载功能 另存为

上次写的java 跟js的下载功能 写的不清晰,有些需求可能还达不到,但是有热心的朋友的提供了,感谢 

java-Ms郑 童鞋。

思路 ,代码都更加完善的版本给我,我在这里整理了一下,以供大家查询

其实 导出功能,跟下载功能都可以用到 。上菜。

<a href="#" class="btn daochu" >导出</a>

 js   如果是下载 不需要传path

 function exportExcel(){

         $(".daochu").attr('href','${ctx}/assetBzd/zcBzd/exportExcel?str=+"${zcBzd.menuFlag}"');

 }

扫描二维码关注公众号,回复: 517918 查看本文章

 @RequestMapping(value = "exportExcel")

public void exportExcel(HttpServletRequest request, HttpServletResponse response,Model model,

RedirectAttributes redirectAttributes,@RequestParam(value="str")String str){

String fileName = "报账单列表"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";

String message=""; 

try {

fileName = new String(fileName.getBytes("GBK"), "iso8859-1");

response.reset();  

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

               + fileName);// 指定下载的文件名  

response.setContentType("application/vnd.ms-excel");  

response.setHeader("Pragma", "no-cache");  

response.setHeader("Cache-Control", "no-cache");  

response.setDateHeader("Expires", 0);

ZcBzd zcBzd =new ZcBzd();

zcBzd.setBzdType("sbBzd");

zcBzd.setMenuFlag(str);

Page<ZcBzd> page = zcBzdService.findPage(new Page<ZcBzd>(request, response), zcBzd);

List<BzdConfigTable> bzdConfigTable = zcBzdService.getByClassName(zcBzd.getBzdType());

List<String> listColumn = getlistColumn(bzdConfigTable.get(0));

List<ZcBzd> listData = page.getList();

message=createExcelUtils.getBzdExcelNew(listColumn,listData,response);

System.out.println(message);

//message=createExcelUtils.getBzdExcel(listColumn,listData);

//

//addMessage(redirectAttributes, message);

//model.addAttribute("bzdConfigTable" , bzdConfigTable) ;

//model.addAttribute("page", page);

//return "modules/zcBzd/zcBzdList";

} catch (Exception e) {

addMessage(redirectAttributes, message);

}

//return "";

}

public static String getBzdExcelNew(List<String> listColumn,List<ZcBzd> listData,HttpServletResponse response){

FileOutputStream fout = null;

String message="";

HSSFCell cell = null;

try{

  // 第一步,创建一个webbook,对应一个Excel文件  

       HSSFWorkbook wb = new HSSFWorkbook();  

      // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  

       HSSFSheet sheet = wb.createSheet("学生表一");  

       // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  

       HSSFRow row = sheet.createRow((int) 0);  

       // 第四步,创建单元格,并设置值表头 设置表头居中  

       HSSFCellStyle style = wb.createCellStyle();  

       style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  

       for (int j= 0; j< listColumn.size(); j++){ 

       cell = row.createCell((short) j);  

       cell.setCellValue(listColumn.get(j));  

       cell.setCellStyle(style); 

       }

       

//        cell = row.createCell((short) 0);  

//        cell.setCellValue("学号");  

//        cell.setCellStyle(style);  

//        cell = row.createCell((short) 1);  

//        cell.setCellValue("姓名");  

//        cell.setCellStyle(style);  

//        cell = row.createCell((short) 2);  

//        cell.setCellValue("年龄");  

//        cell.setCellStyle(style);  

//        cell = row.createCell((short) 3);  

//        cell.setCellValue("生日");  

//        cell.setCellStyle(style);  

       // 第五步,写入实体数据 实际应用中这些数据从数据库得到,  

//        List list = createExcelUtils.getStudent();  

     

       for (int i = 0; i < listData.size(); i++){  

           row = sheet.createRow((int) i + 1);  

           ZcBzd zcBzd = (ZcBzd) listData.get(i);  

           // 第四步,创建单元格,并设置值  

           row.createCell((short) 0).setCellValue(zcBzd.getFillPersonName().getName());//填写人姓名  

           row.createCell((short) 1).setCellValue(zcBzd.getEquipmentName());  

           row.createCell((short) 2).setCellValue((double) zcBzd.getAccoQuantity());  

//            cell = row.createCell((short) 3);  

//            cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(zcBzd.getCheAccDate()));

           row.createCell((short) 3).setCellValue((double) zcBzd.getAccoAmount());

           row.createCell((short) 4).setCellValue(zcBzd.getUseDepartmentName().getName()); //归属部门

           row.createCell((short) 5).setCellValue(zcBzd.getAssCustodian().getName());

           row.createCell((short) 6).setCellValue(new SimpleDateFormat("yyyy-mm-dd hh-mm-ss").format(zcBzd.getCheAccDate()));

           row.createCell((short) 7).setCellValue(zcBzd.getInvoiceNo());

           row.createCell((short) 8).setCellValue("1".equals(zcBzd.getWheDutyFree())?"是":"否");

           row.createCell((short) 9).setCellValue(zcBzd.getAccoNo());

           row.createCell((short) 10).setCellValue(new SimpleDateFormat("yyyy-mm-dd hh-mm-ss").format(zcBzd.getFillTime()));

        } 

        OutputStream output = response.getOutputStream();

        BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);

        bufferedOutPut.flush();  

        wb.write(bufferedOutPut);

        bufferedOutPut.close();

 } catch (Exception e){  

           e.printStackTrace();

           message="导出错误";

     }finally{

        if(fout != null){

       try {

fout.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

message="导出错误";

        }

     }

System.out.println("**************************导出完毕**********************");

return message;

}

猜你喜欢

转载自1944914806.iteye.com/blog/2271762
今日推荐