java导出数据到csv文件

工具类:

package com.brochina.base.utils.util;

import org.apache.commons.collections.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author wuzhaosheng
 * @Date 2022/1/12 10:21
 */
public class CsvExportUtil {
    /**
     * CSV文件列分隔符
     */
    private static final String CSV_COLUMN_SEPARATOR = ",";

    /**
     * CSV文件行分隔符
     */
    private static final String CSV_ROW_SEPARATOR = "\r\n";

    /**
     * @param dataList 集合数据
     * @param titles   表头部数据
     * @param propertys  数据实体的属性
     * @param os       输出流
     */
    public static <T> void doExport(List<T> dataList, String[] titles, String[] propertys, OutputStream os) throws Exception {
        StringBuilder bud = new StringBuilder();
        // 组装表头
        for (String title : titles) {
            bud.append(title).append(CSV_COLUMN_SEPARATOR);
        }
        bud.append(CSV_ROW_SEPARATOR);
        // 组装数据
        if (CollectionUtils.isNotEmpty(dataList)) {
            for (Object obj : dataList) {
                Field[] fields = obj.getClass().getDeclaredFields();
                for (String property: propertys){
                    for (Field field : fields) {
                        field.setAccessible(true);
                        if(property.equalsIgnoreCase(field.getName())){
                            Object o = field.get(obj);
                            if(o != null){
                                bud.append(field.get(obj).toString());
                            }
                            bud.append(CSV_COLUMN_SEPARATOR);
                            continue;
                        }
                    }
                }
                bud.append(CSV_ROW_SEPARATOR);
            }
        }

        // 写出响应
        os.write(bud.toString().getBytes("GBK")); //windows下防止中文乱码
        os.flush();
    }

    /**
     * 设置Header
     *
     * @param fileName
     * @param response
     * @throws UnsupportedEncodingException
     */
    public static void responseSetProperties(String fileName, HttpServletResponse response) throws UnsupportedEncodingException {
        // 设置文件后缀
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fn = fileName + sdf.format(new Date());
        response.setContentType("application/octet-stream");
        response.setContentType("Content-type:application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition","attachment;filename=\"" + fn + ".csv\"");
    }

}

实体类:

package com.brochina.base.boss.modal;

import java.math.BigDecimal;

public class BaseBoss {
    private String id;

    private String fphm;

    private String fpdm;

    private String jtbh;

    private String jtmc;

    private String zhmc;

    private String gmfmc;

    private String gmfsh;

    private String xsfmc;

    private String xsfsh;

    private String zyspmc;

    private BigDecimal kpjebhsj;

    private BigDecimal sk;

    private String kprq;

    private String kpr;

    private String skr;

    private String fhr;

    private String zfbs;

    private String sqyybz;

    private String fqrqcrsqr;

    private Integer pjlx;

    private String fgs;

    private String zhbh;

    private Integer fpywlx;

    private String sjzq;

    private Integer sfch;

    private Integer sfyd;

    private BigDecimal kphjje;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id == null ? null : id.trim();
    }

    public String getFphm() {
        return fphm;
    }

    public void setFphm(String fphm) {
        this.fphm = fphm == null ? null : fphm.trim();
    }

    public String getFpdm() {
        return fpdm;
    }

    public void setFpdm(String fpdm) {
        this.fpdm = fpdm == null ? null : fpdm.trim();
    }

    public String getJtbh() {
        return jtbh;
    }

    public void setJtbh(String jtbh) {
        this.jtbh = jtbh == null ? null : jtbh.trim();
    }

    public String getJtmc() {
        return jtmc;
    }

    public void setJtmc(String jtmc) {
        this.jtmc = jtmc == null ? null : jtmc.trim();
    }

    public String getZhmc() {
        return zhmc;
    }

    public void setZhmc(String zhmc) {
        this.zhmc = zhmc == null ? null : zhmc.trim();
    }

    public String getGmfmc() {
        return gmfmc;
    }

    public void setGmfmc(String gmfmc) {
        this.gmfmc = gmfmc == null ? null : gmfmc.trim();
    }

    public String getGmfsh() {
        return gmfsh;
    }

    public void setGmfsh(String gmfsh) {
        this.gmfsh = gmfsh == null ? null : gmfsh.trim();
    }

    public String getXsfmc() {
        return xsfmc;
    }

    public void setXsfmc(String xsfmc) {
        this.xsfmc = xsfmc == null ? null : xsfmc.trim();
    }

    public String getXsfsh() {
        return xsfsh;
    }

    public void setXsfsh(String xsfsh) {
        this.xsfsh = xsfsh == null ? null : xsfsh.trim();
    }

    public String getZyspmc() {
        return zyspmc;
    }

    public void setZyspmc(String zyspmc) {
        this.zyspmc = zyspmc == null ? null : zyspmc.trim();
    }

    public BigDecimal getKpjebhsj() {
        return kpjebhsj;
    }

    public void setKpjebhsj(BigDecimal kpjebhsj) {
        this.kpjebhsj = kpjebhsj;
    }

    public BigDecimal getSk() {
        return sk;
    }

    public void setSk(BigDecimal sk) {
        this.sk = sk;
    }

    public String getKprq() {
        return kprq;
    }

    public void setKprq(String kprq) {
        this.kprq = kprq == null ? null : kprq.trim();
    }

    public String getKpr() {
        return kpr;
    }

    public void setKpr(String kpr) {
        this.kpr = kpr == null ? null : kpr.trim();
    }

    public String getSkr() {
        return skr;
    }

    public void setSkr(String skr) {
        this.skr = skr == null ? null : skr.trim();
    }

    public String getFhr() {
        return fhr;
    }

    public void setFhr(String fhr) {
        this.fhr = fhr == null ? null : fhr.trim();
    }

    public String getZfbs() {
        return zfbs;
    }

    public void setZfbs(String zfbs) {
        this.zfbs = zfbs == null ? null : zfbs.trim();
    }

    public String getSqyybz() {
        return sqyybz;
    }

    public void setSqyybz(String sqyybz) {
        this.sqyybz = sqyybz == null ? null : sqyybz.trim();
    }

    public String getFqrqcrsqr() {
        return fqrqcrsqr;
    }

    public void setFqrqcrsqr(String fqrqcrsqr) {
        this.fqrqcrsqr = fqrqcrsqr == null ? null : fqrqcrsqr.trim();
    }

    public Integer getPjlx() {
        return pjlx;
    }

    public void setPjlx(Integer pjlx) {
        this.pjlx = pjlx;
    }

    public String getFgs() {
        return fgs;
    }

    public void setFgs(String fgs) {
        this.fgs = fgs == null ? null : fgs.trim();
    }

    public String getZhbh() {
        return zhbh;
    }

    public void setZhbh(String zhbh) {
        this.zhbh = zhbh == null ? null : zhbh.trim();
    }

    public Integer getFpywlx() {
        return fpywlx;
    }

    public void setFpywlx(Integer fpywlx) {
        this.fpywlx = fpywlx;
    }

    public String getSjzq() {
        return sjzq;
    }

    public void setSjzq(String sjzq) {
        this.sjzq = sjzq == null ? null : sjzq.trim();
    }

    public Integer getSfch() {
        return sfch;
    }

    public void setSfch(Integer sfch) {
        this.sfch = sfch;
    }

    public Integer getSfyd() {
        return sfyd;
    }

    public void setSfyd(Integer sfyd) {
        this.sfyd = sfyd;
    }

    public BigDecimal getKphjje() {
        return kphjje;
    }

    public void setKphjje(BigDecimal kphjje) {
        this.kphjje = kphjje;
    }


}

 

 

接口:

    @PostMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BaseBoss baseBoss = baseBossMapper.selectByPrimaryKey("42002702201060858090000006655013");
        List<BaseBoss> list = new ArrayList<>();
        list.add(baseBoss);
        String[] titles = {"申请单号","发票号码","发票代码","集团编号","集团名称","账户名称","购买方名称","购买方税号","销售方名称","销售方税号","主要商品名称","开票金额不含税价","税款","开票日期","开票人","收款人","复核人","作废标识","申请原因备注","发起人起草人申请人","票据类型","分公司","账户编号","发票业务类型","时间账期","是否冲红","是否预打","开票合计金额"};
        String[] propertys = {"id","fphm","fpdm","jtbh","jtmc","zhmc", "gmfmc", "gmfsh", "xsfmc", "xsfsh", "zyspmc", "kpjebhsj", "sk", "kprq", "kpr", "skr", "fhr", "zfbs", "sqyybz", "fqrqcrsqr", "pjlx", "fgs", "zhbh", "fpywlx", "sjzq", "sfch", "sfyd", "kphjje"};

        CsvExportUtil.responseSetProperties("baseBoss表", response);
        OutputStream outputStream = null;

        try {
                outputStream = response.getOutputStream();
            CsvExportUtil.doExport(list, titles, propertys, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

 

posted on 2022-01-12 11:15  小吴编  阅读(1279)  评论(0编辑  收藏  举报