Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of Excel implementation of C # Operation

Shulou Source: shulou.com Published: 2022-06-02 11:44:20 09月30日 Update

C # operation Excel implementation of the example analysis, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

How does the C # operation Excel perform? What methods will we mainly use in the actual C # operation of Excel development programs? What are the specific implementation steps? Let's introduce it to you in detail through an example:

C # operates Excel instance:

Using System;//C# operation Excel using System.Reflection; namespace CExcel1 {class Class1 {[STAThread] static void Main (string [] args) {/ / create Application object Excel.Application xApp=new Excel.ApplicationClass (); xApp.Visible=true / / you can get the WorkBook object in one of two ways: the following is to open the existing file Excel.Workbook xBook=xApp.Workbooks._Open (@ "D:\ Sample.xls", Missing.Value,Missing.Value,Missing.Value,Missing.Value, Missing.Value,Missing.Value,Missing.Value,Missing.Value, Missing.Value,Missing.Value,Missing.Value,Missing.Value); / / xBook=xApp.Workbooks.Add (Missing.Value) / / Code for creating a new file / / specify the Sheet to be operated in two ways: Excel.Worksheet xSheet= (Excel.Worksheet) xBook.Sheets [1]; / / Excel.Worksheet xSheet= (Excel.Worksheet) xApp.ActiveSheet; / / read data through the Range object Excel.Range rng1=xSheet.get_Range ("A1", Type.Missing); Console.WriteLine (rng1.Value2) / / read, through the Range object, but using different interfaces to get Range Excel.Range rng2= (Excel.Range) xSheet.Cells [3jue 1]; Console.WriteLine (rng2.Value2); / / C# to operate Excel write data Excel.Range rng3=xSheet.get_Range ("C6", Missing.Value); rng3.Value2= "Hello"; rng3.Interior.ColorIndex=6 / / set the background color of Range / / C # Operation Excel save method 1: save WorkBook / / xBook.SaveAs (@ "D:\ CData.xls", Missing.Value,Missing.Value,Missing.Value,Missing.Value, Missing.Value / /, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value,Missing.Value,Missing.Value / /, Missing.Value,Missing.Value) / / C # operation Excel save method 2: save WorkSheet / / xSheet.SaveAs (@ "D:\ CData2.xls", Missing.Value,Missing.Value,Missing.Value, Missing.Value / /, Missing.Value,Missing.Value,Missing.Value, Missing.Value,Missing.Value); / / C # operation Excel save method 3 xBook.Save (); xSheet=null; xBook=null; xApp.Quit () / / this sentence is very important, otherwise the Excel object cannot exit xApp=null; from memory} read the above content, have you mastered the method of C# operating the instance analysis of Excel implementation? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Examples objects ways instances methods instance analysis analysis content data files more problems differences importance helplessness for this code memory reasons reality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Microsoft Redmi NVidia Xiaomi