C # Operations Excel

Source: Internet
Author: User

The first time I was working on Excel, the code I looked up on the internet was posted directly, just to run, and not to care about the attributes in the connection string.
So in the obtained DataTable instead of the column name, the last is in the for loop set separately, and later on the Internet to find articles, found even
The HDR in the string is set to get the column name.
Value of the parameter HDR:
Hdr=yes, this means that the first row is the title, not as data use, if used Hdr=no, then the first row is not the title, as data to use. The system default is Yes
Parameter Excel 8.0 for Excel 97 to 2003 is used with Excel 8.0,2007 or 2010 for both extended Properties=excel 12.0
IMEX (IMport EXport mode) settings
There are three modes of IMEX:
0 is Export mode
1 is Import mode
2 is Linked mode (full update capabilities)
What I want to highlight here is the IMEX parameter, because different patterns represent different reading and writing behaviors:
When imex=0 is "Export mode", the Excel file opened by this mode can only be used for "write" purposes.
When Imex=1 is "Import Mode", the Excel file opened in this mode can only be used for "read" purposes.
When imex=2 is "connected mode", the Excel file opened in this mode can support both "read" and "write" purposes.
The meanings are as follows:
0---output mode;
1---input mode;
2----link mode (full update capability)

But also learned something new, such as if the current table does not exist in the column name, the default column name is F1, F2, and so on.

The actual code is:

 Public StaticSystem.Data.DataTable exceltodatatable (stringstrexcelfilename) {                       //Application Myexcel = new application (); //MyExcel.Application.Workbooks.Open (Strexcelfilename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); //This.txtFile.Text The full path of the Excel file//Microsoft.Office.Interop.Excel.Workbook myBook = myexcel.workbooks[1];            ////Get the first sheet            //Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet) mybook.sheets[1]; //string strsheetname = Sheet. Name; //Sheet name//definition of the source            stringstrconn ="provider=microsoft.jet.oledb.4.0;"+"Data source="+ Strexcelfilename +";"+"Extended properties= ' Excel 8.0; Hdr=no;imex=1 ';"; //string strexcel = "SELECT * FROM [sheet1$]"; //define the data table to be storedDataSet ds =NewDataSet (); //connecting to a data sourceOleDbConnection conn =NewOleDbConnection (strconn); Conn.            Open (); DataTable schematable= Conn. GetOleDbSchemaTable (System.Data.OleDb.OleDbSchemaGuid.Tables,NULL); stringstrSheetName = schematable.rows[0][2]. ToString ().            Trim (); //SQL statements//string strexcel = string. Format ("select * from [{0}$]", strsheetname); //This is a way            stringStrexcel =string. Format ("SELECT * from [{0}]", strSheetName);//This is a way//fit to a data sourceOleDbDataAdapter adapter =NewOleDbDataAdapter (Strexcel, strconn); Adapter.                        Fill (ds, strSheetName); Conn.            Close (); System.Data.DataTable DT=ds.            Tables[strsheetname];  for(inti =0; i < dt. Columns.count; i++) {dt. Columns[i]. ColumnName= dt. rows[0]["F"+ (i +1). ToString ()].            ToString (); }            //myexcel.displayalerts = false; //mybook.close (Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Missing.Value, Missing.Value); //sheet = null; //myBook = null; //Myexcel.quit (); //myexcel = null; //System.GC.Collect ();            returnDT; }

C # Operations Excel

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.