1. 程式人生 > >Excel批量匯入(匯出同理)

Excel批量匯入(匯出同理)

  • 在做JavaWeb中新增資料使我們在所難免會遇到的,在大資料的環境下批量新增和批量刪除是必須的,而批量刪除只需要獲取到我們需要刪除的ID就可以了,在批量新增中我們就會遇到問題,問題是得到批量的資料,這就涉及到了poi技術中的Excel匯入匯出
  • 在前端中我們使用的Dropzone上傳技術,在BootStrap中使用模態框顯示Dropzone介面
  • <div class="ibox-content">
    <form id="my-awesome-dropzone" class="dropzone"
    action="${pageContext.request.contextPath}/change/assistantUpload.action">
    <div class="dropzone-previews"></div>
    <button type="submit" class="btn btn-primary pull-right">提交</button>
    </form>
    </div>

  • Dropzone技術特點就是可以將溫江拖拽到畫面裡面,然後開始js工作在form表單提交之後開始到後臺收取資料

  • 在後臺的函式上面加上如下標記 就可以保證範文到制定的函數了
  • 這裡寫圖片描述
  • 然後我們需要制定上傳的編碼格式為UTF-8這裡寫圖片描述
  • 下面就是後臺資料處理的部分

    `try {
    // 上傳得請求物件
    MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
    // 獲取上傳的檔案
    CommonsMultipartFile multipartFile = (CommonsMultipartFile) req
    .getFile(“upload”);
    // 獲取檔案的名稱 xxx.jpg xxx_1000101897.jp
    String fileName = multipartFile.getOriginalFilename();
    String suffix = fileName.substring(fileName.lastIndexOf(“.”));
    // 宣告excel檔案物件
    Workbook wb = null;
    // 獲取檔案上傳輸入流物件
    InputStream is = multipartFile.getInputStream();
    // 判斷檔案的字尾名稱
    if (“.xls”.equalsIgnoreCase(suffix)) {
    wb = new HSSFWorkbook(is);
    } else if (“.xlsx”.equalsIgnoreCase(suffix)) {
    wb = new XSSFWorkbook(is);
    }

        // 儲存的班級名稱
        List<String> clazzNames = new ArrayList<String>();
        // 儲存的學號
        List<String> studnos = new ArrayList<String>();
        // 判斷是否為null
        if (wb != null) {
            // 讀取內容
            Sheet sheet = wb.getSheetAt(0);
            List<StudentCustom> students = new ArrayList<StudentCustom>();
            // 讀取行數
            int rowNumber = sheet.getPhysicalNumberOfRows();
            // 遍歷行數
            for (int row = 1; row < rowNumber; row++) {
                StudentCustom student = new StudentCustom();
                // 獲取一行記錄物件
                HSSFRow entity = (HSSFRow) sheet.getRow(row);
                // 獲取列數
                int colNumber = entity.getPhysicalNumberOfCells();
                // 遍歷列數
                for (int col = 0; col < colNumber; col++) {
                    // 獲取指定列
                    HSSFCell cell = entity.getCell(col);
                    // 聲明當前列的值
                    String value = "";
                    Date date = null;
                    // 判斷是否為null
                    if (cell != null) {
                        // 判斷單元格型別
                        switch (cell.getCellType()) {
                        // 數字型別
                        case Cell.CELL_TYPE_NUMERIC:
                            // 判斷是否是日期型別
                            if (HSSFDateUtil.isCellDateFormatted(cell)) { // 日期型別
                                SimpleDateFormat sdf = new SimpleDateFormat(
                                        "yyyy-MM-dd");
                                date = HSSFDateUtil.getJavaDate(cell
                                        .getNumericCellValue());
    
                            } else {
                                // 獲取數字
                                Integer data = (int) cell
                                        .getNumericCellValue();
                                if (col == 1) {
                                    HSSFCellStyle cellStyle = cell
                                            .getCellStyle();
                                    String format = cellStyle
                                            .getDataFormatString();
                                    DecimalFormat f = new DecimalFormat(
                                            format);
                                    value = f.format(data);
                                } else {
                                    value = data.toString();
                                }
                            }
                            break;
                        // 判斷是否是字元型
                        case Cell.CELL_TYPE_STRING:
                            value = cell.getStringCellValue();
                            break;
                        // 判斷是否是boolean型
                        case Cell.CELL_TYPE_BOOLEAN:
                            Boolean data = cell.getBooleanCellValue();
                            value = data.toString();
                            break;
                        // 錯誤型別
                        case Cell.CELL_TYPE_ERROR:
                            // System.out.println("單元格內容出現錯誤");
                            value = "error";
                            break;
                        // 判斷是否是公式型別
                        case Cell.CELL_TYPE_FORMULA:
                            value = String.valueOf(cell
                                    .getNumericCellValue());
                            if (value.equals("NaN")) {// 如果獲取的資料值非法,就將其裝換為對應的字串
                                value = cell.getStringCellValue()
                                        .toString();
                            }
                            break;
                        case Cell.CELL_TYPE_BLANK:
                            // System.out.println("單元格內容 為空值 ");
                            value = null;
                            break;
                        default:
                            value = cell.getStringCellValue().toString();
                            break;
                        }
    
                        //System.out.println(value);
    
                        if (col == 1) {
                            studnos.add(value);
                            student.setStudno(value);
                            // 預設密碼
                            student.setPass(value);
                        } else if (col == 2) {
                            student.setName(value);
                        } else if (col == 3) {
                            student.setSex(value);
                        } else if (col == 4) {
                            student.setEnrol(date);
                        } else if (col == 5) {
                            student.setEmail(value);
                        } else if (col == 6) {
                            if (!clazzNames.contains(value)) {
                                clazzNames.add(value);
                            }
                            // 查詢設定班級的id值
                            Clazz clazz = new Clazz();
                            clazz.setName(value);
                            student.setClazz(clazz);
                        }
                    }
    
                }
                // 行結束
                students.add(student);
            }
    
            // 第一步檢測班級是否存在
            // 查詢出該使用者管理的班級
            List<Clazz> clazzs = clazzService.selectByAssistanUserId(user
                    .getId());
            // 班級的名稱集合
            List<String> names = new ArrayList<String>();
            // 遍歷班級
            for (Clazz clz : clazzs) {
                // 把班級名稱加入到集合中
                names.add(clz.getName());
            }
            // 遍歷excel中存在的班級
            for (String clazzName : clazzNames) {
                //System.out.println(clazzName);
                // 判斷excel中的班級是否 存在班級集合中
                if (!names.contains(clazzName)) {
                    throw new ClazzExitException("該使用者不負責班級,或者班級不存在");
                }
            }
    
            // 第二步檢測學號是否存在
            QueryVo vo = new QueryVo();
            int count = studentService.selectAdminStudentsCount(vo);
            vo.setStartSize(0);
            vo.setPageSize(count);
            // 查詢出所有學生資訊
            List<StudentCustom> stus = studentService.selectAdminStudents(vo);
    
            List<String> nos = new ArrayList<String>();
            // 遍歷studnos學號,
            for (StudentCustom stu : stus) {
                // 把班級名稱加入到集合中
                nos.add(stu.getStudno());
            }
            // 1.1如果excel中有重複的學號,也不可以插入 擴充套件
            for(int i =0;i<studnos.size();i++){
                for(int j=i+1;j<studnos.size();j++){
                    if(studnos.get(i)==studnos.get(j)){
                        throw new ExcelStuNoExitException(studnos.get(i)+"該學號在Excel存在重複");
                    }
                }
            }
            // 檢測查詢的學號中是否含有該班級,如果含有 就跑出異常
            for (String no : nos) {
                System.out.println(no);
                // 判斷excel中的班級是否 存在班級集合中
                if (studnos.contains(no)) {
                    System.out.println(no);
                    throw new StuNoExitException(no+"學號已存在");
                }
            }
    
            // excel解析結束
            // 按班級分好類
            Map<Clazz, List<StudentCustom>> map = new HashMap<Clazz, List<StudentCustom>>();
            // 批量儲存學生資訊
    
            for (String clazzName : clazzNames) {
                List<StudentCustom> s = new ArrayList<StudentCustom>();
                Clazz clazz = clazzService.selectClazzName(clazzName);
                for(int i=0;i<students.size();i++){
                    if(students.get(i).getClazz().getName()==clazzName)
                    {
                        students.get(i).setClazzId(clazz.getId());
                        s.add(students.get(i));
                    }
                }
                map.put(clazz, s);
            }
            transactionService.insertStudentsCustomFromExcel(map);
            // 同步更新班級的人數
    
        }
    
        out.print("{\"msg\":\"success\"}");
    } catch (ClazzExitException ce) {
        out.print("{\"msg\":\""+ce+"\"}");
    } catch (ExcelStuNoExitException ce) {
        out.print("{\"msg\":\""+ce+"\"}");
    } catch (StuNoExitException ce) {
        out.print("{\"msg\":\""+ce+"\"}");
    } catch (Exception ex) {
        out.print("{\"msg\":\""+ex+"\"}");
    } finally {
        out.flush();
        out.close();
    
  • 另外值得一提的是在前端我們的需要因要到一段js來設定傳送的介面
  • `function initUploadOptions(){
    Dropzone.options.myAwesomeDropzone = {
    method : ‘post’,
    paramName : ‘upload’,
    autoProcessQueue : false,
    uploadMultiple : false,
    maxFiles : 1,
    init : function() {
    var myDropzone = this;
    this.element.querySelector(“button[type=submit]”)
    .addEventListener(“click”, function(e) {
    e.preventDefault();
    e.stopPropagation();
    myDropzone.processQueue()
    });
    this.on(“sending”, function() {
    alert(“傳送過程中”);
    });
    this.on(“success”, function(file, response) {
    alert(file + “成功” + response);
    var obj=eval(“(“+response+”)”);
    file.previewTemplate.appendChild(document
    .createTextNode(obj.msg));
    window.location.reload(true);
    });
    this.on(“error”, function(file, response) {
    alert(file + “失敗” + response);
    })
    }
    }

    };`

  • 在這裡我們需要制定引數的名稱為upload 這樣我們在controller中渠道upload這個上傳的物件 整個過程就可以將Excel中的資料拿到了,至於怎麼存取就看個人了,你可以用list去接收這些資料
  • 上面這些就是為了實現功能,讀者可以自行在此基礎上進行分裝方法放在自己的專案上就可以使用了
  • 在此之前我還有一種方法是封裝在Util工具類中的 至於這點我就不多說了我放在下載裡 有需要的朋友自己下載吧