Java 匯入資料到Excel並提供檔案下載介面

Howie_Y發表於2018-03-23

最近的專案中遇到了一個將資料庫的資訊匯入到一個 Excel 檔案的需求,而且還要提供下載該 Excel 檔案的介面 ,搞定之後,進行了一下總結,希望給大家帶來幫助

原始碼: github.com/HowieYuan/E…

依賴

<!-- 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++;
    }
}
複製程式碼

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

Java 匯入資料到Excel並提供檔案下載介面

Java 匯入資料到Excel並提供檔案下載介面

提供檔案下載介面

該方法利用檔案流來寫入檔案,方法型別為 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

相關文章