블로그 이미지
세피롯스

calendar

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

Notice

Tag

2010. 3. 12. 12:09 초보프로그래머만..




jsp상에서 디비에 밀어넣을 엑셀파일의 url을 얻고(input type="file"을 이용해서) action단으로 가면
-------------------java소스---------------------------------
  Workbook workbook = null;
  Sheet sheet = null;

  File file = new File(request.getParameter("fileurl"));
  String yyyymmdd = request.getParameter("yyyymmdd");
   String[][] excelData = null;
  try{
   workbook = Workbook.getWorkbook(file);   //파일경로의 엑셀파일정보를 얻어오고
   sheet = workbook.getSheet(0);                 //0번째 시트의 정보를 sheet에 넣고
   int rowCount = sheet.getRows();                            //총 로우수
   int colCount = sheet.getColumns();                       //총 열의 수
   
   if(rowCount <= 0) throw new Exception("읽을 데이터가 엑셀에  없습니다.");

   excelData = new String[rowCount][colCount];

   ArrayList list = new ArrayList();
   AddressDTO addressDTO = new AddressDTO();             //입력파라메터를 담을 빈파일
   //엑셀데이터를 배열에 저장
//엑셀시트7번째줄부터 데이타가 들어가도록 설정했기때문에 i는 6부터 for문을 돌린다. 
      for(int i = 6 ; i < rowCount ; i++) {   
       addressDTO = new AddressDTO();
       for(int k = 0 ; k < colCount ; k++) {
        Cell cell =sheet.getCell(k, i);                   // 해당위치의 셀을 가져오는 부분
        if(cell != null) {
//첫번째 셀에 데이타가없으면 for문을 종료하기위한 값을 넣는다.
         if(k ==0 && cell.getContents().equals("")){
          i = rowCount;                                     
          k = colCount;
         }else{
//빈파일에 값담기
//해당행(i)의 열값(k)을 담는다
          if(k == 0) addressDTO.setName(cell.getContents());     
          if(k == 1) addressDTO.setEmail(cell.getContents());
          if(k == 2) addressDTO.setMobile(cell.getContents());
          if(k == 3) addressDTO.setTel(cell.getContents());
          if(k == 4) addressDTO.setSosok(cell.getContents());
          if(k == 5) addressDTO.setRelation(cell.getContents());
         }
        }
       }
//한 라인for문이 끝날때마다 인서트처리(한줄씩 인서트)
       scheduleBSO.insertAddress(addressDTO);         
}

posted by 세피롯스