java匯出產生csv檔案,java匯出csv

來源:互聯網
上載者:User

java匯出產生csv檔案,java匯出csv

首先我們需要對csv檔案有基礎的認識,csv檔案類似excel,可以使用excel開啟,但是csv檔案的本質是逗號分隔的,對比如:

txt中顯示:

修改檔案尾碼為csv後顯示如下:

在java中我們一般使用poi操作excel,匯入,匯出都可以,但是poi很消耗記憶體,尤其在匯出時,這個時候我們其實可以選擇匯出產生csv檔案,因為其跟文本差不多,所以效率很高。

簡單寫了一個實作類別,代碼如下:

 1 /** 2      *              3      *                 匯出產生csv格式的檔案 4      * @author         ccg 5      * @param          titles csv格式頭文 6      * @param          propertys 需要匯出的資料實體的屬性,注意與title一一對應 7      * @param          list 需要匯出的對象集合 8      * @return 9      * @throws         IOException10      * Created         2017年1月5日 上午10:51:4411      * @throws         IllegalAccessException 12      * @throws         IllegalArgumentException 13      */14     public static <T> String exportCsv(String[] titles,String[] propertys,List<T> list) throws IOException, IllegalArgumentException, IllegalAccessException{15         File file = new File("d:\\test.csv");16         //構建輸出資料流,同時指定編碼17         OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file), "gbk");18         19         //csv檔案是逗號分隔,除第一個外,每次寫入一個儲存格資料後需要輸入逗號20         for(String title : titles){21             ow.write(title);22             ow.write(",");23         }24         //寫完檔案頭後換行25         ow.write("\r\n");26         //寫內容27         for(Object obj : list){28             //利用反射擷取所有欄位29             Field[] fields = obj.getClass().getDeclaredFields();30             for(String property : propertys){31                 for(Field field : fields){32                     //設定欄位可見度33                     field.setAccessible(true); 34                     if(property.equals(field.getName())){35                         ow.write(field.get(obj).toString());36                         ow.write(",");37                         continue;38                     }39                 }40             }41             //寫完一行換行42             ow.write("\r\n");43         }44         ow.flush();45         ow.close();46         return "0";47     }

測試類別如下:

 1 public void test() throws IOException, IllegalArgumentException, IllegalAccessException{ 2         String[] titles = new String[]{"ID","姓名"}; 3         String[] propertys = new String[]{"id","name"}; 4         List<User> list = new ArrayList<User>(); 5         User user; 6         user = new User(); 7         user.setId(1L); 8         user.setName("張三"); 9         list.add(user);10         user = new User();11         user.setId(2L);12         user.setName("李四");13         list.add(user);14         CsvUtil.getInstance().exportCsv(titles,propertys, list);15     }

匯出後產生的檔案跟一樣,算是一個封裝吧,傳入表頭,以及表頭對應實體的屬性即可,注意要一一對應。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.