csv导出百万级数据

1:测试数据200多万,项目使用

2:csv工具类,我用的第一方法

package com.wptx.comm.config;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

public class CSVUtils {
	
	private static CSVUtils csvUtils;
	
	public static CSVUtils getInstance() {
		if (csvUtils == null) {
			csvUtils = new CSVUtils();
		}
		return csvUtils;
	}
	
	 /**
	   * 生成为CVS文件 
	   * @param exportData 
	   *       源数据List 
	   * @param map 
	   *       csv文件的列表头map 
	   * @param outPutPath 
	   *       文件路径 
	   * @param fileName 
	   *       文件名称 
	   * @return 
	   */
	@SuppressWarnings({ "rawtypes","unused" })
	public static String createCSVFile(List exportData, LinkedHashMap map, String outPutPath, String fileName) {
		File csvFile = null;
		BufferedWriter csvFileOutputStream = null;
		String path = null;
		try {
			File file = new File(outPutPath);
			if (!file.exists()) {
				file.mkdirs();
			}
			// 定义文件名格式并创建  
			csvFile = File.createTempFile(fileName, ".csv", new File(outPutPath));
			path = csvFile.getCanonicalPath();
			// System.out.println(csvFile.getName());
			// System.out.println("csvFile:" + path);
			// UTF-8使正确读取分隔符","  osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));   
			// 如果生产文件乱码,windows下用gbk,linux用UTF-8
			csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"), 1024);
			csvFileOutputStream.write('\ufeff');
			
			// 写入文件头部
			for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {
				java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
				csvFileOutputStream.write((String) propertyEntry.getValue() != null ? (String) propertyEntry.getValue() : "");
				
				if (propertyIterator.hasNext()) {
					csvFileOutputStream.write(",");
				}
			}
			
			csvFileOutputStream.newLine();
			
			// 写入文件内容  
			for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
				Object row = (Object) iterator.next();
				for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {
					java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
					// csvFileOutputStream.write((String) BeanUtils.getProperty(row, (String) propertyEntry.getValue() != null ? (String) propertyEntry.getValue() : ""));
					csvFileOutputStream.write((String) BeanUtils.getProperty(row, (String) propertyEntry.getKey()));
					
					if (propertyIterator.hasNext()) {
						csvFileOutputStream.write(",");
					}
				}
				
				if (iterator.hasNext()) {
					csvFileOutputStream.newLine();
				}
			}
			
			csvFileOutputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				csvFileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return csvFile.getName();
	}
		
//  /**
//   * 生成为CVS文件 
//   * @param exportData 
//   *       源数据List 
//   * @param map 
//   *       csv文件的列表头map 
//   * @param outPutPath 
//   *       文件路径 
//   * @param fileName 
//   *       文件名称 
//   * @return 
//   */
//	@SuppressWarnings({ "rawtypes","unused" })
//	public static String createCSVFile(List exportData, LinkedHashMap map, String outPutPath, String fileName) {
//		File csvFile = null;
//		BufferedWriter csvFileOutputStream = null;
//		String path = null;
//		try {
//			File file = new File(outPutPath);
//			if (!file.exists()) {
//				file.mkdir();
//			}
//			// 定义文件名格式并创建  
//			csvFile = File.createTempFile(fileName, ".csv", new File(outPutPath));
//			path = csvFile.getCanonicalPath();
			System.out.println(csvFile.getName());
			System.out.println("csvFile:" + path);
//			// UTF-8使正确读取分隔符","  osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));   
//			// 如果生产文件乱码,windows下用gbk,linux用UTF-8
//			// csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "GBK"), 1024);
//			CsvWriter csvWriter = new CsvWriter(csvFile.getAbsolutePath(), ',', Charset.forName("UTF-8"));
//			// csvWriter.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));
//			String chatset = new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF });
//			
//			String[] titleArray = new String[map.size()];
//			// 写入文件头部
//			int i = 0;
//			for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {
//				java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
//				// csvFileOutputStream.write('\ufeff');
//				// csvFileOutputStream.write((String) propertyEntry.getValue() != null ? (String) propertyEntry.getValue() : "");
//				// csvWriter.write((String) propertyEntry.getValue() != null ? (String) propertyEntry.getValue() : "");
//				if (i == 0) {
//					titleArray[i++] = (String) propertyEntry.getValue() != null ? chatset + (String) propertyEntry.getValue() : "";
//				}
//				else {
//					titleArray[i++] = (String) propertyEntry.getValue() != null ? (String) propertyEntry.getValue() : "";
//				}
//				
				if (propertyIterator.hasNext()) {
					// csvFileOutputStream.write(",");
					// csvWriter.write(",");
				}
//			}
//			
//			csvWriter.writeRecord(titleArray, true);
//			
//			
//			// csvFileOutputStream.newLine();
//			// csvWriter.write("\r\n");
//			
//			// 写入文件内容  
//			for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
//				Object row = (Object) iterator.next();
//				String[] datdaArray = new String[map.size()];
//				i = 0;
//				for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {
//					java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
//					// csvFileOutputStream.write('\ufeff');
//					// csvFileOutputStream.write((String) BeanUtils.getProperty(row, (String) propertyEntry.getKey()));
//					// csvWriter.write((String) BeanUtils.getProperty(row, (String) propertyEntry.getKey()));
//					datdaArray[i++] = (String) BeanUtils.getProperty(row, (String) propertyEntry.getKey());
//					
					if (propertyIterator.hasNext()) {
						// csvFileOutputStream.write(",");
						// csvWriter.write(",");
					}
//				}
//				
//				csvWriter.writeRecord(datdaArray, true);
//				
				if (iterator.hasNext()) {
					// csvFileOutputStream.newLine();
					// csvWriter.write("\r\n");
				}
//			}
//			
//			
//			// csvFileOutputStream.flush();
//			csvWriter.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
			try {
				csvFileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
//		}
//		return csvFile.getName();
//	}

	/**
	 *  下载文件    
	 *  @param response     
	 *  @param csvFilePath   
	 *  文件路径    
	 *  @param fileName      
	 *   文件名称     
	 *  @throws IOException   
	 *  
	 */
	public static void exportFile(HttpServletResponse response, String csvFilePath, String fileName) throws IOException {
		response.setContentType("application/csv;charset=UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));

		InputStream in = null;
		try {
			in = new FileInputStream(csvFilePath);
			int len = 0;
			byte[] buffer = new byte[1024];
			response.setCharacterEncoding("UTF-8");
			OutputStream out = response.getOutputStream();
			while ((len = in.read(buffer)) > 0) {
				out.write(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF });
				out.write(buffer, 0, len);
			}
		} catch (FileNotFoundException e) {
			//System.out.println(e);
		} finally {
			if (in != null) {
				in.close();
			}
		}
	}
	
	 /**
     * 生成csv文件之前特殊字符转义
     * @param str
     * @return
     */
    public static String csvString(String str){
    	str=str.toString().replaceAll("[/r/n]"," ");
        if(str.contains("-")){
            str = str.replace("\"","\"\"");
            str ="\"" + str + "\"";
        }
        return str;
    }
	 /** 
	   * 测试数据 
	   * @param args 
	   */ 
	@SuppressWarnings({ "rawtypes", "unchecked" }) 
	public static void main(String[] args) { 
			List exportData = new ArrayList<Map>(); 
			Map row1 = new LinkedHashMap<String, String>(); 
			row1.put("1", "11"); 
			row1.put("2", "12"); 
			row1.put("3", "13"); 
			row1.put("4", "14"); 
			exportData.add(row1); 
			row1 = new LinkedHashMap<String, String>(); 
			row1.put("1", "21"); 
			row1.put("2", "22"); 
			row1.put("3", "23"); 
			row1.put("4", "24萨范德萨费迪"); 
			exportData.add(row1); 
			
			LinkedHashMap map = new LinkedHashMap(); 
			//设置列名
			map.put("1", "第一列-名称"); 
			map.put("2", "第二列名称"); 
			map.put("3", "第三列名称"); 
			map.put("4", "第四列名称"); 
			//这个文件上传到路径,可以配置在数据库从数据库读取,这样方便一些!
			String path = "C:/wpapi/"; 
			
			//文件名=生产的文件名称+时间戳
			String fileName = "文件导出"; 
//			String createCSVFile = CSVUtils.createCSVFile(exportData, map, path, fileName); 
//			System.out.println("文件名称:" + createCSVFile);
			
			String str = "";
		//	System.out.println("aa===" + str.length());
		} 
}

3:业务层。我导出了19个字段。exportData:经过转化后的List集合。titleMap:表头。这两个数据的数字是相互对应了,剩余的两个参数是,导出的路径和文件名

        //查询需要导出的数据
		List<TaskStoreItemDTO> dataList = mapper.export(taskId);
		List<LinkedHashMap<String, String>> exportData = new ArrayList<LinkedHashMap<String, String>>();
		for(TaskStoreItemDTO tsd:dataList) {
			LinkedHashMap<String, String> row = new LinkedHashMap<String, String>();
			if(!_isNullOrEmpty(tsd.getTabNbr())) {
				row.put("1", tsd.getTabNbr());
			}else {
				row.put("1", "");
			}
			if(!_isNullOrEmpty(tsd.getRegionName())) {
				row.put("2", tsd.getRegionName());			
			}else {
				row.put("2", "");
			}
			if(!_isNullOrEmpty(tsd.getSmallRegionName())) {
				row.put("3", tsd.getSmallRegionName());
			}else {
				row.put("3", "");
			}
			if(!_isNullOrEmpty(tsd.getClubId())) {
				row.put("4", tsd.getClubId());
			}else {
				row.put("4", "");
			}
			if(!_isNullOrEmpty(tsd.getDeptNbr())) {
				row.put("5", tsd.getDeptNbr());
			}else {
				row.put("5", "");
			}
			if(!_isNullOrEmpty(tsd.getItemNbr())) {
				row.put("6", tsd.getItemNbr());
			}else {
				row.put("6", "");
			}
			if(!_isNullOrEmpty(tsd.getSerialNbr())) {
				row.put("7", tsd.getSerialNbr());
			}else {
				row.put("7", "");
			}
			if(!_isNullOrEmpty(tsd.getUpcNbr())) {
				row.put("8", tsd.getUpcNbr());
			}else {
				row.put("8", "");
			}
			if(!_isNullOrEmpty(tsd.getGoodsName())) {
				row.put("9", tsd.getGoodsName());
			}else {
				row.put("9", "");
			}
			if(!_isNullOrEmpty(tsd.getSalesType())) {
				row.put("10", tsd.getSalesType());
			}else {
				row.put("10", "");
			}
			if(!_isNullOrEmpty(tsd.getFixtureName())) {
				row.put("11", tsd.getFixtureName());
			}else {
				row.put("11", "");
			}
			if(!_isNullOrEmpty(tsd.getOperatorTime())) {
				row.put("12", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tsd.getOperatorTime()));
			}else {
				row.put("12","");
			}
			if(!_isNullOrEmpty(tsd.getHaveFeedbackStatus())) {
				if(tsd.getHaveFeedbackStatus()==0) {
					row.put("13", "否");
				}else {
					row.put("13", "是");
				}
			}else {
				row.put("13", "");
			}
			if(!_isNullOrEmpty(tsd.getFeedbackStatus())) {
				if(tsd.getFeedbackStatus()==0) {
					row.put("14", "未拍照");
				}else {
					row.put("14", "已拍照");
				}
			}else {
				row.put("14", "");
			}
			if(!_isNullOrEmpty(tsd.getOperateReasonContent())) {
				row.put("15", tsd.getOperateReasonContent());
			}else {
				row.put("15","");
			}
			if(!_isNullOrEmpty(tsd.getDisplayStatus())) {
				if(tsd.getDisplayStatus()==0) {
					row.put("16", "待分配");
				}
				if(tsd.getDisplayStatus()==1) {
					row.put("16", "未陈列");		
				}
				if(tsd.getDisplayStatus()==2) {
					row.put("16", "已陈列");
				}
				if(tsd.getDisplayStatus()==3) {
					row.put("16", "无法陈列");
				}
			}else {
				row.put("16", "");
			}
			if(!_isNullOrEmpty(tsd.getAuditStatus())) {
				if(tsd.getAuditStatus()==0) {
					row.put("17", "未提交审核");
				}
				if(tsd.getAuditStatus()==1) {
					row.put("17", "待审核");		
				}
				if(tsd.getAuditStatus()==2) {
					row.put("17", "已审核");
				}
			}else {
				row.put("17", "");
			}
			if(!_isNullOrEmpty(tsd.getSubmitHoro())) {
				if(tsd.getSubmitHoro()==0) {
					row.put("18", "否");
				}
				if(tsd.getSubmitHoro()==1) {
					row.put("18", "是");
				}
			}else {
				row.put("18", "");
			}
			if(!_isNullOrEmpty(tsd.getRemark())) {
				row.put("19", tsd.getRemark());
			}else {
				row.put("19", "");
			}
			exportData.add(row);
		}
		LinkedHashMap<String, String> titleMap = new LinkedHashMap<String, String>();
		titleMap.put("1", "档期");
		titleMap.put("2", "大区");
		titleMap.put("3", "小区");
		titleMap.put("4", "门店号");
		titleMap.put("5", "部门号");
		titleMap.put("6", "itemNbr");
		titleMap.put("7", "陈列序号");
		titleMap.put("8", "UPC");
		titleMap.put("9", "商品名称");
		titleMap.put("10", "促销类型");
		titleMap.put("11", "陈列类型");
		titleMap.put("12", "陈列时间");
		titleMap.put("13", "是否需要拍照");
		titleMap.put("14", "是否已拍照");
		titleMap.put("15", "无法执行原因");
		titleMap.put("16", "商品状态");
		titleMap.put("17", "审核状态");
		titleMap.put("18", "是否提交总部");
		titleMap.put("19", "备注");
		//导出的路径
		String filePath=serviceConfig.getExportFeedbackPath();
		//导出的文件名称
		String filename="商品陈列清单";
		
		String newFilePath = CSVUtils.getInstance().createCSVFile(exportData, titleMap, filePath, filename);

 

 

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值