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 (); } } }}