海量数据excel文件下载

目前项目有一个需求,导出下载海量数据excel数据,excel 03版本最多65536,07版1048576条,Excel2003的最大列数是256列,2007以上版本是16384列。如果要下载的excel包含上百万甚至上千万条数据,直接下载excel文件很容易发生JVM内存溢出。为了避开这个问题,把下载excel转换成下载csv文件,这样可以节省大量的内存。目前测试下载20多万条数据是没问题,上百万甚至千万的数据正在压力测试中。。。

// 生成CSV格式文本文件
            String[] title = new String[]{"类别","位置","原因"};
            String csvData = Utils.createCSVData(parseDetailDescription(result) , title);
            response.setContentType("text/plain");    
            response.addHeader("Content-Disposition", "attachment;filename=detailDescription.csv");    
            OutputStream out = response.getOutputStream();    
            OutputStreamWriter write = new OutputStreamWriter(out , "gb2312");      
            write.write(csvData);       
            write.flush();   
            return null;

/**
     * @Description 导出csv文件
     * @param exportData
     * @param title
     * @return
     */
    public static String createCSVData(List<String[]> exportData , String[] title){ 
    	if(exportData == null || exportData.size() == 0 || null == title || title.length == 0){
    		if(logger.isDebugEnabled()){
    			logger.debug("组织csv数据缺少必须参数,exportData=" + exportData + ",title=" + title);      
    		}
    		return null;
    	}
    	StringBuffer result = new StringBuffer();
    	for(int i = 0 ; i < title.length ; i++){
    		result.append(title[i]);
    		if(i == title.length - 1){
    			result.append("\r\n");
    		} else {
    			result.append(",");
    		}
    	}
    	String[] data = null;
    	for(int i = 0 ; i < exportData.size() ; i++){
    		data = (String[])exportData.get(i);
    		if(null == data){
    			continue;
    		}
    		for(int j = 0 ; j < data.length ; j++){
    			result.append(data[j]);
    			if(j == title.length - 1){
    				result.append("\r\n"); 
    			} else {
    				result.append(",");
    			}
    		}
    	}
		return result.toString();
    }

/**
     * @Description 解析错误信息
     * @param description
     * @return
     */
    private List<String[]> parseDetailDescription(String description){
    	if(StringUtils.isBlank(description)){
    		return null;
    	}
    	int errorIndex = description.indexOf("错误");
    	if(errorIndex == -1){
    		return null;
    	}
    	int warnIndex = description.indexOf("警告");
    	int infoIndex = description.indexOf("信息");
    	String errorMsg = null;
    	String warnMsg = null;
    	String infoMsg = null;   
    	if(warnIndex == -1){  
    		if(infoIndex == -1){
    			errorMsg = description.substring(errorIndex + 3);
    		} else {
    			errorMsg = description.substring(errorIndex + 3 , infoIndex);
    			infoMsg = description.substring(infoIndex + 3); 
    		}
    	} else {
    		errorMsg = description.substring(errorIndex + 3 , warnIndex);
    		if(infoIndex == -1){
    			warnMsg = description.substring(warnIndex + 3); 
    		} else {
    			warnMsg = description.substring(warnIndex + 3 , infoIndex); 
    			infoMsg = description.substring(infoIndex + 3); 
    		}
    	}
    	String[] temp = null;
    	String[] msgArr = null;
    	String[] data = null;
    	List<String[]> result = new ArrayList<String[]>();
    	if(StringUtils.isNotBlank(errorMsg)){
    		msgArr = errorMsg.split(";");
    		for(int i = 0 ; i < msgArr.length ; i++){
    			if(StringUtils.isBlank(msgArr[i])){
    				continue;
    			}
    			data = new String[3];
    			data[0] = "错误";
    			temp = msgArr[i].split(" ");
    			if(temp.length < 2){
    				data[1] = "";
    				data[2] = msgArr[i];
    			} else {
    				data[1] = temp[0];
    				data[2] = temp[1];
    			} 
    			result.add(data);
    		}
    	}
    	if(StringUtils.isNotBlank(warnMsg)){
    		msgArr = warnMsg.split(";");
    		for(int i = 0 ; i < msgArr.length ; i++){
    			if(StringUtils.isBlank(msgArr[i])){
    				continue;
    			}
    			data = new String[3]; 
    			data[0] = "警告";
    			temp = msgArr[i].split(" ");
    			if(temp.length < 2){
    				data[1] = "";
    				data[2] = msgArr[i];
    			} else {
    				data[1] = temp[0];
    				data[2] = temp[1];
    			} 
    			result.add(data);
    		}
    	}
    	if(StringUtils.isNotBlank(infoMsg)){
    		msgArr = infoMsg.split(";");
    		for(int i = 0 ; i < msgArr.length ; i++){
    			if(StringUtils.isBlank(msgArr[i])){
    				continue;
    			}
    			data = new String[3];     
    			data[0] = "信息";
    			temp = msgArr[i].split(" ");
    			if(temp.length < 2){
    				data[1] = "";
    				data[2] = msgArr[i];
    			} else {
    				data[1] = temp[0];
    				data[2] = temp[1];
    			} 
    			result.add(data);
    		}
    	}
    	return result;
    }


你可能感兴趣的:(海量数据excel文件下载)