1. 程式人生 > >java後端匯入excel模板和匯入excel檔案去讀資料

java後端匯入excel模板和匯入excel檔案去讀資料

模板轉載地址:https://www.cnblogs.com/zhangyangtao/p/9802948.html

直接上程式碼(我是基於ssm寫的demo,匯入檔案目前只能讀取.xls字尾的excel檔案)

1     <!--匯入的核心依賴-->
2     <dependency>
3         <groupId>net.sourceforge.jexcelapi</groupId>
4         <artifactId>jxl</artifactId>
5         <version>2.6.12</
version> 6 </dependency>

 

 1     //這是下載模板的方法
 2     @RequestMapping("/downloadExcel.do")
 3     @ResponseBody
 4     public void downloadExcel(HttpServletResponse res, HttpServletRequest req) throws Exception {
 5     //檔案的名稱
 6     String fileName = "excelTemplate.xls";
 7     ServletOutputStream out;
8 res.setContentType("multipart/form-data"); 9 res.setCharacterEncoding("UTF-8"); 10 res.setContentType("text/html"); 11 //檔案的路徑 resource/template/1.xlsx 12 String filePath = getClass().getResource("/template/" + fileName).getPath(); 13 System.out.println(filePath); 14 String userAgent = req.getHeader("User-Agent");
15 if (userAgent.contains("MSIE") || userAgent.contains("Trident")) { 16 fileName = java.net.URLEncoder.encode(fileName, "UTF-8"); 17 }else { 18 // 非IE瀏覽器的處理: 19 fileName = new String((fileName).getBytes("UTF-8"), "ISO-8859-1"); 20 } 21 filePath = URLDecoder.decode(filePath, "UTF-8"); 22 res.setHeader("Content-Disposition", "attachment;fileName=" + fileName); 23 FileInputStream inputStream = new FileInputStream(filePath); 24 out = res.getOutputStream(); 25 int b = 0; 26 byte[] buffer = new byte[1024]; 27 while ((b = inputStream.read(buffer)) != -1) { 28 // 4.寫到輸出流(out)中 29 out.write(buffer, 0, b); 30 } 31 inputStream.close(); 32 out.flush(); 33 out.close(); 34 // if (out != null) { 35 // out.flush(); 36 // out.close(); 37 // 38 //   } 39 40 } 41

匯入excel檔案讀取資料的方法

 1 @RequestMapping("/importExcel.do")
 2     public void import2(String xlsPath) throws IOException, BiffException {
 3 
 4         // 匯入已存在的Excel檔案,獲得只讀的工作薄物件
 5         FileInputStream fis = new FileInputStream(xlsPath);
 6         Workbook wk = Workbook.getWorkbook(fis);
 7         // 獲取第一張Sheet表
 8         Sheet sheet = wk.getSheet(0);
 9         // 獲取總行數
10         int rowNum = sheet.getRows();
11         System.out.println("插入總行數:" + (rowNum-2));
12         // 從資料行開始迭代每一行
13         for (int i = 2; i < rowNum; i++) {
14             // 先判斷插入的資料是否和資料庫的資料重複
15             System.out.println(sheet.getCell(0, i).getContents());
16             if (userService.findUser(sheet.getCell(0, i).getContents()) > 0) {
17                 continue;
18             }
19             User u = new User();
20             // getCell(column,row),表示取得指定列指定行的單元格(Cell)
21             // getContents()獲取單元格的內容,返回字串資料。適用於字元型資料的單元格
22             // 使用實體類封裝單元格資料
23             u.setName(sheet.getCell(0, i).getContents());
24             u.setAge(sheet.getCell(1, i).getContents());
25             u.setNickName(sheet.getCell(2, i).getContents());
26             userService.saveUser(u);
27             System.out.println("成功插入:" + sheet.getCell(0, i).getContents());
28 
29             /*
30              * // 判斷單元格的型別,單元格主要型別LABEL、NUMBER、DATE if (sheet.getCell(2, i).getType ==
31              * CellType.NUMBER) {
32              * 
33              * // 轉化為數值型單元格 NumberCell numCell = (NumberCell) sheet.getCell(2, i); //
34              * NumberCell的getValue()方法取得單元格的數值型資料 info.setRscore(numCell.getValue());
35              * 
36              * } if (sheet.getCell(3, i).getType == CellType.NUMBER) { NumberCell numCell =
37              * (NumberCell) sheet.getCell(3, i); info.setRscore(numCell.getValue); }
38              * 
39              * if (sheet.getCell(4, i).getType == CellType.DATE) { DateCell dateCell =
40              * (DateCell) sheet.getCell(4, i); // DateCell的getDate()方法取得單元格的日期型資料
41              * info.setDate(dateCell.getDate()); }
42              */
43         }
44         fis.close();
45         wk.close();
46     }