1. 程式人生 > 其它 >JAVA報表開發-POI處理EXCEL-匯入

JAVA報表開發-POI處理EXCEL-匯入

1、實現使用者資料的匯入

把以下資料匯入到資料庫裡面

2、實現使用者資料的匯入

 public void uploadExcel(MultipartFile file) throws Exception {
        if(file.isEmpty()){
            return;
        }
        InputStream inputStream = file.getInputStream();
        //使用者名稱  手機號  省份  城市  工資 入職日期 出生日期 現住地址
        //有內容的workbook工作薄
        org.apache.poi.ss.usermodel.Workbook workbook = new
XSSFWorkbook(inputStream); //獲取到第一個工作表(sheet) Sheet sheet = workbook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum();//當前sheet的最後一行的索引值 Row row=null; SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd"); // List<User> userList = new ArrayList<>();
String phone=""; //讀取工作表裡的內容 for (int i=1;i<=lastRowNum;i++){ User user = new User(); row = sheet.getRow(i); String userName = row.getCell(0).getStringCellValue();//使用者名稱 try { phone = row.getCell(1).getStringCellValue(); }
catch (IllegalStateException e) { phone = row.getCell(1).getNumericCellValue()+""; } String province = row.getCell(2).getStringCellValue();//省份 String city = row.getCell(3).getStringCellValue();//城市 Integer salary = ((Double) row.getCell(4).getNumericCellValue()).intValue();//工資 Date hireDate = simpleDateFormat.parse( row.getCell(5).getStringCellValue());//入職日期 Date birthDay = simpleDateFormat.parse(row.getCell(6).getStringCellValue()) ;//出生日期 String address = row.getCell(7).getStringCellValue();//現住地址 user.setUserName(userName); user.setPhone(phone); user.setProvince(province); user.setCity(city); user.setSalary(salary); user.setHireDate(hireDate); user.setBirthday(birthDay); user.setAddress(address); // userList.add(user); userMapper.insert(user); System.out.println(user.toString()); } // System.out.println(userList.toString()); }

如果匯入中文亂碼 需設定資料庫連線串後加

?serverTimezone=GMT%2B8&characterEncoding=utf-8