Java 導入數據到Excel並提供文件下載接口


作者:Howie_Y
鏈接:https://juejin.im/post/5ab4799451882521d6577fe4

最近的項目中遇到了一個將數據庫的信息導入到一個 Excel 文件的需求,而且還要提供下載該 Excel 文件的接口 ,搞定之后,進行了一下總結,希望給大家帶來幫助

源碼: https://github.com/HowieYuan/Excel-Download

依賴

<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>

我們需要用到 jxl 包的類,而 jxl.jar 正是操作 excel 表格的工具類庫,除了 jxl 以外,poi 包也是一個 操作 excel 的類庫。
而對比兩個包,jxl 更適用與數據量大的情況,而 poi 在數據量不高(大約5000以內)時,效率較高,但占用內存大,更容易內存溢出。

測試數據

private int id;
private String name;
private int age;
private String gender;

public List<Person> getPersonList() { List<Person> list = new ArrayList<>(); list.add(new Person(1, "Howie", 20, "female")); list.add(new Person(2, "Wade", 25, "male")); list.add(new Person(3, "Duncan", 30, "male")); list.add(new Person(4, "Kobe", 35, "male")); list.add(new Person(5, "James", 40, "male")); return list; } 

1. 將數據全部導入到一張表格

//創建文件本地文件
//直接將文件創建在項目目錄中
String filePath = "人員數據.xlsx"; File dbfFile = new File(filePath); //使用 Workbook 類的工廠方法創建一個可寫入的工作薄(Workbook)對象 WritableWorkbook wwb = Workbook.createWorkbook(dbfFile); //如果文件不存在,則創建一個新的文件 if (!dbfFile.exists() || dbfFile.isDirectory()) { dbfFile.createNewFile(); } //獲得人員信息 list (PersonFactroy 類已經被依賴注入) List<Person> list = personFactroy.getPersonList(); //創建一個可寫入的工作表 WritableSheet ws = wwb.createSheet("列表 1", 0); //添加excel表頭 ws.addCell(new Label(0, 0, "序號")); ws.addCell(new Label(1, 0, "姓名")); ws.addCell(new Label(2, 0, "年齡")); ws.addCell(new Label(3, 0, "性別")); int index = 0; for (Person person : list) { //將生成的單元格添加到工作表中 //(這里需要注意的是,在Excel中,第一個參數表示列,第二個表示行) ws.addCell(new Label(0, index + 1, String.valueOf(person.getId()))); ws.addCell(new Label(1, index + 1, person.getName())); ws.addCell(new Label(2, index + 1, String.valueOf(person.getAge()))); ws.addCell(new Label(3, index + 1, person.getGender())); index++; } 

 

導入數據后的 Excel 表

 

2. 將數據導入到多個表格

//前面的代碼一致

//每個工作表格最多存儲2條數據(注:excel表格一個工作表可以存儲65536條)
int mus = 2; 
//數據的總大小
int totle = list.size();
//總表格數
int avg = totle / mus + 1;
for (int i = 0; i < avg; i++) { //創建一個可寫入的工作表 WritableSheet ws = wwb.createSheet("列表" + (i + 1), i); //添加excel表頭 ws.addCell(new Label(0, 0, "序號")); ws.addCell(new Label(1, 0, "姓名")); ws.addCell(new Label(2, 0, "年齡")); ws.addCell(new Label(3, 0, "性別")); int num = i * mus; int index = 0; for (int m = num; m < list.size(); m++) { //判斷index == mus的時候跳出當前for循環 if (index == mus) { break; } Person person = list.get(m); //將生成的單元格添加到工作表中 //(這里需要注意的是,在Excel中,第一個參數表示列,第二個表示行) ws.addCell(new Label(0, index + 1, String.valueOf(person.getId()))); ws.addCell(new Label(1, index + 1, person.getName())); ws.addCell(new Label(2, index + 1, String.valueOf(person.getAge()))); ws.addCell(new Label(3, index + 1, person.getGender())); index++; } } 

這里是根據每個表的數據量來分,大家也可以根據其中一個屬性等等來分成各個表格

 

 

 

 

提供文件下載接口

該方法利用文件流來寫入文件,方法類型為 void,不需要 return,除此之外,接口參數中需要添加上 HttpServletResponse

    @RequestMapping(value = "/getExcel", method = RequestMethod.GET) public void createBoxListExcel(HttpServletResponse response) throws Exception { String filePath = "人員數據.xlsx"; /** * 這部分是剛剛導入 Excel 文件的代碼,省略 */ String fileName = new String("人員數據.xlsx".getBytes(), "ISO-8859-1"); //設置文件名 response.addHeader("Content-Disposition", "filename=" + fileName); OutputStream outputStream = response.getOutputStream(); FileInputStream fileInputStream = new FileInputStream(filePath); byte[] b = new byte[1024]; int j; while ((j = fileInputStream.read(b)) > 0) { outputStream.write(b, 0, j); } fileInputStream.close(); outputStream.flush(); outputStream.close(); } 

然后,我們直接在地址欄輸入localhost:8080/getExcel既可立刻下載你的文件

注意: String fileName = new String("人員信息.xlsx".getBytes(), "ISO-8859-1"); 我使用了 ISO-8859-1 編碼,原因是 ISO-8859-1編碼是單字節編碼,向下兼容 ASCII,而 Header 中只支持 ASCII,傳輸的文件名必須是 ASCII




免責聲明!

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



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