473,414 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Open Excel and put in values from C#

JM
Hi

I have been trying to open Excel 2003 but I am having rotten luck. Just
can't see to get it working. I looked at this article...

http://www.codeproject.com/csharp/cs...asp?print=true

Anyone have any ideas?
Nov 16 '05 #1
2 9823
This "Excel" namespace of yours is not contained in the .net framework and
looks like somebody's proprietary class. I have successfully been opening
excel worksheets with the OleDb ADO .net service provider (basically you
would connect to a spreadsheet like with any other data source, e.g. sql
server).

Let me try and translate this code to you (I tend to write my variable names
in serbian ;).

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Extended
Properties=""Excel 8.0;HDR=Yes;IMEX=2"";Data Source="; // The connection
string would look something like this we will add the data source later in
the code, this could be a member of the form class or your own class you
use to access your data, anyways you understand it should be visible in the
function from where you are trying to open the worksheet (e.g. a click event
handler)

// here we will get the file name of the worksheet from an OpenFileDialog.

OpenFileDialog ofd = new OpenFileDialog();
if(ofd.ShowDialog() == DialogResult.OK)
{
connectionString += ofd.FileName; // we add the file name after Data
Source (with excel worksheets the file name is the data source)
}
con = new OleDbConnection(connectionString); // we open a connection
try
{
con.Open();
}

catch(OleDbException err) // some error checking is not a bad thing.
{
MessageBox.Show(err.Message);
}
string query = "SELECT * FROM
["+Path.GetFileNameWithoutExtension(ofd.FileName)+" $A5:F1000]"; // we build
the query string, note that the table name is usually the file name plus the
excel range specifiers.

OleDbDataAdapter ad = new OleDbDataAdapter(query, con);

try
{

ad.Fill(ourTable); // we fill a table with our DataAdapter
}
catch(OleDbException err) // some error checking is not a bad thing.
{
MessageBox.Show(err.Message);
}

This example is only querying a worksheet but you get the picture, it works
the same for updating.
"JM" wrote:
Hi

I have been trying to open Excel 2003 but I am having rotten luck. Just
can't see to get it working. I looked at this article...

http://www.codeproject.com/csharp/cs...asp?print=true

Anyone have any ideas?

Nov 16 '05 #2
JM
Here is what worked for me! Thanks for your help!

http://msdn.microsoft.com/library/de...rp05152003.asp

"Aleksandar Mraović" wrote:
This "Excel" namespace of yours is not contained in the .net framework and
looks like somebody's proprietary class. I have successfully been opening
excel worksheets with the OleDb ADO .net service provider (basically you
would connect to a spreadsheet like with any other data source, e.g. sql
server).

Let me try and translate this code to you (I tend to write my variable names
in serbian ;).

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Extended
Properties=""Excel 8.0;HDR=Yes;IMEX=2"";Data Source="; // The connection
string would look something like this we will add the data source later in
the code, this could be a member of the form class or your own class you
use to access your data, anyways you understand it should be visible in the
function from where you are trying to open the worksheet (e.g. a click event
handler)

// here we will get the file name of the worksheet from an OpenFileDialog.

OpenFileDialog ofd = new OpenFileDialog();
if(ofd.ShowDialog() == DialogResult.OK)
{
connectionString += ofd.FileName; // we add the file name after Data
Source (with excel worksheets the file name is the data source)
}
con = new OleDbConnection(connectionString); // we open a connection
try
{
con.Open();
}

catch(OleDbException err) // some error checking is not a bad thing.
{
MessageBox.Show(err.Message);
}
string query = "SELECT * FROM
["+Path.GetFileNameWithoutExtension(ofd.FileName)+" $A5:F1000]"; // we build
the query string, note that the table name is usually the file name plus the
excel range specifiers.

OleDbDataAdapter ad = new OleDbDataAdapter(query, con);

try
{

ad.Fill(ourTable); // we fill a table with our DataAdapter
}
catch(OleDbException err) // some error checking is not a bad thing.
{
MessageBox.Show(err.Message);
}

This example is only querying a worksheet but you get the picture, it works
the same for updating.
"JM" wrote:
Hi

I have been trying to open Excel 2003 but I am having rotten luck. Just
can't see to get it working. I looked at this article...

http://www.codeproject.com/csharp/cs...asp?print=true

Anyone have any ideas?

Nov 16 '05 #3

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

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
1
by: Lize | last post by:
Hi, I'm writing an ASP application to open an excel workbook, then run a macro stored in the excel file, which produces outputs that will be displayed back onto my ASP application. Now the...
8
by: Juan Manuel Alegría B. | last post by:
Hi group!!! I am new in the ASP.NET programming. I am trying to open a ..txt file but I can't do it. I am using: but i don't know if its the right way. Thanks for help me. Dim proc As New...
3
by: Agnes | last post by:
Dim dsExcelExport As New System.Data.DataSet Dim daExcelExport As New System.Data.SqlClient.SqlDataAdapter Dim Excel As New Excel.Application Dim strExcelFile As String Dim strFileName As...
22
by: fniles | last post by:
I have a .CSV file (comma delimited) that I want to open using OLEDB, but I get the error "External table is not in the expected format." If I save the .CSV file to an .XLS file, I can open the...
0
by: balu435 | last post by:
Hi I used the following code to enter values to an already open browser using VBA. Dim IeBrow As InternetExplorer Dim IeDoc As Object Set IeBrow = New InternetExplorer IeBrow.Visible = True ...
3
by: toffee | last post by:
Hi all, I got a pre-formatted spreadsheet. would it be possible using js to copy the data from a table on the current webpage, open the spreadsheet and paste the content ? if so, anyone got any...
2
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
22
by: robertgregson | last post by:
Using C#, .NET3.5, Visual Studio 2008 and WCF on Windows VISTA SP1, I have written a service, service host (as a C# console application) and a client. The service uses...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.