Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods for C # to operate Excel

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the methods of C # operating Excel". In the daily operation, I believe that many people have doubts about the methods of operating Excel in C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what are the methods of operating Excel in C#?" Next, please follow the editor to study!

At the beginning of the C# operation of the basic Excel instance, let's take a look at the environment configuration of the instance implementation:

Windows XP

Microsoft Visual Studio 2008

Office 2003

There are too many examples of C # operating Excel basic instances searching for C# to achieve Excel operation on the Internet, but I don't know how many are released after verifying that they are really feasible, and it is also because of the development needs. I found some code but found that most of them could not be executed correctly, so I decided to supplement the key points I encountered in practice for reference. The following is an example:

C # manipulates the file header of the basic Excel instance:

Using System.Reflection; / / reference this to use the Missing field using Excel; Excel.ApplicationClass excel = new Excel.ApplicationClass (); excel.Visible = true; / / activate Excel Workbook wBook= excel.Workbooks.Add (true); / / Worksheet wSheet = (Excel._Worksheet) wBook.ActiveSheet; Worksheet wSheet = (Excel.Worksheet) wBook.ActiveSheet; excel.Cells [3,5] = "our company phone:" + Phone Excel.Cells [4,5] = "our fax:" + Zhen; excel.Cells [5,5] = "contact:" + ComName; excel.Cells [4,1] = "customer:" + CustomerName; excel.Cells [5,1] = "contact:" + Associate; excel.Cells [3,8] = "account name:"; excel.Cells [3,9] = AccountName; excel.Cells [4,8] = "bank of deposit:" Excel.Cells [4,9] = BranchName; excel.Cells [5,8] = "account:"; excel.Cells [5,9] = "'" + AccountID; / / set a query prompt box that forbids pop-up saving and overwriting excel.DisplayAlerts = false; excel.AlertBeforeOverwriting = false; / / Save workbook / / wBook.Save () / / Save the active table each time, / / only in this way can you save different Excel tables multiple times. / / the default save location is "my document" / / C # operation Excel basic instance excel.Cells.Font.Size = 12; excel.Cells.Font.Bold = false; / / Excel.Range m_objRange = m_objRange.get_Range (1,3) WSheet.get_Range (excel.Cells [1,3], excel.Cells [1,3]). Font.Size = 24; wSheet.get_Range (excel.Cells [1,3], excel.Cells [1,3]). Font.Bold = true; wSheet.get_Range (excel.Cells [3,1], excel.Cells [3,1]). Font.ColorIndex = 3 / / set it to red here and cannot use Font.Color to set the color / / m_objRange.Cells.Font.Size = 24; / / m_objRange.Cells.Font.Bold = true; / / C # operate Excel basic instance excel.ActiveWorkbook.SaveCopyAs (filename); excel.Quit ()

C # operation Excel basic example code comments section only briefly describes the cause of each statement, individual or worthy of consideration.

C # operation Excel basic instance statement one

Workbook wBook= excel.Workbooks.Add (true)

The parameter to Workbooks.Add is of type object, usually using true or null, indicating that the workbook is created under the default document or using enumerated values

XlWBATemplate.xlWBATWorksheet, but passing in a full excel file name is tantamount to opening an existing workbook.

C # operation Excel basic instance statement 2

Worksheet wSheet = (Excel.Worksheet) wBook.ActiveSheet

If you can manipulate multiple worksheets, instantiate it and add it to the wBook.Worksheets. If you are opening an existing workbook, this statement may also report an error. * call wBook.ActiveSheet to get or add some judgment.

C # operation Excel basic instance statement three

Excel.ActiveWorkbook.SaveCopyAs (filename)

These two lines of code are essential and essential, otherwise, the dialog box "whether to save sheet1.xls" will pop up when you save it. Determine the currently active table and save the table.

C # operation Excel basic instance statement four

Excel.Quit ()

This shutdown has always been doubtful, because when C # operates on com unmanaged objects, the excel process does not necessarily terminate with the fact that Quit () does not release the object, so someone uses KillProcess () to handle it. Personally, I don't think this is a good idea and may destroy other executing excel processes.

Null; is right to take comfort. One thing that must be done, however, is that no changes can be made before Quit (), or the saved dialog box will pop up. So make sure you have executed the Save () method of WorkBook or Application before exiting.

At this point, the study of "what are the methods for C # to operate Excel" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report