//namespaces, Variables, and ConstantsusingSystem;usingSystem.Configuration;usingSystem.Data;PrivateOleDbDataAdapter da;PrivateDataTable DT;Private voidExcel_load (Objectsender, System.EventArgs e) { //Create the DataAdapter.Da =NewOleDbDataAdapter ("SELECT * FROM [sheet1$]", configurationsettings.appsettings["ExcelConnectString1"]); //Create the insert command.String Insertsql ="INSERT into [sheet1$] (CategoryID, CategoryName, Description) VALUES (?,?,?)"; Da. InsertCommand=NewOleDbCommand (Insertsql, Da. SelectCommand.Connection); Da. INSERTCOMMAND.PARAMETERS.ADD ("@CategoryID", OleDbType.Integer,0,"CategoryID"); Da. INSERTCOMMAND.PARAMETERS.ADD ("@CategoryName", OleDbType.Char, the,"CategoryName"); Da. INSERTCOMMAND.PARAMETERS.ADD ("@Description", OleDbType.VarChar, -,"Description"); //Create the update command.String Updatesql ="UPDATE [sheet1$] SET categoryname=?, description=?"WHERE categoryid=?";Da. UpdateCommand =NewOleDbCommand (Updatesql, Da. SelectCommand.Connection); Da. UPDATECOMMAND.PARAMETERS.ADD ("@CategoryName", OleDbType.Char, the,"CategoryName"); Da. UPDATECOMMAND.PARAMETERS.ADD ("@Description", OleDbType.VarChar, -,"Description"); Da. UPDATECOMMAND.PARAMETERS.ADD ("@CategoryID", OleDbType.Integer,0,"CategoryID"); //Fill The table from the Excel spreadsheet.DT =NewDataTable (); Da. Fill (DT); //Define the primary key.Dt. PrimaryKey =NewDatacolumn[] {dt. columns[0]}; //Records can only is inserted using this technique.Dt. Defaultview.allowdelete =false; Dt. Defaultview.allowedit=true; Dt. Defaultview.allownew=true; //Bind The default view of the table to the grid.Datagrid.datasource =dt. DefaultView;}Private voidUpdatebutton_click (Objectsender, System.EventArgs e) {da. Update (DT);}//The code snippet comes from:http://www.sharejs.com/codes/csharp/7754
C # Create, read, and modify Excel