ArrayList/MySQL數據集合寫入Excel 1.文章概述: 寫入 Excel 文件通常需要使用一些庫或工具,而"EasyExcel"通常是指的阿裡巴巴開源的EasyExcel庫。這個庫可以讓我們在Java中簡便地進行Excel文件的讀寫操作。 2.導入配置: <dependency> <g ...
ArrayList/MySQL數據集合寫入Excel 1.文章概述: 寫入 Excel 文件通常需要使用一些庫或工具,而"EasyExcel"通常是指的阿裡巴巴開源的EasyExcel庫。這個庫可以讓我們在Java中簡便地進行Excel文件的讀寫操作。 2.導入配置:
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> </dependency>
3.Excel模板類
package com.ccc.bean.tools_enty; import com.alibaba.excel.annotation.ExcelProperty; import lombok.Data; import lombok.NoArgsConstructor; @NoArgsConstructor @Data public class WriteDataBean implements Comparable { // Excel標頭名稱 @ExcelProperty("a") private String a; @ExcelProperty("b") private String b; public String getA() { return a; } public void setA(String a) { this.a = a; } public String getB() { return b; } public void setB(String b) { this.b = b; } @Override public int compareTo(Object o) { // //可設置排序 // if (o instance of WriteDataBean){
// WechatOfficialAccountBean wechatOfficialAccountBean= (WechatOfficialAccountBean) o;
// return this.getUpload_time().hashCode()-wechatOfficialAccountBean.getUpload_time().hashCode();
// }
throw new ClassCastException("不能轉換 WriteDataBean ");
}
}
4.工具類示例代碼:
package com.ccc.tools; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; import com.felephantst.bean.tools_enty.WriteDataBean; import java.io.*; import java.util.ArrayList; import java.util.List; /** * @ClassName: FileTool * @Description TODO 操作文件工具類 * @Author: 東霖 * @Date: 2023/9/12 13:19 * @Version 1.0 **/ public class FileTool { /** * 讀取指定文件 * * @param filePath * @return 返回一個字元串集合 */ public static List<String> readFile(String filePath) { List<String> stringList = new ArrayList<>(); try { FileReader fr = new FileReader(filePath); BufferedReader bf = new BufferedReader(fr); String str; // 按行讀取字元串 while ((str = bf.readLine()) != null) { stringList.add(str); } bf.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } return stringList; } /** * TODO: 小數據量寫出數據到 Excel(2000條左右用這個) * * @param bean 模板類 * @param outFilePath 輸出路徑全稱:test.xlsx * @param sheetName excel Sheet 名稱 * @param collect 數據集 */ public static void writeMinDataExcel(Class<?> bean, String outFilePath, String sheetName, ArrayList collect) { OutputStream outputStream = null; try { outputStream = new FileOutputStream(outFilePath); EasyExcel.write(outputStream, bean).sheet(sheetName).doWrite(collect); //寫出到文件 outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * TODO: 大數據量寫出數據到 Excel(2000條左右用這個) * * @param bean 模板類 * @param outFilePath 輸出路徑全稱:test.xlsx * @param sheetName excel Sheet 名稱 * @param sizeLimitDataList 數據集 * @param sizeLimit 設置多少條數據為一個Sheet */ public static void writeMaxDataExcel(Class<?> bean, String outFilePath, String sheetName, ArrayList sizeLimitDataList, Integer sizeLimit) { // 總Sheet數 int num = sizeLimitDataList.size() / sizeLimit + (sizeLimitDataList.size() % sizeLimit > 0 ? 1 : 0); // 輸出流 OutputStream outputStream = null; try { // 以位元組流的形式輸出響應正文 outputStream = new FileOutputStream(outFilePath); try (ExcelWriter excelWriter = EasyExcel.write(outputStream, bean).build()) { // 中間list調用寫入 List<?> partList = null; WriteSheet writeSheet =null; for (int i = 0; i < num; i++) { // 每次寫入都要創建WriteSheet, 這裡註意必須指定sheetNo, 並且sheetName必須不一樣 writeSheet = EasyExcel.writerSheet(i, sheetName + (i + 1)).build(); // 截取批次長度的list partList = sizeLimitDataList.subList(0, sizeLimit); // 分批業務邏輯處理- 列印替代 excelWriter.write(partList, writeSheet); // 去除已經處理的部分 (Arrays.asList()方式生成的數據不能進行此修改操作,會報錯) partList.clear(); } // 獲取最後一次截取後的剩餘列表數據 if (!sizeLimitDataList.isEmpty()) { // 業務邏輯數據處理 excelWriter.write(sizeLimitDataList, writeSheet); } } } catch (IOException ex) { throw new RuntimeException(ex); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException ex) { throw new RuntimeException(ex); } } } } public static void main (String[]args){ WriteDataBean writeDataBean = new WriteDataBean(); writeDataBean.setA("a"); writeDataBean.setB("b"); WriteDataBean writeDataBean1 = new WriteDataBean(); writeDataBean1.setA("ad"); writeDataBean1.setB("bd"); objects.add(writeDataBean1); objects.add(writeDataBean); writeMaxDataExcel(WriteDataBean.class, "D://cdc.xlsx", "test", objects, 1); writeMinDataExcel(WriteDataBean.class, "D://cddc.xlsx", "test", objects); } }
本文來自博客園,作者:zhuzhu&you,轉載請註明原文鏈接:https://www.cnblogs.com/zhuzhu-you/p/17697195.html