java使用CsvPrinter導出csv


//創建輸出流,防止Excel打開亂碼

public static CSVPrinter getCsvPrinter(String fileName, String[] header) throws Exception {
    OutputStream out = new FileOutputStream("地址"+ fileName);
CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(header);
OutputStreamWriter osw = new OutputStreamWriter(out);
// 防止Excel打開亂碼
byte[] bytes = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
osw.write(new String(bytes));
return new CSVPrinter(osw, csvFormat);
}
int count = 數據大小
limit(默認)與count取小
CSVPrinter csvPrinter = CsvExportUtil.getCsvPrinter(fileName, "表頭");
for (int i = 0; i < (int) Math.ceil(count * 1.0 / limit); i++) {
  //進行分頁獲取
List<> list = xxxMapper.getList(條件, i * limit, limit);
exportCommon(list, csvPrinter);
}
csvPrinter.flush();
//記得關閉
csvPrinter.close();
return fileName;

exportCommon()方法就是將傳進來的實體類導出到csv中
其中
csvPrinter.printRecord(可以按你表頭的順序放入值,也可以放入list,注意順序就行)行

csvPrinter.printRecords(可以按你表頭的順序放入值,也可以放入list,注意順序就行)列


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM