473,386 Members | 1,736 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,386 software developers and data experts.

Writing to an Excel spreadsheet

I am trying to write data to an excel spreadsheet from a C# app and
cannot make it work. Does anyone have any good examples? Please do
not include any XML examples.

Nov 16 '05 #1
4 7216
Microsoft has a Sample called 'AutoExcel'...

One way to find it is to launch C# Express
and query help for 'Excel Sample'.

If that fails, perhaps i can post it to a NG where
large posts are tolerable.
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"John S" <jo********@cinfin.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I am trying to write data to an excel spreadsheet from a C# app and
cannot make it work. Does anyone have any good examples? Please do
not include any XML examples.

Nov 16 '05 #2
Here is the URL for the AutoExcel sample on MSDN

http://whidbey.msdn.microsoft.com/li...85bae063c4.asp

Click "Download Sample" at the top to download the code.

-Josh Koppang

"Peter N Roth" wrote:
Microsoft has a Sample called 'AutoExcel'...

One way to find it is to launch C# Express
and query help for 'Excel Sample'.

If that fails, perhaps i can post it to a NG where
large posts are tolerable.
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"John S" <jo********@cinfin.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I am trying to write data to an excel spreadsheet from a C# app and
cannot make it work. Does anyone have any good examples? Please do
not include any XML examples.


Nov 16 '05 #3
John,

You can do this a couple of different ways. If you just want to populate an
existing workbook/worksheet you can use the OleDb class to accomplish this.
Just create a datasource to the workbook and open a connection. Now you will
be able to access the sheets in the workbook just like tables. I have
provided an example below to do this.

You can also use the excel object to create the workbook from scratch. I
have pasted a link to the MSDN that details how to do this with Office XP or
Office 2003. If you are using Office XP you can download the PIA to use but
if you are using Excel 2000 you will need to use the tlbimp to generate the
PIA yourself.

I hope this helps
-------------------------------------

//link
http://support.microsoft.com/kb/302084/EN-US/

//code
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=D:\temp\queryExcelTemp.xls;" +
"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "Insert into [Sheet1$] (FirstName, LastName)" +
" values ('Fred', 'Flintstone')";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE [Sheet1$] SET FirstName = 'Homer', LastName =
'Simpson' " + " WHERE FirstName = 'Joe'";
cmd.ExecuteNonQuery();
conn.Close();

Nov 16 '05 #4
Interesting! MS's example accesses Excel
directly thru interop.

Thanks for teaching me something new.
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"Brian Brown" <Br********@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
John,

You can do this a couple of different ways. If you just want to populate
an
existing workbook/worksheet you can use the OleDb class to accomplish
this.

[ ]
Nov 16 '05 #5

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

Similar topics

2
by: Nevo | last post by:
Can I write to files, say for example, to an excel spreadsheet and have it written in a certain order or in certain cells? Thanks, VAL
6
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at...
11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
2
by: bob | last post by:
My program copys things from a word document and puts them into an excel spreadsheet. The problem I have though is these annoying symbols that appear in place of spaces in the text after Ive...
3
by: dhurwitz | last post by:
I am trying to create an XML spreadsheet for use with Excel. I first saved a simple spreadsheet as an XML file, and now I am trying to recreate that file in code, using .NET 2.0. The beginning of...
1
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access...
0
by: Grip | last post by:
Hi, I have gone throught the group and Microsoft's online help and have seen many suggestions but I am still seeking clarity: 1. I have an excel spreadsheet. Column A contains text that may...
1
by: vijaymohan | last post by:
Hi ..I am very new to perl..can some one help me with this script pls.. I am Querying database and writing data to excel.. Here is my script: #!/usr/bin/perl -w use strict; use DBI;
1
by: =?Utf-8?B?ZmhpbGxpcG8=?= | last post by:
We have a code snippet that downloads data to Excel. it is writing row by row. This causes a performance issue. Any ideas on how to speed this up will be appreciated. Please find below an...
12
by: vijayarl | last post by:
Hi All, To Moderator : Sorry i have posted this querry to member rather putting in the forum. To All: Problem stmt: I am reading the data from the excel files & writing its content to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.