easyexcel导出 导出列不固定

public void test(){
    /*组装exc数据*/
        List<List<String>> dataList = assemblyExcelData(list);

        /*表头*/
        List<List<String>> headList = new ArrayList<List<String>>();

        headList.add(Arrays.asList("报表日期"));
        if (StringUtil.paramIsIntact(request.getParameter("canteenno"))){
            headList.add(Arrays.asList("餐厅名称"));
        }
        if (StringUtil.paramIsIntact(request.getParameter("merchantno"))){
            headList.add(Arrays.asList("商户名称"));
        }
        if ("1".equals(request.getParameter("type"))){
            headList.add(Arrays.asList("服务人数"));
        }else {
            headList.add(Arrays.asList("营业额"));
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("营业报表导出", "UTF-8");

        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream()).registerWriteHandler(baseService.getCellStyle())
                .registerWriteHandler(new ExcelWidthStyleStrategy())
                .head(headList).sheet("统计报表")
                .doWrite(dataList);
}


private List<List<String>> assemblyExcelData(List<JSONObject> list) {
        List<List<String>> data = new ArrayList<List<String>>();
        for (JSONObject obj : list) {
            List<String> strs = new ArrayList<String>();
            strs.add(obj.getString("rptdate"));
            if (StringUtil.paramIsIntact(obj.getString("canteenname"))){
                strs.add(obj.getString("canteenname"));
            }
            if (StringUtil.paramIsIntact(obj.getString("merchantname"))){
                strs.add(obj.getString("merchantname"));
            }
            if (StringUtil.paramIsIntact(obj.getString("mealtimecode"))){
                strs.add(obj.getString("mealtimecode"));
            }
            if (StringUtil.paramIsIntact(obj.getString("turnover"))){
                strs.add(obj.getString("turnover"));
            }else {
                strs.add(obj.getString("consumenum"));
            }
            data.add(strs);
        }
        return data;
    }


@Component
public class ExcelWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
	@Override
	protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
		// 使用sheet对象 简单设置 index所对应的列的列宽
		Sheet sheet = writeSheetHolder.getSheet();
		sheet.setColumnWidth(cell.getColumnIndex(), 5000);
	}
}

public HorizontalCellStyleStrategy getCellStyle() {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN); //下边框
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);//左边框
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);//上边框
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);//右边框
        //设置 垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        return horizontalCellStyleStrategy;
    }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值