C # Methods for using ADO to update and add data to an Excel table

Source: Internet
Author: User
Tags ole
This example describes how C # uses ADO to update and add data to an Excel table. Share to everyone for your reference. The specific analysis is as follows:

Microsoft NET provides an interactive way to work with Microsoft Office programs by using ADO. The built-in OLE DB provides the ability to manipulate Excel. XLS spreadsheet. You can create a named range in Excel to determine the table name, we also need the column headings, and if the spreadsheet does not contain column headings, then you need to add them.

How do I create a named range in Excel?
With the spreadsheet open, select the data query that you want to include, including the title.
Select the Insert menu, and then name and select Define.
Enter a name for the table, and then click Add.
Close the dialog box and save the workbook.

The following example shows how to update a record and insert data, using regular SQL commands and executing commands through OLE DB.

Using system;using System.data.oledb;namespace consoleapplication1{class Program {  static void Main ()  {   String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\offices.xls; Extended Properties=excel 8.0; ";   String selectstring = "INSERT into Office VALUES (' 12345′, ' Acme Inc ', ' Acme, CA ', ' testy mctest ', ' 01234-987654′ '); Office is the named range.   OleDbConnection con = new OleDbConnection (connectionString);   OleDbCommand cmd = new OleDbCommand (selectstring, con);   Try   {    con. Open ();    Cmd. ExecuteNonQuery ();   }   catch (Exception ex)   {    Console.WriteLine (ex. Message);   }   Finally   {    con. Dispose ();   }  } }}


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.