java匯出.CSV檔案
阿新 • • 發佈:2018-11-13
用到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); }