Canalblog
Editer l'article Suivre ce blog Administration + Créer mon blog
Publicité
SpireXLS
7 décembre 2010

Using C# to Read Data from Excel File

With .Net, we can deal with many problems of Excel. During the process, kinds of data, Excel objects are used to achieve acquirements, such as transfer data to Excel workbook, interaction of Excel workbooks. After an Excel workbook is finished, we need to read data from workbooks. Developers would like to open an Excel application with C# and operate workbooks with it, for example, read data from Excel workbook. We can call this function as C# Read Excel File.

 

How to read data by using C#? Firstly, we should open the Add References dialog, which is Microsoft Excel 12.0 Object Library on the COM tab. Next, using Excel = Microsoft. Office. Interop. Excel to make working with the Excel namespace easier. If you want to read data from Excel File, you should create an Excel. Application object to control Excel, which can makes object be visible so that you can watch it work. Then, the code opens a workbook. Most of the Open method’s parameters are optional so that they are passed the values Type.Missing. Generally, the workbook which is opened by C# is read-only. This function is considered as C# Read Workbook. If you want to write, you should use the other function called C# Write Workbook which allows you to writing or modifying something in Workbook.

 

The code for reading uses the first worksheet and indexes in Excel tend to start with 1 not 0. If you key 0 in cell, it will be blank. The code calls the SetTitleListValues methods twice to get data from worksheet and then closes Excel. SetTitleListValues method is a fairy specialized method, which gets a title from a cell and gets values from the cells. Firstly, the code gets a Range representing the title cell. Then, it gets the values below the title. Through the interaction between Range and Cell to perform the most interesting work for reading data from workbook.

Publicité
Publicité
Commentaires
SpireXLS
Publicité
Publicité