Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

Reading Excel Files in C#

C#

  • Please log in to reply
3 replies to this topic

#1 NexusNemesis

NexusNemesis

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Programming Language:C, Java, C++, C#, (Visual) Basic, Visual Basic .NET, Assembly
  • Learning:PHP, JavaScript

Posted 10 September 2012 - 08:13 PM

Introduction

In this tutorial we will look on how to read a cell value in a worksheet in excel (xls or the newer one xlsx is supported).You can use the C# methods provided in this tutorial for reading an excel file for your own C# projects. This will be a quick simple tutorial and you will learn very quick.


Things Needed:
  • Microsoft Office 2007 or 2010
  • Microsoft Visual Studio or Visual C#
Steps:
  • Add a reference for Microsoft.Office.Interop.Excel (right click references then Add Reference)

Posted Image

Posted Image


You need to use the reference in your code to access the methods.
using Microsoft.Office.Interop.Excel; //use the reference in your code

These are the methods I have created in order to read cell values in excel, you may use it for free.
//Add this codes in your progam code
	 private static Microsoft.Office.Interop.Excel.ApplicationClass appExcel;
	 private static Workbook newWorkbook = null;
	 private static _Worksheet objsheet = null;

	 //Method to initialize opening Excel
	 static void excel_init(String path)
	 {
		 appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();

		 if (System.IO.File.Exists(path))
		 {
			 // then go and load this into excel
			 newWorkbook = appExcel.Workbooks.Open(path, true, true);
			 objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
		 }
		 else
		 {
			 MessageBox.Show("Unable to open file!");
			 System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
			 appExcel = null;
			 System.Windows.Forms.Application.Exit();
		 }
		
	 }

	 //Method to get value; cellname is A1,A2, or B1,B2 etc...in excel.
	 static string excel_getValue(string cellname)
	 {
		 string value = string.Empty;
		 try
		 {
			 value = objsheet.get_Range(cellname).get_Value().ToString();
		 }
		 catch
		 {
			 value = "";
		 }

		 return value;
	 }

	 //Method to close excel connection
	 static void excel_close()
	 {
		 if (appExcel != null)
		 {
			 try
			 {
				 newWorkbook.Close();
				 System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
				 appExcel = null;
				 objsheet = null;
			 }
			 catch (Exception ex)
			 {
				 appExcel = null;
				 MessageBox.Show("Unable to release the Object " + ex.ToString());
			 }
			 finally
			 {
				 GC.Collect();
			 }
		 }
	 }

Use it in your program:
To use it in your program, you need to initialize opening excel first by using this method:
excel_init("C:\\excel_tutorial.xlsx");
The format is excel_init(Path_of_your_excel_file); if you use slash, make sure to make it a double slash \\.

Reading a cell value method:
excel_getValue("B10");
The format is excel_getValue(Cellname); where cellname is like A1,C4,H10,M21, etc...
This method will return the value of the cell in String data type.

Safely Close the excel connection:
excel_close();

And where done!
Attached is a sample GUI program for reading a cell in excel.
Attached File  Read_Excel_Files.rar   508.11KB   10834 downloads


If you have questions/problems/suggestions please comment on this article.

Thank you very much,
Nexus Nemesis @ codecall
  • 0

#2 lbarker

lbarker

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 18 April 2013 - 12:36 AM

Hi,

 

Thanks for the above code for working with Excel and C#.

 

I've had a problem using the method that gets a cell value;

 

value = objsheet.get_Range(cellname).get_Value().ToString();

 

It would seem that get_Value() cannot have an empty parameter list - "No overload for method for get_Value takes 0 arguments".

 

I've tried using xlRangeValueDefault, as suggested by MSDN, but this is invalid.

 

Can you please help?

 

Regards,

Lee.


  • 0

#3 NexusNemesis

NexusNemesis

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Programming Language:C, Java, C++, C#, (Visual) Basic, Visual Basic .NET, Assembly
  • Learning:PHP, JavaScript

Posted 18 April 2013 - 04:47 AM

Hi,

 

Did you try the sample program? Read_Excel_Files.rar 

?


  • 0

#4 DellMercant

DellMercant

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 26 May 2014 - 10:01 PM

try this simple tutorial...http://csharp.net-in...el-tutorial.htm

 

dell.


  • 0