473,842 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9845
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 connectionStrin g = @"Provider=Micr osoft.Jet.OLEDB .4.0;Extended
Properties=""Ex cel 8.0;HDR=Yes;IME X=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.ShowDial og() == DialogResult.OK )
{
connectionStrin g += ofd.FileName; // we add the file name after Data
Source (with excel worksheets the file name is the data source)
}
con = new OleDbConnection (connectionStri ng); // we open a connection
try
{
con.Open();
}

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

OleDbDataAdapte r ad = new OleDbDataAdapte r(query, con);

try
{

ad.Fill(ourTabl e); // we fill a table with our DataAdapter
}
catch(OleDbExce ption 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 connectionStrin g = @"Provider=Micr osoft.Jet.OLEDB .4.0;Extended
Properties=""Ex cel 8.0;HDR=Yes;IME X=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.ShowDial og() == DialogResult.OK )
{
connectionStrin g += ofd.FileName; // we add the file name after Data
Source (with excel worksheets the file name is the data source)
}
con = new OleDbConnection (connectionStri ng); // we open a connection
try
{
con.Open();
}

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

OleDbDataAdapte r ad = new OleDbDataAdapte r(query, con);

try
{

ad.Fill(ourTabl e); // we fill a table with our DataAdapter
}
catch(OleDbExce ption 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
35573
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 and extract information from specific worksheets and cells. I'm not really sure how to get started with this process. I ran the COM Makepy utility from my PythonWin (IDE from ActiveSTate),
1
6393
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 problem I'm having is whenever I try to open a workbook (of any format, i.e. 97/2000, 95, or xp, and of any size, and with or without macros), excel just hangs forever, and if I killed the excel application in task manager, my asp application will...
8
8277
by: Juan Manuel Alegra 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 System.Diagnostics.Process proc.Start(file:///C:/bitacora.txt)
3
2091
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 String dsExcelExport.Clear() daExcelExport.SelectCommand = New SqlCommand daExcelExport.SelectCommand.Connection =
22
5020
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 connection with no problem. What is the correct way to open a .CSV file ? If I can not open the CSV file, how can I programmatically save the CSV file to an XLS file ? Thanks a lot. dim myCon OleDb.OleDbConnection
0
1860
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 IeBrow.Navigate ("www.rediffmail.com") IeDoc.loginform.login.Value = userid IeDoc.loginform.passwd.Value = passwd ..
3
16015
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 links or pointers? i've already tried google - but all i get is ActiveX methods which work in a very few cases.
2
2455
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 databases in that server. Then after the user selects a database, I have another button that he/she click and I want to retrieve file groups from a specific table. At this point when he/she clicks on that button I get an error:
22
68885
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 Microsoft.Office.Interop.Excel.Application to access an Excel file on the local hard drive, opens it (using Application.Workbooks.Open), reads out some data and the provides said data to the client. Everything works perfectly when the Service Host is a C# console...
0
9871
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10610
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10672
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9453
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7855
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7035
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5696
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4499
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 we have to send another system
3
3144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.