2014. 8. 12. 22:01 Spring/Java
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
<jquery.form.js 추가>
1 | <script src="/resources/js/jquery.form.js"></script> |
<javascript 부분>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | function checkFileType(filePath){ var fileFormat = filePath.split("."); if(fileFormat.indexOf("xls") > -1){ return true; }else{ return false; } } function check(){ var file = $("#excel").val(); if(file == "" || file == null){ alert("파일을 선택해주세요."); return false; }else if(!checkFileType(file)){ alert("엑셀 파일만 업로드 해주세요."); return false; } if(confirm("업로드 하시겠습니까?")){ $("#excelUpForm").attr("action", "/admin/compExcelUpload"); var options = { success : function(data) { alert("모든 데이터가 업로드 되었습니다."); $("#ajax-content").html(data); }, type : "POST" }; $('form').ajaxSubmit(options); } } |
3. Server에서 excel 처리-
파일을 따로 저장할 필요가 없어서, 받은 파일의 input stream으로 workbook을 만들고-
각 Column을 VO객체에 담아서 ArrayList를 만들었다-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | @RequestMapping(value = "admin/compExcelUpload") public ModelAndView excelUpload(MultipartHttpServletRequest req) throws ParseException{ mav = new ModelAndView("/admin/comp_list"); MultipartFile file = req.getFile("excel"); ArrayList<CompanyDetailVO> comp_list = new ArrayList<>(); if (file != null && file.getSize() > 0) { try { Workbook wb = new HSSFWorkbook(file.getInputStream()); Sheet sheet = wb.getSheetAt(0); int last = sheet.getLastRowNum(); System.out.println("Last : " + last); for(int i=1; i<=last; i++){ Row row = sheet.getRow(i); CompanyDetailVO company = new CompanyDetailVO(); String company_name = row.getCell(0, Row.CREATE_NULL_AS_BLANK).getStringCellValue(); if(!company_name.equals("") && company_name != null){ company.setCompany_name(company_name); company.setCompany_ceo(row.getCell(1, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); company.setIncorp_date(row.getCell(2, Row.CREATE_NULL_AS_BLANK).getDateCellValue()); company.setStaff_cnt((int) row.getCell(3, Row.CREATE_NULL_AS_BLANK).getNumericCellValue()); company.setCompany_url(row.getCell(4, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); company.setCompany_sales((int) row.getCell(5, Row.CREATE_NULL_AS_BLANK).getNumericCellValue()); company.setCompany_type(row.getCell(6, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); String zipcode = row.getCell(7, Row.CREATE_NULL_AS_BLANK).getStringCellValue(); String[] zipArray = zipcode.split("-"); company.setCompany_zipcode1(zipArray[0]); company.setCompany_zipcode2(zipArray[1]); company.setCompany_addr(row.getCell(8, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); company.setCompany_detail_addr(row.getCell(9, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); company.setCompany_telnumber(row.getCell(10, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); company.setCompany_ad_url1(row.getCell(11, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); company.setCompany_ad_url2(row.getCell(12, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); company.setCompany_ad_url3(row.getCell(13, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); company.setCompany_ad_url4(row.getCell(14, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); company.setCompany_ad_url5(row.getCell(15, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); company.setCompany_ad_url6(row.getCell(16, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); comp_list.add(company); } } } catch (IllegalStateException | IOException e) { e.printStackTrace(); } } service.insertCompExcel(comp_list); mav.addObject("comp_list", service.getCompanies()); return mav; } |
4. 추가적으로 MyBatis 다중 Insert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
BLOB으로 저장 된 이미지 출력 (0) | 2014.08.13 |
---|---|
AOP 사용, 어노테이션 (0) | 2014.08.10 |
ppt to image/pdf 변환 (0) | 2014.08.10 |
간단한 암호화, MD5, SHA1 (0) | 2014.08.10 |
MyBatis 연결 설정 (0) | 2014.08.09 |