asp.net function code to dynamically get Excel table names

Source: Internet
Author: User
Tags add copy net range string table name tostring
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\";D ata 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 read Excel dynamic fetch table name
Copy CodeThe code is as follows:
String a=file1.postedfile.filename.tostring ();
String excelfilepath=a;
Excel.Application myexcel=new Excel.applicationclass ();
object omissing = System.Reflection.Missing.Value;
MyExcel.Application.Workbooks.Open (Excelfilepath,omissing,omissing,omissing,omissing,omissing,omissing, omissing,omissing,omissing,omissing,omissing,omissing,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 + +)//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.