Tool Material:
Windows 7,visual Studio, Microsoft Office 2007
Create a solution
Menu new Project Windows Forms application:
Add Related components:
Add two DataGridView, a TextBox, two buttons, such as:
To add an Excel resource:
C # creates an Excel file, where you actually extract a pre-created Excel file from the resource, and after the file extraction succeeds, you use the OLE DB method to connect to Excel and write the data to the Excel file.
Create a new Excel file in the folder and set the column name in the first row of the Sheet1 table:
Double-click the "Resources.resx" file to open the resource File view:
Add an existing file, select the Excel file you just created
Extracting an Excel file from a resource
[CSharp]View Plaincopy
- String Excelpath = AppDomain.CurrentDomain.BaseDirectory + "Excel" + DateTime.Now.Ticks + ". xlsx";
- if (System.IO.File.Exists (Excelpath))
- {
- TextBox1.Text + = ("File already exists! ");
- return;
- }
- Try
- {
- //Extract the Excel file from the resource
- System.IO.FileStream fs = new System.IO.FileStream (Excelpath, FileMode.OpenOrCreate);
- Fs. SetLength (0);
- Fs. Write (Properties.Resources.Excel, 0, Properties.Resources.Excel.Length);
- Fs. Close ();
- Fs. Dispose ();
- TextBox1.Text = "Extract Excel file successfully!" "+ " \ r \ n ";
- }
- catch (System.Exception ex)
- {
- Excelpath = string. Empty;
- TextBox1.Text + = ("Extract Excel file failed:" + ex.) Message);
- TextBox1.Text + = ("\ r \ n");
- Application.doevents ();
- return;
- }
Defining a connection string
[CSharp]View Plaincopy
- //define OLE DB connection string
- string Strconn = "provider=microsoft.ace.oledb.12.0; persist security info=false; " + "data source=" + @excelPath +
- Oledbconnection conn = new oledbconnection ();
- conn. connectionstring = strconn;
Note: The value of Imex in the connection string is 10, and if it is 1 or 2, an error " operation must use an updatable query " is reported when an INSERT INTO statement is executed.
Display information for all tables in an Excel file in DataGridView1
[CSharp]View Plaincopy
- DataTable Oledt = conn. GetOleDbSchemaTable (OleDbSchemaGuid.Tables, null);
- Datagridview1.datasource = Oledt;
- Datagridview1.show ();
Insert a few data into the "Sheet1" table, you need to add a "$" symbol after the table name to access the Excel table, the INSERT statement can not specify the column name
[CSharp]View Plaincopy
- OleDbCommand cmd = null;
- Try
- {
- ///To insert a few data into the "Sheet1" table, you need to add a "$" symbol after the table name to access the Excel table, the INSERT statement can not specify the column name
- cmd = new OleDbCommand ("Insert into [sheet1$] Values (' abc ', ' Bac ', ' 0 ', ' 123456 ', ' Test ', ' testing ', ' AA ')", conn); (a,b,c,d,e,f,g)
- Cmd. ExecuteNonQuery ();
- Cmd. ExecuteNonQuery ();
- Cmd. ExecuteNonQuery ();
- Cmd. ExecuteNonQuery ();
- Cmd. ExecuteNonQuery ();
- }
- catch (System.Exception ex)
- {
- TextBox1.Text + = ("Insert data failed:" + ex.) Message);
- TextBox1.Text + = ("\ r \ n");
- }
Display the contents of the table "Sheet1" in dataGridView2, you need to add the "$" symbol after the table name when accessing the Excel table
[CSharp]View Plaincopy
- cmd = new OleDbCommand ("select * from [sheet1$]", conn);
- OleDbDataAdapter ADP = new OleDbDataAdapter (CMD);
- DataSet ds = new DataSet ();
- Adp. Fill (DS);
- Datagridview2.datasource = ds. Tables[0];
Traversing the contents of a schema
[CSharp]View Plaincopy
- DataTable DT = conn. GetSchema ();
- for (int i = 0; i < dt. Columns.count; i++)
- {
- TextBox1.Text + = dt. Columns[i]. Caption;
- if (i + 1 < dt. Columns.count)
- {
- TextBox1.Text + = ",";
- }
- }
- for (int j = 0; j < dt. Rows.Count; J + +)
- {
- For (int i = 0; i < dt. Columns.count; i++)
- {
- if (dt. Rows[j][dt. Columns[i]]! = null)
- {
- TextBox1.Text + = dt. Rows[j][dt. Columns[i]]. ToString ();
- }
- Else
- {
- TextBox1.Text + = "null";
- }
- if (i + 1 < dt. Columns.count)
- {
- TextBox1.Text + = ",";
- }
- }
- TextBox1.Text + = ("\ r \ n");
- }
Close the Excel data connection
[CSharp]View Plaincopy
- IF (Conn. State = connectionstate.closed)
- {
- Try
- {
- Conn. Close ();
- }
- catch (System.Exception ex)
- {
- TextBox1.Text + = ("Close Excel Data connection:" + ex.) Message);
- TextBox1.Text + = ("\ r \ n");
- }
- }
Open File Directory
[CSharp]View Plaincopy
- System.Diagnostics.Process.Start ("Explorer.exe", AppDomain.CurrentDomain.BaseDirectory);
C # Create an Excel file and export the data to an Excel file