엑셀 파일로부터 db 파일을 만드는 방법.

일단 소스만 복사해둔다.

그리고 db파일을 열어볼수 있는 전용뷰어 또한 첨부로 올려둔다.

 

private Handler handler = new Handler() {
     public void handleMessage(Message msg) {
      if(msg.what == 0)
      {
       progressDialog.dismiss();

       queryListCursor("");
       setDataList();
       mStoreInfo.notifyDataSetChanged();
      }
      else if(msg.what == 1)
      {
       progressDialog.dismiss();
       Toast.makeText(StoreInfoActivity.this, "데이타를 가져오는데 에러가 발생하였습니다.", Toast.LENGTH_SHORT).show();
      }
     }
 };
 
 private void getAllData()
 {
  progressDialog = ProgressDialog.show(this, "", "데이타를 가져오고 있습니다.", true, false);
  new Thread() {
      public void run() {
     if(copyExcelFileToDB() == true)
    {
     handler.sendEmptyMessage(0);
    }
    else
    {
     handler.sendEmptyMessage(1);
    }
      }
  }.start();  
 }

 

 

private boolean copyExcelFileToDB()
 {
  Workbook workbook = null;
        Sheet sheet = null;
        boolean returnValue = true;
             
        try {
       
         InputStream is = getBaseContext().getResources().getAssets().open("1st.xls");
         workbook = Workbook.getWorkbook(is);
         
         if(workbook != null)
         {
          sheet = workbook.getSheet(0);
          
          if(sheet != null)
          {
           int nMaxColumn = 6;
           int nRowStartIndex = 0;
           int nRowEndIndex = sheet.getColumn(nMaxColumn - 1).length - 1;
           int nColumnStartIndex = 0;
           int nColumnEndIndex = 5;
          
           for(int nRow = nRowStartIndex; nRow <= nRowEndIndex; nRow++)
           {                       
            //번호, 현상호명, 배출건수, 자동, 수동, 현소재지
            
            String num = sheet.getCell(nColumnStartIndex, nRow).getContents();
            String name = sheet.getCell(nColumnStartIndex + 1, nRow).getContents();
            String total = sheet.getCell(nColumnStartIndex + 2, nRow).getContents();
            String auto = sheet.getCell(nColumnStartIndex + 3, nRow).getContents();
            String no_auto = sheet.getCell(nColumnStartIndex + 4, nRow).getContents();
            String addr = sheet.getCell(nColumnStartIndex + 5, nRow).getContents();
            
            ContentValues cv = new ContentValues();
            cv.put(StoreDBHandler.STORE_KEY_NUM, num);
            cv.put(StoreDBHandler.STORE_KEY_NAME, name);
            cv.put(StoreDBHandler.STORE_KEY_TOTAL, total);
            cv.put(StoreDBHandler.STORE_KEY_AUTO, auto);
            cv.put(StoreDBHandler.STORE_KEY_NOAUTO, no_auto);
            cv.put(StoreDBHandler.STORE_KEY_ADDRESS, addr);
            
            mGetHandler.mDb.insert(DBHandler.KEY_STORESINFO_TABLE_NAME, null, cv);
           }
  
          } else {
           Log.e(TAG, "Sheet is null");
           returnValue = false;
          }
         }
         else
         {
          Log.e(TAG, "Workbook is null");
          returnValue = false;
         }
         
        } catch (Exception e) {
         e.printStackTrace();
         returnValue = false;
        } finally {
         if(workbook != null)
         {
          workbook.close();
//          mGetHandler.DBclose();
          returnValue = true;
         }
        }
        return returnValue;
 } 




 

블로그 이미지

미네르바98

안드로이드와 영화 리뷰, 생활정보에 관한 내용을 기재합니다.

,