解决Excel导入时必须默认Sheet1工作簿问题

解决方式如下代码:

FileName:是要导入的excel文件全称(带所在服务期端路径如:C:\Inetpub\wwwroot\Sanita\Web\Excel\abc.xls)

代码
OleDbConnection myOleDbConnection = new  OleDbConnection( " Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " +  FileName  + " ; Extended Properties=Excel 8.0; " );
                myOleDbConnection.Open();

                DataTable dtSheetName
= myOleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new   object []{ null , null , null , " Table " });
          
string []strTableNames = new   string [dtSheetName.Rows.Count];
          
for ( int  k = 0 ;k < dtSheetName.Rows.Count;k ++ )
          {
            strTableNames[k]
= dtSheetName.Rows[k][ " TABLE_NAME " ].ToString();
          }
                OleDbCommand myOleDbCommand
= new  OleDbCommand( " Select * from [ " +  strTableNames[ 0 + " ] " ,myOleDbConnection);
                System.Data.OleDb.OleDbDataReader dtrReader;
                dtrReader
= myOleDbCommand.ExecuteReader(); 

                System.Collections.ArrayList lstList 
=   new  System.Collections.ArrayList();    
                
while (dtrReader.Read())

 

 

你可能感兴趣的:(Excel)