zgD{7{J:{pjc{7Ju{7{p{7{7{qz{z{7Buyqg{7{p{jz{{7BcP{{jy{jq{p{{g{7{V|p{wpUg{{72c{k]VEiy]{wiy}y{U{ph{hyhSyXXX{pUghSzXDbJAPyPyPyPyPyPyXb%{Vyqyy{yyvy{y{yX{aAaBBEABrAArBABDWB-PW-|F666Z-P66666XCBWP66666WWP84ZC-LWC 66666WWC-

Excel` creates XLS and XLSX files. These files are hard to read in C# programs. They are handled with the Microsoft.Office.Interop.Excel assembly. This assembly sometimes creates performance issues. Step-by-step instructions are helpful. `Add the Microsoft.Office.Interop.Excel assembly by going to Project -> Add Reference.`Visual Studio `visual-studio`Interop.` You must include a namespace to use Excel in your C# program. You have to use Visual Studio's GUI to add an assembly to your program to use Excel interop. Use the Add Reference command for this. `First,` we make a new C# class file in Visual Studio and you can call it something like ExcelInterop. We use that name in the examples, but you are free to use something more familiar or standard. `Add the using line to your class file—this will make the Excel namespace handy.`Excel interop methods.` If you look at various Excel interop objects in IntelliSense, you will see hundreds of undocumented functions. You will only need to use a few—unless you have complex requirements. `Interop class.` We need a place to store our Excel information, so we use our special class. Put an Application object called _excelApp as a member. In the constructor, make the _excelApp a new Application object. `Class `class`Open.` To open Excel files, you must call Workbooks.Open. It is harder than it should be. Put a public function on the class, and it can use the _excelApp we already have. The following code uses the Workbooks.Open method. `I apologize` for the mess that is Workbooks.Open. This is probably the result of legacy code at Microsoft. There are many Type.Missing arguments. And don't panic over those Type.Missing arguments. You don't need to know what they do.`Next,` open a workbook with Workbooks.Open. Send the workbook we open to another function called ExcelScanInternal. Close the workbook and release all the memory. You will need to deal with exceptions in the catch block. `I don't have the information about all the detailed exceptions that can be thrown. Just catch them in one statement.`Catch `catch`Loop.` To get data from workbooks, you will need to loop over the sheets. An Excel workbook has one or more sheets. In Excel, you can switch between sheets by clicking on the tabs on the bottom. `In Excel Interop, sheets are indexed starting at 1. This is similar to Visual Basic but not the C# language.`In this example,` the object array is directly usable in the C# language. Once you get the objects, you don't need to do any more Interop. This provides a huge performance boost. There are benefits to reducing calls to Excel Interop. `Next: `I show some benchmarks I took comparing the various techniques of Excel interop.`Performance.` If you do your Excel processing wrong, you could be waiting a long time to process even 10 spreadsheets at once. Using the Cells[] indexer on a range, or getting the range with the Range property or get_Range() is slow. `This document is based on .NET 3.5. The approach shown here is the fastest one.`Compatibility.` There is a compatibility issue with the Excel program on the Apple Mac OS X platform. Excel stores OA dates in different formats on Macs and PCs. The code above shows one solution, which is to use get_Value() instead of Value2. `FromOADate `fromoadate`The function get_Value() returns objects that do not vary based on the original platform.`Charts.` It is also possible to create charts directly in your Excel workbooks. The ChartWizard method is useful for this. Also, you can use new features from the .NET Framework 4.0 to simplify how you use Excel.`570px`450px`This example` uses several hard-coded constant values. The range of the data we want to chart is encoded in the topLeft and bottomRight constants. You can see we use the ChartObjects property and then Add a Chart. `Next: `We set the range of the chart with SetSourceData. We use the XlChartType.xlLine enumerated constant and call ChartWizard().`Include the Microsoft.Office.Interop.Excel namespace by right-clicking on References and selecting Add Reference.`In this example,` we use the named parameters and default parameters functionality in the .NET Framework 4.0. In older versions of the .NET Framework, you will need to specify the Missing value yourself. `Some code contributed by Randall Kelsey was adapted for the charting example.`The program requires` an Excel document named Book1.xlsx located at C:\Book1.xlsx. In this file, you need to add four values in the first column. The program, upon execution, will create a chart based on those four values. `Many options can be changed to create different charts based on different data ranges.`We showed how named parameters and default parameters from the .NET Framework 4.0 can clean up Microsoft.Office.Interop.Excel code.`Summary.` We saw how to get started with Excel interop and avoid severe performance problems with the assembly. It is confusing to call the complicated Open method with Type.Missing in more places than necessary.

789Namespace containing Excel 8erop m8s9 8Microsoft.Office.Interop.Excel;9Class that stores Application: C#9 9/8<8> /8This 8c8 the Excel Interop code we need. /8It can be shared8many places8a8duplication. /8</8> 98Excel8erInterop {9 7/8<8> 7/8Store the Application object we can use8the member functions. 7/8</8> 79Application _excelApp;9 7/8<8> 7/8Initialize a 8Excel reader. Must be 8egrated 7/8with an Excel 8erface object. 7/8</8> 798Excel8erInterop() 7{ 77_excelApp88Application(); 7} }9M8 that opens Excel workbooks: C#9 9/8<8> /8Open the file path received8Excel. Then, open the workbook /8within the file. Send the workbook8the next function, the 8ernal scan /8function. Will throw an exception if a file cannot be found or opened. /8</8> 988ExcelOpenSpreadsheets(8 thisFileName) { 7try 7{9 77// 778This mess of code opens an Excel workbook. I don't know what all 778those arguments do, but they can be changed8influence behavior. 77// 779Workbook workBook8_excelApp.Workbooks.Open(thisFileName, 777Type.Missing, Type.Missing, Type.Missing, Type.Missing, 777Type.Missing, Type.Missing, Type.Missing, Type.Missing, 777Type.Missing, Type.Missing, Type.Missing, Type.Missing, 777Type.Missing, Type.Missing);9 77// 778Pass the workbook8a separate function. This 8function 778will iterate through the worksheets8the workbook. 77// 779ExcelScanIntenal(workBook);9 77// 778Clean up. 77// 779workBook.Close(false, thisFileName, null); 77Marshal.ReleaseComObject(workBook); 7} 7catch 7{9 77// 778Deal with exceptions. 77// 79} }9M8 that gets sheets: C#9 9/8<8> /8Scan the selected Excel workbook8store the in8mation8the cells /88this workbook8an object[,] 8. Then, call another m8 /8to process the data. /8</8> 98 8ExcelScanIntenal(Workbook workBookIn) {9 7// 788sheet 88store the 8 of sheets. 7// 798numSheets8workBookIn.Sheets.8;9 7// 78Iterate through the sheets. They are indexed starting at 1. 7// 798(8sheetNum81; sheetNum < numSheets81; sheetNum++) 7{ 77Worksheet sheet8(Worksheet)workBookIn.Sheets[sheetNum];9 77// 778Take the used range of the sheet. Finally, get an object 8 of all 778of the cells8the sheet (their 8s). You can do things with those 7788s. See notes about compatibility. 77// 779Range excelRange8sheet.UsedRange; 77object[,] 888(object[,])excelRange.get_8( 777XlRange8DataType.xlRange8Default);9 77// 778Do something with the data8the 8 with a custom m8. 77// 779ProcessObjects(88); 7} }9Excel 8erop benchmark9 Cells[]:7777930.0 seconds9 get_Range(), Cells[]: 915.0 seconds9 UsedRange, get_8(): 9 1.5 seconds9 [fastest]9 8Microsoft.Office.Interop.Excel888 { 7const 8 fileName8"C:\\Book1.xlsx"; 7const 8 topLeft8"A1"; 7const 8 bottomRight8"A4"; 7const 8 graphTitle8"Graph Title"; 7const 8 xAxis8"Time"; 7const 8 yAxis8"8"8787{9 778Open Excel8get first worksheet. 779var application88Application(); 77var workbook8application.Workbooks.Open(fileName); 77var worksheet8workbook.Worksheets[1] as 777Microsoft.Office.Interop.Excel.Worksheet;9 778Add chart. 779var charts8worksheet.ChartObjects() as 777Microsoft.Office.Interop.Excel.ChartObjects; 77var chartObject8charts.860, 10, 300, 300) as 777Microsoft.Office.Interop.Excel.ChartObject; 77var chart8chartObject.Chart;9 778Set chart range. 779var range8worksheet.get_Range(topLeft, bottomRight); 77chart.SetSourceData(range);9 778Set chart properties. 779chart.ChartType8Microsoft.Office.Interop.Excel.XlChartType.xlLine; 77chart.ChartWizard(Source: range, 777Title: graphTitle, 777CategoryTitle: xAxis, 7778Title: yAxis);9 778Save. 779workbook.Save(); 7} }9

!=creates Excel chart from data