C # create an Excel file and export the data to an Excel file,
Tool raw materials:
Windows 7, Visual Studio 2010, Microsoft Office 2007
Create a solution
Menu> new project> Windows Forms Application:
Add related components:
Add two dview datagri, one TextBox and two buttons, for example:
Add an Excel Resource:
C # create an Excel file. Here, a previously created Excel file is extracted from the resource. After the file is extracted successfully, use the OleDb method to connect to Excel and write data to the Excel file.
Create an 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 and select the created Excel File
Extract Excel files from resources
[Csharp]View plaincopy
- String excelPath = AppDomain. CurrentDomain. BaseDirectory + "Excel" + DateTime. Now. Ticks + ". xlsx ";
- If (System. IO. File. Exists (excelPath ))
- {
- TextBox1.Text + = ("the file already exists! ");
- Return;
- }
- Try
- {
- // Extract an 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 = "Excel file extracted! "+" \ R \ n ";
- }
- Catch (System. Exception ex)
- {
- ExcelPath = string. Empty;
- TextBox1.Text + = ("Excel file extraction failed:" + ex. Message );
- TextBox1.Text + = ("\ r \ n ");
- Application. DoEvents ();
- Return;
- }
Define the connection string
[Csharp]View plaincopy
- // Define the OleDB connection string
- String strConn = "Provider = Microsoft. ace. oleDb.12.0; Persist Security Info = False; "+" data source = "+ @ excelPath +"; Extended Properties = 'excel 12.0; HDR = yes; IMEX = 10 '";
- OleDbConnection conn = new OleDbConnection ();
- Conn. ConnectionString = strConn;
Note: The IMEX value in the connection string uses 10. If it is 1 or 2, the"The operation must use an updatable query..
Display information of all tables in the Excel file in dataGridView1
[Csharp]View plaincopy
- DataTable oleDt = conn. GetOleDbSchemaTable (OleDbSchemaGuid. Tables, null );
- DataGridView1.DataSource = oleDt;
- DataGridView1.Show ();
Insert several pieces of data into the "Sheet1" table. When accessing an Excel table, add the "$" symbol after the table name. The Insert statement can not specify the column name.
[Csharp]View plaincopy
- OleDbCommand cmd = null;
- Try
- {
- // Insert several pieces of data into the "Sheet1" table. to access an Excel table, add the "$" symbol after the table name. The Insert statement can not specify the column name.
- Cmd = new OleDbCommand ("Insert Into [Sheet1 $] Values ('abc', 'bac ', '0', '123', 'test', 'test ', 'A') ", conn); // (A, B, C, D, E, F, G)
- Cmd. ExecuteNonQuery ();
- Cmd. ExecuteNonQuery ();
- Cmd. ExecuteNonQuery ();
- Cmd. ExecuteNonQuery ();
- Cmd. ExecuteNonQuery ();
- }
- Catch (System. Exception ex)
- {
- TextBox1.Text + = ("failed to insert data:" + ex. Message );
- TextBox1.Text + = ("\ r \ n ");
- }
The content of the table "Sheet1" is displayed in dataGridView2. to access an Excel table, add the "$" symbol after the table name.
[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];
Traverse Schema content
[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 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 the file directory
[Csharp]View plaincopy
- System. Diagnostics. Process. Start ("assumer.exe", AppDomain. CurrentDomain. BaseDirectory );
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.