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();