导出excel:下载模板时填充数据方法实现

    /**
     * 导出excel
     * @param model
     * @param request
     * @param userAgent
     * @param id
     * @return
     */
    @RequestMapping(value = "export.htm")
    public void export(ModelMap model,HttpServletRequest request,HttpServletResponse response,UserAgent userAgent,@RequestParam(value = "taxpayerId",required = false) Integer id) {
        //获取填充数据
        TaxpayerInfo selectByPrimaryKey = taxpayerInfoManager.selectByPrimaryKey(id);
        List<SupportOlder> supportOlderList=supportOlderManager.getListByTaxpayerId(id);
        //创建excel
        ExcelUtil excelUtil=new ExcelUtil();
        //获取模板存放路径
        String line=File.separator; 
        String realPath=request.getRealPath("")+"\\excelmodel\\";
        System.out.println(realPath);
        //windows下
        if("\\".equals(line)){
            realPath = realPath.replace("/", "\\");  // 将/换成\\
            realPath=realPath+line;
        }
       //linux下
        if("/".equals(line)){
            realPath = realPath.replace("\\", "/");
            realPath=realPath+line;
        }
        String path=realPath+"supportOlder.xls";
        String fileName="赡养老人支出明细.xls";
        
        //给excel设置模板、sheet名称
        excelUtil.setSrcPath(path);
        excelUtil.setSheetName("Sheet1");
        excelUtil.getSheet();
        
        //开始填充数据
        String cardType="";
        cardType = getCardType(selectByPrimaryKey.getCardType()==null?"":selectByPrimaryKey.getCardType());
        excelUtil.setCellStrValue(1, 1, cardType);
        excelUtil.setCellStrValue(1, 3, selectByPrimaryKey.getCardNo());
        excelUtil.setCellStrValue(2, 1, selectByPrimaryKey.getName());
        excelUtil.setCellStrValue(2, 3, selectByPrimaryKey.getIdentifyNo());
        excelUtil.setCellStrValue(3, 1, selectByPrimaryKey.getWithholdingAgent());
        excelUtil.setCellStrValue(3, 3, selectByPrimaryKey.getAgentNo());
        int row=4;
        int col=-1;
        Integer num=0;
        for (SupportOlder supportOlder : supportOlderList) {
            row++;
            num++;
            col=1;
            excelUtil.setCellStrValue(row, 0, num.toString());
            excelUtil.setCellStrValue(row, col++, supportOlder.getName());
            excelUtil.setCellStrValue(row, col++, getCardType(supportOlder.getCardType()));
            excelUtil.setCellStrValue(row, col++, supportOlder.getCardNo());
        }
        //导出excel
        excelUtil.exportToWeb(response,fileName);
        
    }

工具类为ExcelUtil:

package com.zhiyuancorp.web.util;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值