java中使用poi导出数据到EXCEL模板中。

package business.component.check.controller;
import accel.common.core.spring.util.ResourceUtils;
import accel.common.data.jdbc.Dao;
import accel.common.webapp.security.Auth;
import business.commons.util.MapUtils;
import business.component.channel.domain.SecUser;
import business.component.check.domain.QdCheckChannel;
import business.component.check.domain.QdCheckRetailer;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.*;

public class ExcelController{

    public ModelAndView eXcel(@RequestParam Map<String, Object> paramMap,HttpServletRequest
    request ,HttpServletResponse response)throws Exception {


        SecUser secUser = Auth.getUser();
        boolean flag = false;

        String checkType = request.getParameter("checkType");
        byte[] bytes = checkType.getBytes("iso8859-1");//把乱码反向编码得回正确的二进制
        checkType = new String(bytes, "utf-8");//拿着二进制自己去查utf-8, 得出正确的字符
        String checkMonth = MapUtils.getString(paramMap, "checkMonth");
        String pkId = MapUtils.getString(paramMap, "pkId");
        String type = MapUtils.getString(paramMap, "type");

        //数据表
        QdCheckChannel newqd = new QdCheckChannel();
        newqd.setPkId(pkId);
        newqd.setCheckType(checkType);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");//时间格式的转换
        try {
            newqd.setCheckMonth(dateFormat.parse(checkMonth));
        } catch (ParseException e) {
            e.printStackTrace();
        }


        //获取文件名和模板
        String templateName = "check-franchise-template.xlsx";//EXCEL模板的名字
        String fileName = "特许经营考核评分表.xlsx";//导出时下载的EXCEL文件名

        QdCheckChannel qdCheckChannel = Dao.load(QdCheckChannel.class, pkId);
        Map<String, String> map = new HashMap<String, String>();
        map.put("fromId", pkId);
        List<QdCheckRetailer> retailers = Dao.listBySql(QdCheckRetailer.class, 
        "query.clerk.retailer.list", map);
        //从数据中取出所要导出的数据存放在List集合对象中

        InputStream inputStream = new FileInputStream(ResourceUtils.getFile
        ("classpath:template/" + templateName));
       //读取模板文件

        Workbook wb = null;
        try {
            wb = new XSSFWorkbook(inputStream);//根据不同的excel版本采用不同的Workbook

            Sheet sheet = wb.getSheetAt(0);//读取模板中的sheet内容
            //在相应的单元格进行赋值
            for (int i = 0; i < retailers.size(); i++) {//对数据进行循环导出
                org.apache.poi.ss.usermodel.Row row = sheet.getRow(2 + i);
               //模板表头占用两行,i为第一条数据 从第三行开始
                if (row == null) {
                    row = sheet.createRow(2 + i);
                    row.createCell(0).setCellValue(i+1);
                    row.createCell(1).setCellValue(checkMonth);
                    row.createCell(2).setCellValue(retailers.get(i).getRetailerCode());
                    row.createCell(3).setCellValue(retailers.get(i).getAddress());
                } else {
                    row.getCell(0).setCellValue(i+1);
                    row.getCell(1).setCellValue(checkMonth);
                    row.getCell(2).setCellValue(retailers.get(i).getRetailerCode());
                    row.createCell(3).setCellValue(retailers.get(i).getAddress());
                }
            }

        } catch (Exception ex) {
            wb = new HSSFWorkbook(inputStream);
            //读取了模板内所有sheet内容
            HSSFSheet sheet = (HSSFSheet) wb.getSheetAt(0);
            //在相应的单元格进行赋值
            for (int i = 0; i < retailers.size(); i++) {
                HSSFRow row = sheet.getRow(2 + i);
                if (row == null) {
                    row = sheet.createRow(2 + i);
                    row.createCell(0).setCellValue(i+1);
                    row.createCell(1).setCellValue(checkMonth);
                    row.createCell(2).setCellValue(retailers.get(i).getRetailerCode());
                    row.createCell(3).setCellValue(retailers.get(i).getAddress());
                } else {
                    row.getCell(0).setCellValue(i+1);
                    row.getCell(1).setCellValue(checkMonth);
                    row.getCell(2).setCellValue(retailers.get(i).getRetailerCode());
                    row.createCell(3).setCellValue(retailers.get(i).getAddress());
                }
            }
        }


        OutputStream output = response.getOutputStream();
        response.reset();
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("applicationnd/msexcel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + 
         fileName + "\"");//下载文件
        wb.write(output);//写入到Excel模板文件中
        output.close();//关闭


        return null;

    }










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值