GIDForums  

Go Back   GIDForums > Computer Programming Forums > .NET Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Feb-2008, 05:53
arestispartan's Avatar
arestispartan arestispartan is offline
New Member
 
Join Date: Feb 2008
Location: Durban, South Africa
Posts: 12
arestispartan will become famous soon enough

Reading a Microsoft Excel file in C#


Hi

I am trying to open an Excel file for reading. However, I'm not sure what referecence to use in my c# project, and what the using statement should be. I have added the "office" reference to my project, but I can't use the Excel object to create new types e.g. the statement "Excel.Application app = new Excel.Appliocation()" is not working.

Any help or ideas?
Thanks
Aresti
  #2  
Old 27-Feb-2008, 23:20
arestispartan's Avatar
arestispartan arestispartan is offline
New Member
 
Join Date: Feb 2008
Location: Durban, South Africa
Posts: 12
arestispartan will become famous soon enough

Re: Reading an Excel file in C#


Ok, I think I've made some progress in this. There's a microsoft Excel 9.0 Object library which I can add as a reference in my project, and the compiler seems to pick up the Excel object. I will come back and maybe post mty code if I get this working.

Aresti
  #3  
Old 28-Feb-2008, 06:51
arestispartan's Avatar
arestispartan arestispartan is offline
New Member
 
Join Date: Feb 2008
Location: Durban, South Africa
Posts: 12
arestispartan will become famous soon enough
Wink

Re: Reading an Excel file in C#


Hi

This is for C# - Reading and Writing to Excel

Ok, I think I have worked out or borrowed a way of connecting to an Excel file, here with path: "C:\Test Projects\Excel example\Excel - reading an excel file\ReadThis.xls", like you connect to a database. You need the "System.Data" reference and use the statement "using using System.Data.OleDb;" at the top of your code.

I have tried to comment the code as you go along so hopefully this makes sense. I got this to work yesterday. The file ReadThis.xls has a column called A with two values that get printed to a MessageBox. It also has a column called B that has all its 1's changed into 2's.

This looks long, but it also has two parts! A read part and an update part.

C-SHARP / C# Code:
//Create the Connection String
//You must specify the Provider as is here, I think
//Thre Data Source is the path to your file
//Extended Properties is something to do with excel, use as here

string ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;
					Data Source=C:\Test Projects\Excel example\Excel - reading an excel file\ReadThis.xls;
					Extended Properties=Excel 5.0";

//Create the connection

System.Data.OleDb.OleDbConnection ExcelConnection = 
new System.Data.OleDb.OleDbConnection
				 (ConnectionString);
			
//create a string for the query

string ExcelQuery;

//Sheet1 is the sheet name
//create the query:
//read column with heading A from the Excel file

ExcelQuery = "Select A from [Sheet1$]"; // from Sheet1";

//use "Select * ... " to select the entire sheet
//create the command

System.Data.OleDb.OleDbCommand ExcelCommand = new System.Data.OleDb.OleDbCommand(ExcelQuery,ExcelConnection);
			
//Open the connection

ExcelConnection.Open();

//Create a reader

System.Data.OleDb.OleDbDataReader ExcelReader;
ExcelReader = ExcelCommand.ExecuteReader();
			
//For each row after the first
//Message box the values in the first column i.e. column 0

while (ExcelReader.Read())
{
  MessageBox.Show((ExcelReader.GetValue(0)).ToString());
}
ExcelConnection.Close();

//Try update the file

ExcelQuery = "Update [Sheet1$] set B = 2 where B = 1"; // from Sheet1";

//Create the command to be executed

ExcelCommand = new System.Data.OleDb.OleDbCommand
(ExcelQuery,ExcelConnection);

//Open the connection to the file

ExcelConnection.Open();

//Execute the update

ExcelCommand.ExecuteNonQuery();

//Close the connection

ExcelConnection.Close();

If you have any questions please ask.

Aresti
Last edited by admin : 28-Feb-2008 at 07:08. Reason: Please insert your example C# codes between [CSHARP] and [/CSHARP] tags
  #4  
Old 21-May-2008, 07:09
ken_hirota ken_hirota is offline
New Member
 
Join Date: May 2008
Posts: 1
ken_hirota is on a distinguished road

Re: Reading an Excel file in C#


Hi!

Do you know any other way to read a xls file? Like a method that read cells?

Tks!
  #5  
Old 02-Jun-2008, 04:56
arestispartan's Avatar
arestispartan arestispartan is offline
New Member
 
Join Date: Feb 2008
Location: Durban, South Africa
Posts: 12
arestispartan will become famous soon enough

Re: Reading an Excel file in C#


Hi Ken

Unfortunately I don't know of another way of reading in the spreadsheets at the moment. It took me a while to work out the above solution. In my example you have to loop through the rows of the spreadsheet, and you have to have it setup like a table (e.g. column A headed by "A" and column B headed by "B", or headed by whatever your column headings are). I think that in VB there is a way of accessing the cells individually like you are asking for.

Cheers
Arestispartan
  #6  
Old 20-Jun-2008, 15:30
Arul Arul is offline
New Member
 
Join Date: Jun 2008
Posts: 1
Arul is on a distinguished road

Re: Reading a Microsoft Excel file in C#


HI,
Thank you very much for the code help

I followed your code, however I am getting a strange scenario error. My Excel sheet has titles in Numeric instead of Alphabetic. When I use the numeric value in the select query, its not working, However if the column title is Alphabetic, it is working

This is WORKING

ExcelQuery = "Select A from [MySheet$]"; // from Sheet1";

BUT This is not working (Excelsheet column titles are in numeric value)
ExcelQuery = "Select 1 from [MySheet$]"; // from Sheet1";

Please help me on this
  #7  
Old 20-Jun-2008, 15:37
JustinFox JustinFox is offline
Junior Member
 
Join Date: Mar 2008
Posts: 59
JustinFox will become famous soon enough

Re: Reading a Microsoft Excel file in C#


name the column 'one' instead of '1'. Maybe numerics aren't a valid format for column headers for 'tables'.

Justin Fox
  #8  
Old 20-Jun-2008, 15:40
JustinFox JustinFox is offline
Junior Member
 
Join Date: Mar 2008
Posts: 59
JustinFox will become famous soon enough

Re: Reading a Microsoft Excel file in C#


I implemented a writing to an excel file a long time ago, it was done by using the _sheets object and _WorkBook object.

I ran into problems with saving the darn thing tho. But writing to the excel file was pretty easy.

Just google writing to excel file using _Sheets and _Workbook etc...

hope you can find something

Justin Fox
  #9  
Old 23-Jun-2008, 07:18
arestispartan's Avatar
arestispartan arestispartan is offline
New Member
 
Join Date: Feb 2008
Location: Durban, South Africa
Posts: 12
arestispartan will become famous soon enough

Re: Reading a Microsoft Excel file in C#


Hi Arul

I will look into it when I get some time... Have you tried putting quotes around the number 1 like

ExcelQuery = "Select '1' from [MySheet$]";
or ExcelQuery = "Select "1" from [MySheet$]";

?

It may be that you can't use numeric constants as column headings. Can you not add in an extra (top) row into your sheet as headings?

Arestispartan
  #10  
Old 23-Jun-2008, 07:21
arestispartan's Avatar
arestispartan arestispartan is offline
New Member
 
Join Date: Feb 2008
Location: Durban, South Africa
Posts: 12
arestispartan will become famous soon enough

Re: Reading a Microsoft Excel file in C#


Hi Justin

What reference do you need for your project to use the _sheets and _workbook object?

Thanks
Arestispartan
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading MS Excel file through C++ code swati_npl C++ Forum 5 03-Mar-2008 05:12
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Reading and Writing to a text file raptorhawk C++ Forum 16 14-Apr-2005 13:09
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.