Copy codeThe Code is as follows:
Public string GetExcelFirstTableName (string excelFileName)
{
String tableName = null;
If (File. Exists (excelFileName ))
{
Using (OleDbConnection conn = new OleDbConnection ("Provider = Microsoft. Jet." +
"OLEDB.4.0; Extended Properties = \" Excel 8.0 \ "; Data Source =" + excelFileName ))
{
Conn. Open ();
DataTable dt = conn. GetOleDbSchemaTable (OleDbSchemaGuid. Tables, null );
For (int I = 0; I <dt. Rows. Count; I ++)
{
TableName + = dt. Rows [I] [2]. ToString (). Trim ();
View sourceprint? 1}
}
}
Return tableName;
}
Asp.net reads the excel file to dynamically obtain the table name.
Copy codeThe Code is as follows:
String a = File1.PostedFile. FileName. ToString ();
String excelFilePath =;
Excel. Application myExcel = new Excel. ApplicationClass ();
Object oMissing = System. Reflection. Missing. Value;
MyExcel. Application. Workbooks. Open (audit, oMissing, oMissing );
Excel. Workbook myBook = myExcel. Workbooks [1];
Excel. Worksheet mySheet = (Excel. Worksheet) myBook. Worksheets [1];
Response. Write (mySheet. Name );
String Name = mySheet. Name;
System. Data. DataTable dt = new System. Data. DataTable ("mytable ");
Dt. Columns. Add ("F1", System. Type. GetType ("System. String "));
Dt. Columns. Add ("F2", System. Type. GetType ("System. String "));
Dt. Columns. Add ("F3", System. Type. GetType ("System. String "));
Dt. Columns. Add ("F4", System. Type. GetType ("System. String "));
Dt. Columns. Add ("F5", System. Type. GetType ("System. String "));
DataSet myDs = new DataSet ();
MyDs. Tables. Add (dt );
DataRow myRow;
MyDs. Clear ();
For (int I = 2; I <= 4; I ++) // The first behavior title, not read
{
MyRow = myDs. Tables ["mytable"]. NewRow ();
For (int j = 1; j <= 5; j ++)
{
Excel. Range r = (Excel. Range) mySheet. Cells [I, j];
String strValue = r. Text. ToString ();
String aa = strValue;
String columnname = "F" + j. ToString ();
MyRow [columnname] = strValue;
}
MyDs. Tables ["mytable"]. Rows. Add (myRow );
//}
MyExcel. Quit ();
DataGrid1.DataSource = myDs. Tables ["mytable"]. DefaultView;
DataGrid1.DataBind ();