CSV Excel 부호

2235 단어
public class CsvExport {
private static final String SPLIT_LINE_SEPARATOR = "
"; private List headList; private List> dataList; private boolean isFirst = false; private String fileUrl; private CSVPrinter csvFilePrinter; private FileWriter fileWriter; private void init(){ File file = new File(fileUrl); try { if (!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } fileWriter = new FileWriter(fileUrl); //BOM UTF-8 Excel byte[] uft8bom={(byte)0xef,(byte)0xbb,(byte)0xbf}; fileWriter.write(new String(uft8bom)); } catch (IOException e) { log.error(" :", e); } try { csvFilePrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withRecordSeparator(SPLIT_LINE_SEPARATOR)); } catch (IOException e) { log.error(" csv :", e); } } public void close(){ try { fileWriter.close(); csvFilePrinter.close(); } catch (IOException e) { log.error(" :", e); } } public CsvExport(List headList, List> dataList, boolean isFirst, String fileUrl) { this.headList = headList; this.dataList = dataList; this.isFirst = isFirst; this.fileUrl = fileUrl; init(); } public static CsvExport getInstance(List headList, List> dataList, boolean isFirst, String fileUrl){ return new CsvExport(headList, dataList, isFirst, fileUrl); } public void addList(List> list){ this.dataList.addAll(list); } public void exportCsv(){ if (isFirst && CollectionUtils.isNotEmpty(headList)){ try { csvFilePrinter.printRecord(headList); } catch (IOException e) { log.error(" csv :", e); } isFirst = false; headList = null; } if(CollectionUtils.isEmpty(dataList)){ return ; } dataList.forEach(d -> { try { csvFilePrinter.printRecord(d); } catch (IOException e) { log.error(" csv :", e); } }); dataList.clear(); return ; }

}

좋은 웹페이지 즐겨찾기