asp.net動態獲取Excel表名的代碼

複製代碼 代碼如下:

asp.net動態獲取Excel表名的代碼

public string GetExcelFirstTableName(string excelFileName)

{

string tableName = null;

if (ts(excelFileName))

{

using (OleDbConnection conn = new OleDbConnection("Provider=." +

"OLEDB.4.0;Extended Properties="Excel 8.0";Data Source=" + excelFileName))

{

();

DataTable dt = leDbSchemaTable(es, null);

for (int i = 0; i < t; i++)

{

tableName += [i][2]ring()();

view sourceprint?1 }

}

}

return tableName;

}

讀取excel動態獲取表名

複製代碼 代碼如下:

string a=ring();

string excelFilePath=a;

ication myExcel=new icationClass( ) ;

object oMissing = e ;

(excelFilePath,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing) ;

book myBook = books[1] ;

sheet mySheet = (sheet)sheets[1] ;

e();

string Name=;

Table dt=new Table("mytable");

("F1", ype("ng"));

("F2", ype("ng"));

("F3", ype("ng"));

("F4", ype("ng"));

("F5", ype("ng"));

DataSet myDs = new DataSet();

(dt);

DataRow myRow;

r();

for( int i = 2 ; i <= 4 ; i ++ ) //第一行爲標題,不讀取

{

myRow = es["mytable"]ow();

for( int j = 1 ; j <= 5 ; j ++ )

{

e r=(e)s[i,j];

string strValue=ring();

string aa=strValue;

string columnname="F"+ring();

myRow[columnname]=strValue;

}

es["mytable"](myRow);

// }

();

Source=es["mytable"]ultView;

Bind();