java导出.CSV文件

版权声明:内容记录学习过成文章,仅供参考 https://blog.csdn.net/qq_40195958/article/details/83897874

用到jar一个

<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.3</version>
</dependency>

代码

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

public class 生成为CVS文件 {

    /** 
     * @param exportData 源数据List
     * @param map csv文件的列表头map
     * @param outPutPath 文件路径
     * @param fileName 文件名称
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static File createCSVFile(List exportData, LinkedHashMap map, String outPutPath, String fileName) throws Exception{
        File csvFile = null;
        BufferedWriter csvFileOutputStream = null;
        try {
            File file = new File(outPutPath);
            if (!file.exists()) {
                file.mkdir();
            }
           
            csvFile = new File(outPutPath+fileName+".csv"); //File.createTempFile(fileName, ".csv", new File(outPutPath));
            //System.out.println("csvFile:" + csvFile);
            // UTF-8使正确读取分隔符","
            OutputStream os = new FileOutputStream(csvFile);  
            os.write(239);   // 0xEF  
            os.write(187);   // 0xBB  
            os.write(191);   // 0xBF       
            csvFileOutputStream = new BufferedWriter(
                    new OutputStreamWriter(
                            os, "UTF-8"),1024);
            // 写入文件头部
            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.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;
    }
}

源数据、文件列表头数据格式

//  表头
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        map.put("1", "Plant Code");
        map.put("2", "Vendor Code");
        map.put("3", "Part No");
        map.put("4", "Part Des");
        map.put("5", "Part Spec");
        map.put("6", "UOM");
        map.put("7", "MOQ");   
//    源数据        
		List<Map<String,String>> exportData = new ArrayList<Map<String,Strin-g>>();
		for(int i=0;i<list.size();i++){
			Map<String,String> map1 = new LinkedHashMap<String, String>();
			map1.put("1", list.get(i).getPlantcode());
			map1.put("2", list.get(i).getVendorcode());
			map1.put("3", list.get(i).getPartno());
			map1.put("4", list.get(i).getPartdes());
			map1.put("5", list.get(i).getPartspec());
			map1.put("6", list.get(i).getUom());
			map1.put("7", list.get(i).getMoq());
			exportData.add(map1);		
		}

猜你喜欢

转载自blog.csdn.net/qq_40195958/article/details/83897874