本文实例为大家分享了Java导出CSV文件的具体代码,供大家参考,具体内容如下
Java导出csv文件:
控制层:
@Controller @RequestMapping("/historyReport/") public class HistoryStockReportController { private static final Logger LOGGER = LoggerFactory.getLogger(HistoryStockReportController.class); @Autowired private HistoryStockReportService historyStockReportService; /** * 下载历史库存报表 * @param request * @param response */ @RequestMapping("new/downLoadHistoryStockInfo.htm") @ResponseBody public ResultMsg
备注:这里查询list集合数据是按照分页查询,pageNo=1,pageSize=1000,这样支持大数据量导出,比如导出10万条数据,分页查询是为了防止把库查询挂了,数据量过大会发生导出OOM
业务层:
@Service public class HistoryStockReportServiceImpl extends BaseImpl implements HistoryStockReportService { private static final Logger LOGGER = LoggerFactory.getLogger(HistoryStockReportServiceImpl.class); //定义数据库查询字段 private String[] columnArray = Constant.CHECK_PP_INDEX_COLUMN.split(","); @Autowired private DalClient dalClient; /** * 下载历史库存报表 * @param condition * @return */ public List> queryDownLoadHistoryStockInfo(Map condition) { List > resultList = dalClient .queryForList("historyStockData.queryDownLoadHistoryStockInfo", condition); if (!CollectionUtil.isEmptyList(resultList)) { IndexDataFormatUtils.coverPpInfo(resultList, columnArray); } return resultList; } }
查询集合处理工具类:IndexDataFormatUtils
public class IndexDataFormatUtils { /** * 统一处理PP视频历史库存、特殊渠道指标报表的衍生指标数据 * @param list * @param columnArray */ public static void coverPpInfo(List> list, String[] columnArray) { for (Map map : list) { // 组装处理rate参数 calculateRate(map, Constant.FEE_PRACTICAL_SHOW_NUM, Constant.THEORY_STOCK_NUM, Constant.FILLFEE_RATE); calculateRate(map, Constant.DELIVERY_PRACTICAL_SHOW_NUM, Constant.THEORY_STOCK_NUM, Constant.DELIVERY_FILL_RATE); calculateRate(map, Constant.SHOW_NUM, Constant.THEORY_STOCK_NUM, Constant.THEORY_STOCK_RATE); calculateRate(map, Constant.THEORY_STOCK_REMAINED_NUM, Constant.THEORY_STOCK_NUM, Constant.THEORY_STOCK_REMAINED_RATE); // 处理数据值为null的单一指标 coverIndexInfoFromNull(map, columnArray); } } /** * 处理占比参数 * * @param map * @param dividendKey * @param divisorKey * @param quotientKey */ public static void calculateRate(Map map, String dividendKey, String divisorKey, String quotientKey) { if (StringUtils.isBlank(MapUtils.getString(map,dividendKey)) || StringUtils.isBlank(MapUtils.getString(map,divisorKey))) { map.put(quotientKey,"-"); return; } BigDecimal dividend = BigDecimal.valueOf(MapUtils.getDoubleValue(map, dividendKey)); // 被除数 BigDecimal divisor = BigDecimal.valueOf(MapUtils.getDoubleValue(map, divisorKey)); // 除数 BigDecimal quotient = BigDecimal.valueOf(0.00); // =0 相等 >0前者大于后者 ,反之 <0 前者小于后者 if(dividend.compareTo(BigDecimal.ZERO) != 0 && divisor.compareTo(BigDecimal.ZERO) != 0){ quotient = dividend.multiply(BigDecimal.valueOf(100)).divide(divisor,2,BigDecimal.ROUND_HALF_UP); } map.put(quotientKey, quotient.setScale(2) + ""); } /** * 处理数据值为null的单一指标 * @param map * @param columnArray */ public static void coverIndexInfoFromNull(Map map, String[] columnArray) { for (String columnName : columnArray) { String columnValue = MapUtils.getString(map,columnName); if (StringUtils.isBlank(columnValue)) { map.put(columnName,"-"); }else { map.put(columnName,columnValue); } } } }
导出数据处理工具类:ExportUtils
public class ExportUtils { /** * 处理下载指标 * */ public static String handleExportData(MapreportData, String[] columns){ StringBuilder sb = new StringBuilder(); for (String columnName:columns) { addStringBuffer(sb,reportData,columnName); } sb.append("\n"); return sb.toString(); } public static void addStringBuffer(StringBuilder sb, Map map,String name){ if(map.get(name) == null ){ sb.append("-,"); }else{ String value = String.valueOf(map.get(name)); String temp = value.replaceAll("\r", "").replaceAll("\n", ""); if(temp.contains(",")){ if(temp.contains("\"")){ temp=temp.replace("\"", "\"\""); } //将逗号转义 temp="\""+temp+"\""; } sb.append("\t").append(temp).append(","); } } }
常量类:
//导出默认分页 public static final int PAGE_NO = 1; public static final int PAGE_SIZE = 1000; /** * PP视频 * 历史存储、特殊渠道数据库查询字段 */ public static final String CHECK_PP_INDEX_COLUMN = "requestNum,advertiserVvNum,responseNum,showNum,clickNum,theoryStockNum,fillFeeNum," + "feePracticalShowNum,deliveryFillNum,deliveryPracticalShowNum,theoryStockRemainedNum," + "systemExceptionLost,userExitLost,income"; /** * 20190509 * pp视频历史库存、特殊渠道日志数据报表 * 报表下载模板头部(英文) */ public static final String PP_DETAIL_COLUMN = "countDate,pointLocationCode,pointLocationName,positionScreenTypeCode,positionScreenTypeName," + "requestNum,advertiserVvNum,responseNum,showNum,clickNum,theoryStockNum,fillFeeNum,feePracticalShowNum,deliveryFillNum," + "deliveryPracticalShowNum,theoryStockRemainedNum,systemExceptionLost,userExitLost,income,fillFeeRate,deliveryFillRate," + "theoryStockRate,theoryStockRemainedRate"; /** * pp视频历史库存日志数据报表 * 报表下载模板头部(中文) */ public static final String[] PP_INDEX_DETAIL_HEAD_NAME_LIST = {"统计时间,","点位/终端编码,","点位/终端名称,","广告位编码,", "广告位名称,", "请求量,", "广告vv量,","返回量,","曝光量,", "点击量,", "理论库存量,", "付费填充量,", "付费实际曝光量,", "配送填充量,", "配送实际曝光量,", "理论库存余量,", "系统异常损失,", "用户退出损失,","收入,", "付费使用率,", "配送使用率,", "库存使用率,", "库存余量占比,"};
导出效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。