Excel 테이블의 데이터 가져오기

11888 단어 Excel
첫 번째 단계: 가져온 파일 변환
  private void btnSelectFile_Click(object sender, EventArgs e)
   {
       OpenFileDialog ofd = new OpenFileDialog();
       ofd.Title = "Excel ";
       ofd.FileName = "";
       ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
       ofd.Filter = "Excel (*.xls)|*.xls";
       ofd.ValidateNames = true;
       ofd.CheckFileExists = true;
       ofd.CheckPathExists = true;
       if (openFileDialog.ShowDialog() == DialogResult.OK)
       {
           txtFileName.Text = openFileDialog.FileName;
       }
 
   }

2단계: 가져오기 수행
    private void btnImport_Click(object sender, EventArgs e)
      {
          if (cboSaleStock.SelectedIndex == -1)
          {
              MessageBox.Show(" ");
              return;
          }
          if (cboShop.SelectedIndex == -1)
          {
              MessageBox.Show(" ");
              return;
          }
          if (string.IsNullOrEmpty(txtFileName.Text) || txtFileName.Text.Length == 0)
          {
              MessageBox.Show(" Excel ! ");
              return;
          }
          DataTable dt = ExcelToDataTable(txtFileName.Text);
          string sql = string.Empty;
          for (int i = 0; i < dt.Rows.Count; i++)
          {
              sql += string.Format(@" declare @sno{4} int,@dgrID{4} varchar(20)
                                      set @dgrID{4}=(select top 1 dgoodsresultid from tbdGoodsResult where shopID='{0}' and stockid='{1}' and goodsid='{2}')
                                      if @dgrID{4} is null begin
                                          set @sno{4}=(select top 1 sno from tbpKeyManager where tablename='tbdGoodsResult')
                                          if @sno{4} is null begin
                                              insert into tbpKeyManager(tablename,sno,trdate) values('tbdGoodsResult',1,CONVERT(varchar(12),getdate(),112 ))
                                              set @sno{4}=1
                                          end else begin 
                                              set @sno{4}=@sno{4}+1 
                                              update tbpKeyManager set sno=sno+1 where tablename='tbdGoodsResult'
                                          end
                                          insert into tbdGoodsResult(dgoodsresultid,shopid,stockid,goodsid,pthismonthqty) 
                                          values(cast(@sno{4} as varchar(11)),'{0}','{1}','{2}','{3}')
                                      end else begin
                                          update tbdGoodsResult set pthismonthqty={3} where dgoodsresultid=@dgrID{4}
                                      end", cboShop.SelectedValue, cboSaleStock.SelectedValue, dt.Rows[i][2], dt.Rows[i][1], i);
          }
          string msg = "";
          if (ws.ExecuteNonQueryWithTrans(sql, ref msg) > 0)
          {
              MessageBox.Show(" ");
          }
          if (msg.Length > 0)
          {
              MessageBox.Show(" :\r
" + msg); } }

3단계: Excel 데이터 가져오기
      private DataTable ExcelToDataTable(string filePath)
      {
          // Excel DataSet 
          string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";// Excel , 
          OleDbConnection conn = new OleDbConnection(strConn);
          conn.Open();
          string strExcel = "";
          OleDbDataAdapter myCommand = null;
          DataSet ds = null;
          strExcel = "select  * from   [sheet1$]";
          myCommand = new OleDbDataAdapter(strExcel, strConn);
          ds = new DataSet();
          myCommand.Fill(ds, "table1");
          DataTable dt = ds.Tables[0];
          dt.Columns.Add("goodsid");
          string joinSql = "select '' pluno";
          foreach (DataRow dr in dt.Rows)
          {
              joinSql += string.Format(" union all select '{0}' pluno", dr[0]);
          }
          string sql = string.Format(@"select tbgoods.goodsid,tbgoods.pluno from tbgoods join ({0}) as t on t.pluno=tbgoods.pluno ", joinSql);
          DataTable dtGoods = cf.GetDataSet(sql).Tables[0];
          foreach (DataRow dr in dt.Rows)
          {
              foreach (DataRow dr1 in dtGoods.Rows)
              {
                  if (dr[0].ToString().ToUpper() == dr1["pluno"].ToString())
                  {
                      dr["goodsid"] = dr1["goodsid"].ToString();
                  }
              }
          }
          return ds.Tables[0];
      }

좋은 웹페이지 즐겨찾기