在Springmvc中导出报表下载Excel文件

本文介绍了如何在Springmvc框架下,通过Controller、Service、Dao、Mapper和Excel工具类协同工作,实现在Web应用中导出报表并下载Excel文件的方法。
摘要由CSDN通过智能技术生成

controller层

@Controller
public class UseCaseController{
   
    @Autowired
    private UseCaseService useCaseService;

/**
*进入到useCase_excel页面
*/
    @RequestMapping(value="useCase_excel.xhtml",method="RequestMethod.GET")
    public ModelAndView download(){
    ModelAndView model = new ModelAndView("useCase_excel");
    }

/**
*实现下载导出excel功能
*/
@RequestMapping(value="useCase_excel_download",method="RequestMethod.GET")
    publice String download(HttpServletRequest request,HttpServletResponse response,String requirementId,String status,String stage){
    String fileName="测试案例报表";
    List<UseCase> useCases = useCaseService.createData(requirement,status,stage);
    List<Map<String,Object>> list =createExcelRecord(useCases);
    //列名
    String columnNames[]={
  "测试案例编号","需求编号","测试案例负责人","测试人","功能描述","完成率","状态","阶段","计划执行日期","更新日期","建立日期","备注"};
    //Map中的key
     String keys[]={
  "useCaseId","requirementId","useCaseOwner","useCaseTester","functionDesc","percent","status","stage","startTimeDesc","updateTimeDesc","createTimeDesc","remark"};
     ByteArrayOutputStream os = new ByteArrayOutputStream();
     try{
         UseCaseExcelUtil.createWorkBook(list,keys,columnNames).write(os);
     }catch(IOException e){
         e.printStackTrace();
      }
      byte content = os.toByteArray();
      InputStream is = new ByteArrayInputStream(content);
      response.reset();
      response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try{
        bis = new BufferedInputStream(is);
        bos = new BufferesOutputStream(out);
        byte[] buff = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
     }catch (final IOException e) {
            throw e; } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
        return null;
    }

     private List<Map<String, Object>> createExcelRecord(List<UseCase> useCases) {
        List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sheetName", "sheet1");
        listmap.add(map);
        UseCase useCase=null;
        for (int j = 0; j < useCases.size(); j++) {
            useCase=useCases.get(j);
            Map<String, Object> mapValue = new HashMap<String, Object>();
            mapValue.put("useCaseId", useCase.getUseCaseId());
            mapValue.put("requirementId", useCase.getRequirementId());
            mapValue.put("useCaseOwner", useCase.getUseCaseOwner());
            mapValue.put("
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值