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.

HKqQJNamespace containing Excel Kzerop mKgsJqqKDMicrosoft.Office.Interop.Excel;JClass that stores Application: C#JqqJ/K{<K7>q/K{This KJcK: the Excel Interop code we need.q/K{It can be sharedKpmany placesKjaKcduplication.q/K{</K7>qJKJExcelKuerInteropq{JqH/K{<K7>qH/K{Store the Application object we can useKpthe member functions.qH/K{</K7>qHJApplication _excelApp;JqqH/K{<K7>qH/K{Initialize a KqExcel reader. Must be KzegratedqH/K{with an Excel Kzerface object.qH/K{</K7>qHJKBExcelKuerInterop()qH{qHH_excelAppKyKqApplication();qH}q}JMKg that opens Excel workbooks: C#JqqJ/K{<K7>q/K{Open the file path receivedKpExcel. Then, open the workbookq/K{within the file. Send the workbookKjthe next function, the Kzernal scanq/K{function. Will throw an exception if a file cannot be found or opened.q/K{</K7>qJKBKcExcelOpenSpreadsheets(KP thisFileName)q{qHtryqH{JqHH//qHHK{This mess of code opens an Excel workbook. I don't know what allqHHK{those arguments do, but they can be changedKjinfluence behavior.qHH//qHHJWorkbook workBookKy_excelApp.Workbooks.Open(thisFileName,qHHHType.Missing, Type.Missing, Type.Missing, Type.Missing,qHHHType.Missing, Type.Missing, Type.Missing, Type.Missing,qHHHType.Missing, Type.Missing, Type.Missing, Type.Missing,qHHHType.Missing, Type.Missing);JqqHH//qHHK{Pass the workbookKja separate function. This KqfunctionqHHK{will iterate through the worksheetsKpthe workbook.qHH//qHHJExcelScanIntenal(workBook);JqqHH//qHHK{Clean up.qHH//qHHJworkBook.Close(false, thisFileName, null);qHHMarshal.ReleaseComObject(workBook);qH}qHcatchqH{JqHH//qHHK{Deal with exceptions.qHH//qHJ}q}JMKg that gets sheets: C#JqqJ/K{<K7>q/K{Scan the selected Excel workbookKVstore the inK|mationKpthe cellsq/K{Kwthis workbookKpan object[,] KU. Then, call another mKgq/K{to process the data.q/K{</K7>qJK2 KcExcelScanIntenal(Workbook workBookIn)q{JqH//qHK{Kksheet K]KVstore the KE of sheets.qH//qHJKinumSheetsKyworkBookIn.Sheets.K];JqqH//qHK{Iterate through the sheets. They are indexed starting at 1.qH//qHJKw(KisheetNumKy1; sheetNum < numSheetsK}1; sheetNum++)qH{qHHWorksheet sheetKy(Worksheet)workBookIn.Sheets[sheetNum];JqqHH//qHHK{Take the used range of the sheet. Finally, get an object KU of allqHHK{of the cellsKpthe sheet (their Khs). You can do things with thoseqHHK{Khs. See notes about compatibility.qHH//qHHJRange excelRangeKysheet.UsedRange;qHHobject[,] KhKSKy(object[,])excelRange.get_KX(qHHHXlRangeKXDataType.xlRangeKXDefault);JqqHH//qHHK{Do something with the dataKpthe KU with a custom mKg.qHH//qHHJProcessObjects(KhKS);qH}q}JExcel Kzerop benchmarkJqqCells[]:HHHHJ30.0 secondsJqget_Range(), Cells[]: J15.0 secondsJqUsedRange, get_KX(): J 1.5 secondsJ [fastest]JqqKDMicrosoft.Office.Interop.ExcelKbKJKAq{qHconst KP fileNameKyQC:\\Book1.xlsxQ;qHconst KP topLeftKyQA1Q;qHconst KP bottomRightKyQA4Q;qHconst KP graphTitleKyQGraph TitleQ;qHconst KP xAxisKyQTimeQ;qHconst KP yAxisKyQKXQKbHK%H{JqHHK{Open ExcelKVget first worksheet.qHHJvar applicationKyKqApplication();qHHvar workbookKyapplication.Workbooks.Open(fileName);qHHvar worksheetKyworkbook.Worksheets[1] asqHHHMicrosoft.Office.Interop.Excel.Worksheet;JqqHHK{Add chart.qHHJvar chartsKyworksheet.ChartObjects() asqHHHMicrosoft.Office.Interop.Excel.ChartObjects;qHHvar chartObjectKycharts.Kv60, 10, 300, 300) asqHHHMicrosoft.Office.Interop.Excel.ChartObject;qHHvar chartKychartObject.Chart;JqqHHK{Set chart range.qHHJvar rangeKyworksheet.get_Range(topLeft, bottomRight);qHHchart.SetSourceData(range);JqqHHK{Set chart properties.qHHJchart.ChartTypeKyMicrosoft.Office.Interop.Excel.XlChartType.xlLine;qHHchart.ChartWizard(Source: range,qHHHTitle: graphTitle,qHHHCategoryTitle: xAxis,qHHHKXTitle: yAxis);JqqHHK{Save.qHHJworkbook.Save();qH}q}J

!=creates Excel chart from data