C # Excel operations (2) -- Open-read Excel documents,

Source: Internet
Author: User

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
  1. 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
  1. Private Excel. Application m_excel;
  2. Private void Office_test1_Load (object sender, EventArgs e)
  3. {
  4. M_excel = new Excel. Application ();
  5. M_excel.Application.Workbooks.Add (true );
  6. Int col;
  7. For (col = 0; col <10; col ++)
  8. {
  9. M_excel.Cells [1, col + 1] = col;
  10. }
  11. // Display Excel content
  12. M_excel.Visible = true;
  13. }


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
  1. Private Excel. Application m_excel;
  2. Private Excel. Workbook m_workbook;


In addition, m_excel should be instantiated in the Onload function.

 

 

[Csharp]View plaincopy
  1. Private void OnOpen (object sender, EventArgs e)
  2. {
  3. M_workbook = m_excel.Workbooks.Open (
  4. "C: \ Users \ David _ss \ Desktop \ Project Management \ project fees collection table .xlsx ",
  5. Type. Missing,
  6. Type. Missing,
  7. Type. Missing,
  8. Type. Missing, Type. Missing );
  9. M_excel.Visible = true;
  10. }

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
  1. Private void OnShowValue (object sender, EventArgs e)
  2. {
  3. Range rng;
  4. Object obj;
  5. String str;
  6. Rng = (Excel. Range) m_excel.Cells [2, 1];
  7. Obj = rng. Value2;
  8. System. Diagnostics. Debug. WriteLine (obj. ToString ());
  9. Str = rng. NumberFormatLocal;
  10. System. Diagnostics. Debug. WriteLine (str );
  11. This. date_ctrl.Value = DateTime. FromOADate (double. Parse (obj. ToString ()));
  12. Rng = (Excel. Range) m_excel.Cells [2, 2];
  13. Obj = rng. Value2;
  14. System. Diagnostics. Debug. WriteLine (obj. ToString ());
  15. Str = rng. NumberFormatLocal;
  16. System. Diagnostics. Debug. WriteLine (str );
  17. This. serialnumber_ctrl.Text = obj. ToString ();
  18. Rng = (Excel. Range) m_excel.Cells [2, 3];
  19. Obj = rng. Value2;
  20. System. Diagnostics. Debug. WriteLine (obj. ToString ());
  21. Str = rng. NumberFormatLocal;
  22. System. Diagnostics. Debug. WriteLine (str );
  23. This. money_ctrl.Text = obj. ToString ();
  24. Rng = (Excel. Range) m_excel.Cells [2, 4];
  25. Obj = rng. Value2;
  26. System. Diagnostics. Debug. WriteLine (obj. ToString ());
  27. Str = rng. NumberFormatLocal;
  28. System. Diagnostics. Debug. WriteLine (str );
  29. This. manager_ctrl.Text = obj. ToString ();
  30. }


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
  1. Str = rng. NumberFormatLocal;
  2. 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].

Related Article

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.