C # Create, read, and modify Excel

Source: Internet
Author: User

//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

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.