Use Microsoft. Ace. oledb to read Excel source code

Source: Internet
Author: User

/// <Summary>
/// Obtain Excel Data to dataset Jia Shiyi
/// </Summary>
/// <Param name = "FILENAME"> complete Excel file name </param>
/// <Param name = "isfirsttitle"> whether the first line is a title line </param>
/// <Returns> </returns>
Public static dataset getdatafromexcel (string filename, bool isfirsttitle)
{
Dataset DS = NULL;
String strconn = "provider = Microsoft. Ace. oledb.12.0; Data Source =" + filename + "; extended properties = 'excel"
+ (Filename. endswith ("xls", stringcomparison. currentcultureignorecase )? "8": "12") + ". 0; HDR =" + (isfirsttitle? "Yes": "no") + "'";
Using (oledbconnection conn = new oledbconnection (strconn ))
{
Conn. open ();
// Return the Excel schema, including the name, type, Creation Time, and modification time of each sheet.
Datatable dtsheetname = conn. getoledbschematable (oledbschemaguid. Tables, new object [] {null, "table "});
DS = new dataset ();
// String array containing the table name in Excel
For (int K = 0; k <dtsheetname. Rows. Count; k ++)
{
String strtablename = dtsheetname. Rows [k] ["table_name"]. tostring ();
String STR = "select * from [" + strtablename + "]";
Oledbdataadapter da = new oledbdataadapter (STR, Conn );
Da. Fill (DS, strtablename. Replace ("$", ""). tolower ());
Da. Dispose ();
}
If (! Isfirsttitle)
{
Foreach (datatable DT in DS. Tables)
{
For (Int J = 0; j <DT. Columns. Count; j ++)
{
DT. Columns [J]. columnname = J. tostring ();
}
}
}
Conn. Close ();
Conn. Dispose ();
}
Return Ds;
}

Welcome to: http: // 121.18.78.216, which is a demonstration platform for ease of query and analysis, workflow, content management, and project management.

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.