100% found this document useful (1 vote)
313 views4 pages

Read Excel Files in C# with COM Interop

The document discusses how to read an Excel file using C# by opening an Excel workbook, getting the active worksheet, and reading the used range of cells. It demonstrates getting a specific cell or range of cells, and looping through the used range to read each cell's value and display it in a message box.

Uploaded by

Yusfan x'Crash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
313 views4 pages

Read Excel Files in C# with COM Interop

The document discusses how to read an Excel file using C# by opening an Excel workbook, getting the active worksheet, and reading the used range of cells. It demonstrates getting a specific cell or range of cells, and looping through the used range to read each cell's value and display it in a message box.

Uploaded by

Yusfan x'Crash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

How to read an Excel file using C#

about:reader?url=[Link]

[Link]

The following program illustrates how to


open an existing Excel spreadsheet in C#
using .NET Framework COM interop capability. Also you can see
how to find Named Ranges in Excel and get the range of occupied
cells (Used area) in excel sheet.
The following C# source code using Microsoft
Excel 12.0 Object Library for reading an Excel
file. In the previous section we saw how to import Microsoft Excel
12.0 Object Library in the CSharp project .
Create Excel file from CSharp
How to specify a range in Excel sheet?
If you want to select a specific cell in Excel sheet, you can code like
this.
[Link] excelSheet = [Link]; [Link]
rng = ([Link])[Link][10, 10];
Reading Named Ranges in Excel
Worksheet.get_Range Method
If you want to select multiple cell value from Excel sheet, you can
code like this.
[Link] excelSheet = [Link]; [Link]
rng = ([Link]) excelSheet.get_Range([Link][1, 1],

1 of 4

27/06/2015 4:56

How to read an Excel file using C#

about:reader?url=[Link]

[Link][3,3]);
How to get the range of occupied cells in excel sheet
For reading entire content of an Excel file in C#, we
have to know how many cells used in the Excel file.
In

order

to

find

the

used

range

we

use

"UsedRange" property of xlWorkSheet . A used


range includes any cell that has ever been used. It will return the last
cell of used area.
[Link] range ; range = [Link];
using System;
using [Link];
using Excel = [Link];
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender,
EventArgs e)
{
[Link] xlApp ;
[Link] xlWorkBook ;
[Link] xlWorkSheet ;
[Link] range ;
string str;
int rCnt = 0;

2 of 4

27/06/2015 4:56

How to read an Excel file using C#

about:reader?url=[Link]

int cCnt = 0;
xlApp = new [Link]();
xlWorkBook =
[Link]("[Link]", 0, true, 5, "", "", true,
[Link],
"\t", false, false, 0, true, 1, 0);
xlWorkSheet =
([Link])[Link].get_Item(1);
range = [Link];
for (rCnt = 1; rCnt <=
[Link]; rCnt++)
{
for (cCnt = 1; cCnt <=
[Link]; cCnt++)
{
str = (string)
([Link][rCnt, cCnt] as [Link]).Value2 ;
[Link](str);
}
}
[Link](true, null, null);
[Link]();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try

3 of 4

27/06/2015 4:56

How to read an Excel file using C#

about:reader?url=[Link]

{
[Link](ob
obj = null;
}
catch (Exception ex)
{
obj = null;
[Link]("Unable to
release the Object " + [Link]());
}
finally
{
[Link]();
}
}
}
}
When you execute this C# source code the program read all used
cells from Excel file.

4 of 4

27/06/2015 4:56

You might also like