Two days of struggle against Excel !!!

Source: Internet
Author: User

The project we took over was the development of several educational software.

Three days ago, the lead assigned me a new task.

The customer asked to use Excel to count students' learning time, learning experiences, and so on.

Did the leaders ask me if I have done this? I used to output B/s Response to Excel, and I agreed with it (now I want to flat my bad face when I saw the two words.

But I almost vomited blood in the last two days.

Microsoft. Office. Interop. Excel;

The specific implementation code is everywhere on the Internet. Don't post the outdated code.

However, I hate to use ado.net to Operate Excel. it's really awkward.

Focus...

First, the Excel table can be divided into multiple Sheet. Customer requirements: for example, click learning experiences to display the Sheet of learning experiences. Click others to display the corresponding Sheet.

Is it easy? It should be a small setting of an attribute.

Not, I'm ending it.

This attribute cannot be found at all. There is a Select () method, regardless of the usage.

I spent a day trying a variety of methods. I went home to the balcony at night and sighed in the sky. Oh, heaven, please give a woman a boost. Cough... Sorry blew through

But it does hurt a lot of brain cells. (shut up)

The next day, a word that my colleague did not intend to give me a sigh of relief (se)

Macro

That's right. The method I use is macro, a tool in Excel-Macro.

In the module * Editor, type:

Sub StudyFind ()
Sheets ("Learning Experiences"). Select
End Sub

Create

Sub StudyTime ()
Sheets ("learning time"). Select
End Sub

After the creation is complete, let's see the call in C.

 

Code
1 private void button2_Click (object sender, EventArgs e)
2 {
3 ApplicationClass xapp = new ApplicationClass ();
4 string filePath = "E :\\ Demo \ Files \ Note.xls ";
5
6 Workbook xbook = xapp. Workbooks. Open (filePath,
7 Missing. Value, Missing. Value
8, Missing. Value, Missing. Value
9, Missing. Value, Missing. Value );
10 xbook. OnSheetActivate = "StudyFind"; // call a macro function
11
12 Process. Start (filePath); // open an Excel file
13}

The Excel file is displayed on the specified Sheet by default,

But there is a prompt in Excel, prompting you ...? If you don't try again, it will be in vain.

 

Second, for example, before modifying an Excel file, the Excel file is open and should be closed after modification. Question:

The Excel component of the office provides a Quit () method to close the Excel file, but the process is still here when you open the process to view it,

Go to Google and find the following code.

 

Code
1
2 [DllImport ("User32.dll", CharSet = CharSet. Auto)]
3 public static extern int GetWindowThreadProcessId (IntPtr hwnd, out int ID );
4 public static void Kill (ApplicationClass excel)
5 {
6 IntPtr t = new IntPtr (excel. Hwnd); // obtain the handle. The specific function is to obtain the memory entry.
7 int k = 0;
8 GetWindowThreadProcessId (t, out k); // obtain the unique identifier k of the process.
9 Process p = Process. GetProcessById (k); // get a reference to Process k
10 p. Kill (); // close process k
11}

 

This code solves this problem, and the process is closed accurately with the identifier.

The code is not written by me, but it makes sense for me to move this code to a blog,

I have never been able to find the attribute "excel. Hwnd" in line 6th of the code, and I still cannot solve the problem after I searched Baidu and google.

At one moment, I was furious with the title of three thousand feet. At one time, I wanted to drop my computer. Fortunately, my ancestor had a spirit in the sky and shouted to stop. This gave me the chance to break the door. My computer was a company, and I had to lose money and leave. I ran the question...

The problem lies in

Microsoft. Office. Interop. Excel;

Interop. Excel;

The Excel classes of these two components are very similar but different.

For example, if the Microsoft. Office. Interop. Excel component has the hwnd attribute, Range. Text is used to obtain the Excel file.

Interop. Excel does not have the hwnd attribute. In Excel, Range. Value is used.

I thought I was using the Microsoft. Office. Interop. Excel component. Later I realized that I added Interop. Excel;

Reference the Microsoft. Office. Interop. Excel component to solve the problem.

I finally finished writing it. I used to write it in Word yesterday. When I was about to finish writing it, the office suddenly closed for me. When I opened it, I saw all the garbled characters (office2003)

Alas, it cannot be described as that. I just quietly greeted the Microsoft team who developed Word 2003.

 

Ps: This is all self-developed. If there are any errors or better solutions. Please make sure that you correct it.

QQ: 549306388 (if you cannot understand it, ask for code)

 

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.