asp.net C # take Excel to merge cell contents

Source: Internet
Author: User
Tags foreach table definition

ASP tutorial. NET C # fetching Excel merged cell contents
Reading Excel data, populating a dataset
Connection string
String xlspath = Server.MapPath ("~/www.111cn.net/somefile.xls");
String connstr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended properties=" Excel 8.0;hdr=no;imex=1 +//Specifies that the extended property is Microsoft Excel 8.0 (97) 9.0 (2000) 10.0 (2002) and that the first row is returned as data. and read in text mode
"Data source=" + Xlspath;
String sql_f = "SELECT * from [{0}]";

OleDbConnection conn = null;
OleDbDataAdapter da = null;
DataTable tblschema = null;
Ilist<string> tblnames = null;

Initialize the connection and open the
conn = new OleDbConnection (CONNSTR);
Conn.Open ();

To get the table definition metadata for a data source
Tblschema = Conn.getschema ("Tables");
Tblschema = conn.getoledbschematable (OleDbSchemaGuid.Tables, new object[] {null, NULL, NULL, "table"});

Gridview1.datasource = Tblschema;
Gridview1.databind ();

Close connection
Conn.close ();

Tblnames = new list<string> ();
foreach (DataRow row in tblschema.rows) {
Tblnames.add ((String) row["table_name"]); Reading table names
}

Initializing adapters
da = new OleDbDataAdapter ();
Prepare data, import dataset
DataSet ds = new DataSet ();

foreach (String tblname in Tblnames) {
Da.selectcommand = new OleDbCommand (String.Format (Sql_f, tblname), conn);
try {
Da.fill (ds, Tblname);
}
catch {
Close connection
if (conn.state = = ConnectionState.Open) {
Conn.close ();
}
Throw
}
}

Close connection
if (conn.state = = ConnectionState.Open) {
Conn.close ();
}

Process each sheet of the imported dataset
Here only do show
Gridview1.datasource = Ds.tables[0];
Gridview1.databind ();

Gridview2.datasource = ds.tables[1];
Gridview2.databind ();

More Codes
// .
Here we do not need to "hard-code" the Selec statement, and we can construct the "table name" of the FROM clause dynamically, depending on the need.

Not only can you obtain information about the field name, field type, and so on in each table, but you can also obtain:
Tblschema = conn.getoledbschematable (OleDbSchemaGuid.Columns, new object[] {null, NULL, NULL, NULL});
At Ado.net 1.x, only OLE DB provided the GetOleDbSchemaTable method, and SqlClient or Orcaleclient had no corresponding method, Because the corresponding database tutorial has provided a similar function of stored procedures or system tables for application access, such as for SQL Server:select *
From Northwind.information_schema.columns
WHERE table_name = n ' customers '

And in Ado.net 2.0, each xxxconnenction implements the base class System.data.common.dbconnection Getschemal


Private DataSet Binddsfromexcel (String Strfiledir, String strdataname)
{
String strconn;
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Strfiledir + "; extended properties= ' Excel 8.0;hdr=false; Imex=1 ' ";
OleDbConnection oleconn = new OleDbConnection (strconn);
Oleconn.open ();
String sql = "SELECT * FROM [" + Strdataname + "$]";//If you don't know your name, use sheets[1]

OleDbDataAdapter oledaexcel = new OleDbDataAdapter (sql, oleconn);
DataSet oledsexcle = new DataSet ();
Oledaexcel.fill (Oledsexcle, strdataname);
Oleconn.close ();
return oledsexcle;
}

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.