Function Code for asp.net to dynamically obtain the Excel table name

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.