`

用JAVA导出CSV

    博客分类:
  • Java
 
阅读更多
/**
     * 导出为CVS文件
     *
     * @param exportData 需要导出的数据
     * @param titleMap 与数据相对应的标题
     * @param outPutPath 导出目录         
     */
    public static File createCSVFile(List<Map<String, String>> exportData, LinkedHashMap<String, String> titleMap, String outPutPath) {
        File csvFile = null;
        BufferedWriter csvFileOutputStream = null;
        try {
            File f=new File(outPutPath);
            if(!f.exists()){
                f.mkdirs();
            }
            csvFile = File.createTempFile("temp", ".csv", new File(outPutPath));
            // GB2312使正确读取分隔符","
            csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "GB2312"),
                    1024);
            // 写入文件头部标题
            for (Iterator propertyIterator = titleMap.entrySet().iterator(); propertyIterator.hasNext();) {
                java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
                csvFileOutputStream.write("\"" + propertyEntry.getValue().toString() + "\"");
                if (propertyIterator.hasNext()) {
                    csvFileOutputStream.write(",");
                }
            }
            csvFileOutputStream.newLine();
            // 写入文件内容
            for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
                Map row = (Map) iterator.next();
                for (Iterator propertyIterator = titleMap.entrySet().iterator(); propertyIterator.hasNext();) {
                    java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
                    csvFileOutputStream.write("\""
                            + row.get(propertyEntry.getKey().toString()).toString() + "\"");
                    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;
    }

    public static void main(String[] args) {
        List<Map<String, String>> exportData = new ArrayList<Map<String, String>>();
        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<String, String> map = new LinkedHashMap<String, String>();
        map.put("1", "第一列");
        map.put("2", "第二列");
        map.put("3", "第三列");
        map.put("4", "第四列");
        createCSVFile(exportData, map, "D:/newTest");
    }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics