Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
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
Tags
more
Archives
Today
Total
관리 메뉴
[닫기]

개발자고생한다

첫번째 Sheet의 Excel Data 읽어오기 본문

카테고리 없음

첫번째 Sheet의 Excel Data 읽어오기

smallbutdeep 2017. 10. 25. 08:21

작업을 하다가 첫번째 Sheet의 데이터를 읽어 오는 일이 있었다.

기존에 대부분  작업은 Sheet이름이 있어 그 이름으로 쿼리를 해서

읽어왔는데 Sheet이름이 유동적으로 변경되어

다음과 같이 했다.

 string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=\"Excel 8.0;Imex=1;HDR=No;\"", path);
            var ds = new DataSet();
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                var dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { nullnullnull"TABLE" });
                string sheet1 = dtSchema.Rows[0].Field<string>("TABLE_NAME");
                OleDbCommand cmd = new OleDbCommand(String.Format("SELECT * FROM [{0}]", sheet1), conn);
                OleDbDataAdapter adp = new OleDbDataAdapter();
                adp.SelectCommand = cmd;
                adp.Fill(ds);
            }
            return ds;
Comments