//創建輸出流,防止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,注意順序就行)列