C # Excel operations (2) -- Open-read Excel documents,
This article describes how to export an Excel file for a software.
The development environment of this article is Visual Studio 2010, C #, Excel 2007.
After creating a new C # project, open Solution Explorer. You can see the example:
Right-click Reference> Add References> Browse
Select three DLL files (Click here to download ):
Interop. Excel. dll
Interop. Microsoft. Office. Core. dll
Interop. VBIDE. dll
After adding the above three references, the content in the Reference tag is as follows:
You can see that the three DLL files you just added have been referenced. OK. Now you can start writing code.
First, add an Excel namespace in Form1.cs
[Csharp]View plaincopy
- Using Excel;
1. Create an Excel file and write content to it
Add the Load message response function for Form1 as follows:
The corresponding Office_test1_Load function is as follows:
[Csharp]View plaincopy
- Private Excel. Application m_excel;
- Private void Office_test1_Load (object sender, EventArgs e)
- {
- M_excel = new Excel. Application ();
- M_excel.Application.Workbooks.Add (true );
- Int col;
- For (col = 0; col <10; col ++)
- {
- M_excel.Cells [1, col + 1] = col;
- }
- // Display Excel content
- M_excel.Visible = true;
- }
Compile, run, and get the following results:
You can see that a new Excel document has been created. Next let's take a look at how to open an existing Excel document.
2. Open an existing Excel document and read the content.
Before reading an Excel document, take a look at the document to be read.
, First behavior title, second behavior data. In addition, note that the content type of the first column is "date", and the type of the third column is: "value".
According to common sense, first add an Open button corresponding to the OnOpen () listener.
Class member definition:
[Csharp]View plaincopy
- Private Excel. Application m_excel;
- Private Excel. Workbook m_workbook;
In addition, m_excel should be instantiated in the Onload function.
[Csharp]View plaincopy
- Private void OnOpen (object sender, EventArgs e)
- {
- M_workbook = m_excel.Workbooks.Open (
- "C: \ Users \ David _ss \ Desktop \ Project Management \ project fees collection table .xlsx ",
- Type. Missing,
- Type. Missing,
- Type. Missing,
- Type. Missing, Type. Missing );
- M_excel.Visible = true;
- }
Next, we will read the document.
To display the content, I added several new controls, as shown in
Then we complete the message response function OnShowValue () corresponding to the "show value" button ()
[Csharp]View plaincopy
- Private void OnShowValue (object sender, EventArgs e)
- {
- Range rng;
- Object obj;
- String str;
- Rng = (Excel. Range) m_excel.Cells [2, 1];
- Obj = rng. Value2;
- System. Diagnostics. Debug. WriteLine (obj. ToString ());
- Str = rng. NumberFormatLocal;
- System. Diagnostics. Debug. WriteLine (str );
- This. date_ctrl.Value = DateTime. FromOADate (double. Parse (obj. ToString ()));
- Rng = (Excel. Range) m_excel.Cells [2, 2];
- Obj = rng. Value2;
- System. Diagnostics. Debug. WriteLine (obj. ToString ());
- Str = rng. NumberFormatLocal;
- System. Diagnostics. Debug. WriteLine (str );
- This. serialnumber_ctrl.Text = obj. ToString ();
- Rng = (Excel. Range) m_excel.Cells [2, 3];
- Obj = rng. Value2;
- System. Diagnostics. Debug. WriteLine (obj. ToString ());
- Str = rng. NumberFormatLocal;
- System. Diagnostics. Debug. WriteLine (str );
- This. money_ctrl.Text = obj. ToString ();
- Rng = (Excel. Range) m_excel.Cells [2, 4];
- Obj = rng. Value2;
- System. Diagnostics. Debug. WriteLine (obj. ToString ());
- Str = rng. NumberFormatLocal;
- System. Diagnostics. Debug. WriteLine (str );
- This. manager_ctrl.Text = obj. ToString ();
- }
The code above corresponds to the data display of the four controls.
The following is the result:
In addition, in the code above, I also output debugging information, that is, the Data Type of each cell:
[Csharp]View plaincopy
- Str = rng. NumberFormatLocal;
- System. Diagnostics. Debug. WriteLine (str );
Debugging output result:
The preceding results show that the date format corresponds to a digital string. Therefore, the Code uses the FromOADate method of DateTime to parse the date.
The date format is yyyy/m/d.
The general cell type is G/General Format
Value format: 0.00 _); [Red] (0.00 ).
--------------------------------------------------------- Gorgeous split -------------------------------------------
To learn more, first learn about the Excel object model. refer to the previous article:
Of course, you can also download the corresponding documentation: csung operation excel2007&excelobject model example
C language ^ how to use
A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010
B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].
C language ^ how to use
A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010
B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].