473,466 Members | 4,855 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Connecting to Excel with C#


Can anyone send me a code snippet on how to connect to an Excel
spreadsheet and then read it in a row at a time?
John S

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
2 7908
These might help:

http://www.codeproject.com/csharp/Excel_using_OLEDB.asp
http://www.c-sharpcorner.com/winforms/ExcelReadMG.asp

"John Sutor" <jo********@cinfin.com> wrote in message
news:e$**************@TK2MSFTNGP11.phx.gbl...

Can anyone send me a code snippet on how to connect to an Excel
spreadsheet and then read it in a row at a time?
John S

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #2
These two methods should get you started. They will fill a dataset with the
excel data.

Hope that helps.
-Darrin

private DataSet GetExcelData(System.IO.FileInfo importFile)
{
OleDbConnection _xlConn = new OleDbConnection();

try
{
DataSet _xlDataSet = new DataSet();
_xlConn = GetOleConnectionForExcel(importFile.FullName);
OleDbDataAdapter _xlAdapt = new OleDbDataAdapter();

_xlConn.Open();

DataTable _schemaTable =
_xlConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables , null);

foreach(DataRow _dr in _schemaTable.Rows)
{
_xlAdapt.SelectCommand = new OleDbCommand("Select * from [" +
_dr["TABLE_NAME"].ToString() + "]", _xlConn);
_xlAdapt.Fill(_xlDataSet, _dr["TABLE_NAME"].ToString());
}

return _xlDataSet;
}
catch(SqlException _sqlException)
{
throw new Exception("Error getting Excel data.", _sqlException);
}
catch(Exception _exception)
{
throw new Exception("Error getting Excel data.", _exception);
}
finally
{
if(_xlConn.State != ConnectionState.Closed)
_xlConn.Close();
}
}

private OleDbConnection GetOleConnectionForExcel(string filePath)
{
string _cnString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + filePath +
";" + "Extended Properties=Excel 8.0;IMEX=1";

return new OleDbConnection(_cnString);
}
"John Sutor" <jo********@cinfin.com> wrote in message
news:e$**************@TK2MSFTNGP11.phx.gbl...

Can anyone send me a code snippet on how to connect to an Excel
spreadsheet and then read it in a row at a time?
John S

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jersey | last post by:
I am creating an instance of Excel in a WinForm application. When I try to run this application, there is a long wait followed by a message from my firewall that my application is attempting to...
0
by: eaocomp | last post by:
I'm trying to load data from Excel to SQL server using VB 6.0. I think I have most of the code but I ned to know how to register Microsoft Excel Object Library in VB as well as the file name and...
1
by: Dishaa V | last post by:
Hello I need to connect to an access db which is on the internet. I do know that URL of the same. its http://www.vallury.com/balance/bank.mdb. Its just a test db. How can I connect to that db...
2
by: John Sutor | last post by:
Can anyone send me a code snippet on how to connect to an Excel spreadsheet and then read it in a row at a time? John S *** Sent via Developersdex http://www.developersdex.com *** Don't just...
2
by: Rashrashetta | last post by:
Hai all, I am a computre science student.I am actually required to build a system with the following description: an intructor wants to use an excel sheet to put his/her students marks...
3
by: Adam Sandler | last post by:
Hello all, I'm trying to use VWD 2005 Express Edition to connect to a database. For my initial prototyping though, I'm just using an Excel spreadsheet as the source and everything resides on the...
3
by: saurabh | last post by:
Does anybody has any idea how to connect to Microsoft Excel through a C# .net or ASP.net?? Pls send me the process if u know... also how can we run commands like select * from <blah blah????If...
3
by: olafbrungot | last post by:
I get the following error message when trying to connect to a host database (OBCD connection) with "MS Query". SQL0471N Invocation of routine "SYSIBM .SQLTABLES " failed due to reason...
2
by: Bart | last post by:
Hi, i'm trying to connect to an excel database ('mytable'). With this code, i get the error: "Keyword not supported: 'provider'". When i remove 'Provider =' from the connection string, i get...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.