回 帖 发 新 帖 刷新版面

主题:请问用c#.net怎样读取excel内部表名?

用c#.net读取excel内容代码:

string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\book1.xls;Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
Excel.Application workbook = new Excel.WorkbookClass();
string strCom = " SELECT * FROM [Sheet1$] " ;
myConn.Open ( ) ;
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
DataSet myDataSet = new DataSet ( ) ;
myCommand.Fill ( myDataSet , "[Sheet1$]" );
myConn.Close ( ) ;
dataGrid1.DataMember= "[Sheet1$]" ;
dataGrid1.DataSource = myDataSet ;

Sheet1是已经知道的内部表名,但是如果不知道怎样查询所有内部表名?谢谢![em2]

回复列表 (共2个回复)

沙发


try
            {   
           
                openFileDialog1.ShowDialog();

                oledbconn1.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source="+openFileDialog1.FileName+";Extended Properties=Excel 8.0;Persist Security Info=False";

                oledbconn1.Open(); 

                DataTable dt = oledbconn1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null, null, null, "TABLE"}); 

                foreach (DataRow dr in dt.Rows) 
                { 
                
                    comboBox1.Items.Add((String)dr["TABLE_NAME"]);
                } 

                

                //MessageBox.Show("打开成功!","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information );

            }

            catch(Exception)    //catch(Exception ex)
            {
                
                MessageBox.Show("Excel文件不能被打开!" ,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error );
            
            }
           


            textBox1.Text=openFileDialog1.FileName;
可以看看我的代码,把当前打开的EXCEL文件中所有的表都填写到comboBox控件的下拉列表中!不知对你是否有用!

板凳


谢谢!!!!

我来回复

您尚未登录,请登录后再回复。点此登录或注册