实习修炼第十七天

整体内容

数据导出到excel
CSV

数据导出到excel

导出excel专用的适配类
在测试时发现由于大意使得分母为零,查看报错日志如下图所示,所以说写代码要严谨纳,微不足道的错搞的很难受在这里插入图片描述

package com.mini.admin.inner.web.goods.domain.vo;

import com.mini.goods.entity.test.GoodsTestDataDetailVO;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * 导出excel专用的适配类
 *
 * @author hjx
 * @date 19/7/16
 */
@Data
public class ExportGoodMessageVO implements Serializable {
    private static final long serialVersionUID = 5810710369255902826L;

    /*
    时间
     */
    private Date day;
    /*
    商品Id
     */
    private Long goodsId;
    /*
    一级分类
     */
    private String firstName;
    /*
    二级分类
     */
    private String secondName;
    /*
    三级分类
     */
    private String thirdName;
    /*
    类目组
     */
    private String categoryGroup;
    /*
    商品名
     */
    private String goodName;
    /*
    曝光量
     */
    private Long exposureCount;
    /*
    点击量
     */
    private Long clickCount;
    /*
    曝光点击率
     */
    private Long exposureClickRate;
    /*
    UV, union visitor
     */
    private Long uv;
    /*
    下单人数
     */
    private Long buyerCount;
    /*
    购买转化率
     */
    private Integer buyRate;
    /*
    销售数量
     */
    private Long saleCount;
    /*
    GMV,商品交易总额
     */
    private Long gmv;
    /*
    曝光转化率
     */
    private Integer exposureRate;
    /*
    曝光点击率
     */
    public Long getExposureClickRate(){
        if(exposureCount!=0){
            return clickCount/exposureCount;
        }
        return 1l;
    }
    /*
    曝光率
     */
    public Integer getExposureRate(){
        if(exposureCount!=0){
            return (int)(buyerCount/exposureCount);
        }
        return 1;
    }
    /*
    购买率
     */
    public Integer getBuyRate(){
        if(uv!=0){
            return (int)(buyerCount/uv);
        }
        return 1;
    }
    public ExportGoodMessageVO convert(GoodsTestDataDetailVO go){
        ExportGoodMessageVO ex = new ExportGoodMessageVO();
        ex.setDay(go.getDay());
        ex.setGoodsId(go.getGoodsId());
        ex.setFirstName(go.getFirstName());
        ex.setSecondName(go.getSecondName());
        ex.setThirdName(go.getThirdName());
        ex.setCategoryGroup(go.getCategoryGroup());
        ex.setGoodName(go.getGoodsName());
        ex.setUv(go.getUv());
        ex.setExposureCount(go.getExposureCount());
        ex.setBuyerCount(go.getBuyerCount());
        ex.setGmv(go.getGmv());
        ex.setSaleCount(go.getSaleCount());
        ex.setClickCount(go.getClickCount());
        return ex;
    }
}

Request请求封装

package com.mini.admin.inner.web.goods.domain.request;


import com.mini.common.service.domain.ServiceRequest;
import com.mini.common.service.util.TraceUtil;
import com.mini.common.web.admin.annotation.AdminHeaderCheck;
import com.mini.common.web.admin.paramter.HttpRequest;
import lombok.Data;

/**
 *
 * @author hjx
 * @date 19/7/16
 */

@Data
@AdminHeaderCheck
public class ExportGoodsMessage2ExcelRequest extends HttpRequest {
    public ExportGoodsMessage2ExcelRequest() {
        super(TraceUtil.generateTraceId());
    }

    public ExportGoodsMessage2ExcelRequest(ServiceRequest base) {
        super(base);
    }

    //商品信息
    //private List exportGoodMessageVOList;

    private Long testId;
}

controller调用

package com.mini.admin.inner.web.goods.controller;

import com.mini.admin.inner.web.goods.domain.request.ExportGoodsMessage2ExcelRequest;
import com.mini.admin.inner.web.goods.domain.vo.ExportGoodMessageVO;
import com.mini.admin.util.CSVExportHelper;
import com.mini.admin.util.CSVExportTemplate;
import com.mini.common.service.domain.ServiceResponse;
import com.mini.common.service.error.CommonError;
import com.mini.goods.entity.test.GoodsTestDataDetailVO;
import com.mini.goods.model.test.QueryGoodsTestDataDetailReq;
import com.mini.goods.service.GoodsTestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 导出excel
 *
 * @author hjx
 * @date 19/7/16
 */
@RestController
@RequestMapping(value = "/export")
public class ExportGoodMessageToExcelController {

    private final static Logger logger = LoggerFactory.getLogger("running");

    //private
    @Resource
    private GoodsTestService goodsTestService;

    @RequestMapping("/downToExcel")
    public ServiceResponse> exportGoodsToexcel(@RequestBody ExportGoodsMessage2ExcelRequest request, HttpServletResponse response) throws Exception {
       if (request == null) {
            throw new RuntimeException("参数有误");
        }
        CSVExportHelper.doExport("exportExcel", new CSVExportTemplate() {
            @Override
            public List getRecordList(int startIndx, int pageSize) {
                if (startIndx > 0) {
                    return null;
                }
                List list = new ArrayList<>();

                QueryGoodsTestDataDetailReq param = new QueryGoodsTestDataDetailReq();
                param.setTestId(request.getTestId());
                ServiceResponse> listServiceResponse = goodsTestService.queryGoodsTestDataDetail(param);

                logger.error("listServiceResponse " + JSON.toJSONString(listServiceResponse));
                if (listServiceResponse.isSuccess() && !CollectionUtils.isEmpty(listServiceResponse.getData())) {
                    for (GoodsTestDataDetailVO rs : listServiceResponse.getData()) {
                        //ExportGoodMessageVO rx = new ExportGoodMessageVO();
                        //BeanUtils.copyProperties(rs,rx);
                        ExportGoodMessageVO rx = new ExportGoodMessageVO().convert(rs);
                        list.add(new ExportGoodMessageVO().convert(rs));
                    }
                }
                logger.error("goodsTest  "+ JSON.toJSONString(list));
                return list;
            }

            @Override
            public List getCols() {
                return Arrays.asList("day", "categoryGroup", "firstName", "secondName",
                        "thirdName", "couponId", "goodName", "exposureCount", "clickCount", "exposureClickRate",
                        "uv","buyerCount", "buyRate","saleCount","gmv","exposureRate");
            }



            @Override
            public List getColsDesc() {
                return Arrays.asList("时间", "类目组", "一级分类", "二级分类",
                        "三级分类", "商品ID", "商品名称", "曝光量", "点击量", "曝光点击率",
                        "UV","下单人数", "购买转化率","销售数量","GMV","曝光转化率");

            }
        },response);
        return new ServiceResponse<>(CommonError.SUCCESS);
    }
}

CSV

快速入门文档
http://commons.apache.org/proper/commons-csv/user-guide.html
逗号分隔值(Comma-Separated Values,CSV)其文件以纯文本形式存储表格数据(数字和文本),通常用于电子表格或数据库软件。
有时在项目中不必非要用CSV,可利用其特性自己实现一个CSVTemplate作为导入或者导出模板,然后实现一个CSVHelper进行文件的操作,比如将查询得来的数据保存到csv文件下就可以实现导出到excel。
使用实例
https://www.cnblogs.com/cjsblog/p/9260421.html 的作者对CSV进行了简单的测试使用;
https://blog.csdn.net/qq_35348457/article/details/79196236 的作者利用新建ExcelUtil进行导入到excel

你可能感兴趣的:(学习进程)