473,490 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

generating Excel files w/ .NET? (an ASP.NET, C# web app)

hello,

can anyone speak to some of the common or preferred methods for
building Excel .XLS files, programmatically thru the .NET framework?

i have an intranet app that needs to generate & email .xls files for
users. these are not reports, but rather more like forms. its a pretty
simple doc, two worksheets -- the first is basic info common to all
recepients. the second is a simple data table that, depending on the
recepient, contains pre-populated rows of data. there are some blank
date fields in the table for the recepient to fill in. (and yep, after
they do so and save, it will eventually get sent back to us for parsing
of the values)

i wont really get into the why's of this project, but suffice it to say
this is the workflow we need.

i had hoped i could retrieve an ADO.NET datatable of my recepient's
rows, loop thru it, and add rows/cells to an in-memory Excel doc. then
stream it out to the user or email it.

as i understand VBA was designed to do this, but i would prefer to
avoid VBA and keep it all .NET. ive found some 3rd party components to
do such a thing, but at $7,500 a CPU, they seem pretty expensive. are
there any other alternatives?
thanks!
matt

Oct 2 '06 #1
8 2444
You can use the Excel COM wrapper, but I've found it quite easy to write
Excel-XML to a file (then you don't even need Excel installed on your
server). If you name that file with an .xls extension, when you double-click
on it, it brings up Excel and gets interpreted just fine.

The trick is to build a real Excel spreadsheet the way you want then save it
as XML to see what tricks are involved in using the XML. Then just do that
in code. I just recently did this to create a spreadsheet with 3 tabs,
auto-wrapped cells,
data filters, inter-tab hyperlinks, cell highlighting, and freeze-panes.
It's all right there in the XML, you just do what they do.

Here's a good starter article:

http://msdn.microsoft.com/library/de.../odc_xmlss.asp
"ma**@mailinator.com" wrote:
hello,

can anyone speak to some of the common or preferred methods for
building Excel .XLS files, programmatically thru the .NET framework?

i have an intranet app that needs to generate & email .xls files for
users. these are not reports, but rather more like forms. its a pretty
simple doc, two worksheets -- the first is basic info common to all
recepients. the second is a simple data table that, depending on the
recepient, contains pre-populated rows of data. there are some blank
date fields in the table for the recepient to fill in. (and yep, after
they do so and save, it will eventually get sent back to us for parsing
of the values)

i wont really get into the why's of this project, but suffice it to say
this is the workflow we need.

i had hoped i could retrieve an ADO.NET datatable of my recepient's
rows, loop thru it, and add rows/cells to an in-memory Excel doc. then
stream it out to the user or email it.

as i understand VBA was designed to do this, but i would prefer to
avoid VBA and keep it all .NET. ive found some 3rd party components to
do such a thing, but at $7,500 a CPU, they seem pretty expensive. are
there any other alternatives?
thanks!
matt

Oct 2 '06 #2
q
I just copy pasted from a project I did last year... Given a
dataTable, it creates the XLS and all that and returns the file name
(there was a segment of the code the generated a file name, but I
deleted that). This works great for everything we need... Basically,
you just create a table and start putting stuff in the table. That
said, there are some seriously anal details it wants.

public static string CreateExcelReport(DataTable dataTable) {
string tempFolder =
System.Configuration.ConfigurationManager.AppSetti ngs["TempFolder"];
if (tempFolder.Length < 1) {
throw new IapException("TempFolder was not specified in
configuration file.");
}
string excelString = @"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=" + tempFolder;
string statements = "";

string excelFileName = "MyFileName.xls";
excelString += excelFileName;
excelString += "; Extended Properties=Excel 8.0";

string current = DateTime.Now.ToString("D").Replace(" ",
"").Replace(",", "");

List<stringcolumns = new List<string>( );
foreach (DataColumn dc in dataTable.Columns) {
columns.Add(dc.ColumnName);
}

bool first = true;
using (OleDbConnection connection = new
OleDbConnection(excelString)) {
connection.Open( );

OleDbCommand cmd = new OleDbCommand( );
cmd.Connection = connection;

string createStatement = "create table Report" +
current + " (";
foreach (string column in columns) {
if (!first) {
createStatement += ", ";
}
string column2 = column.Replace("-", "");
createStatement += "[" + column2 + "]
VarChar(200)";
first = false;
}

createStatement += ");";

statements += createStatement + "\n";
cmd.CommandText = createStatement;
cmd.ExecuteNonQuery( );
}

using (OleDbConnection connection = new
OleDbConnection(excelString)) {
DataTable dt = new DataTable("ExcelTable");
OleDbDataAdapter da = new OleDbDataAdapter( );

string insertStatement = "insert into [Report" + current + "] (";

first = true;
foreach (string column in columns) {
if (!first) {
insertStatement += ", ";
}
string column2 = column.Replace("-", "");
insertStatement += "[" + column2 + "]";
first = false;
}

insertStatement += ") values (";

first = true;
foreach (string column in columns) {
if (!first) {
insertStatement += ", ";
}
string column2 = column.Replace("-", "");
insertStatement += "@" + column2.Replace(" ", "");
first = false;
}

insertStatement += ");";

statements += insertStatement + "\n";

da.InsertCommand = new OleDbCommand(insertStatement,
connection);

foreach (string column in columns) {
string column2 = column.Replace("-", "");
da.InsertCommand.Parameters.Add("@" +
column.Replace(" ", ""), OleDbType.VarChar).SourceColumn = column2;
}

foreach (string column in columns) {
string column2 = column.Replace("-", "");
dt.Columns.Add(column2,
Type.GetType("System.String"));
}

int n = 0;
foreach (DataRow row in dataTable.Rows) {
DataRow excelRow = dt.NewRow( );
foreach (string column in columns) {
excelRow[column.Replace("-", "")] =
row[column];
}

dt.Rows.Add(excelRow);
}

da.Update(dt);

return excelFileName;
}
}

ma**@mailinator.com wrote:
hello,

can anyone speak to some of the common or preferred methods for
building Excel .XLS files, programmatically thru the .NET framework?

i have an intranet app that needs to generate & email .xls files for
users. these are not reports, but rather more like forms. its a pretty
simple doc, two worksheets -- the first is basic info common to all
recepients. the second is a simple data table that, depending on the
recepient, contains pre-populated rows of data. there are some blank
date fields in the table for the recepient to fill in. (and yep, after
they do so and save, it will eventually get sent back to us for parsing
of the values)

i wont really get into the why's of this project, but suffice it to say
this is the workflow we need.

i had hoped i could retrieve an ADO.NET datatable of my recepient's
rows, loop thru it, and add rows/cells to an in-memory Excel doc. then
stream it out to the user or email it.

as i understand VBA was designed to do this, but i would prefer to
avoid VBA and keep it all .NET. ive found some 3rd party components to
do such a thing, but at $7,500 a CPU, they seem pretty expensive. are
there any other alternatives?
thanks!
matt
Oct 3 '06 #3
There are a variety of ways to export to excel, and most of them are
outlined in this article:
http://SteveOrr.net/Articles/ExcelExport.aspx

Here's another option:
http://SteveOrr.net/Articles/ExportPanel.aspx

And here are a few good 3rd party options that make it easy to do complex
things.
http://SteveOrr.net/reviews/AsposeExcel.aspx
http://officewriter.softartisans.com...writer-37.aspx
http://www.syncfusion.com/products/xlsio/

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
<ma**@mailinator.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
hello,

can anyone speak to some of the common or preferred methods for
building Excel .XLS files, programmatically thru the .NET framework?

i have an intranet app that needs to generate & email .xls files for
users. these are not reports, but rather more like forms. its a pretty
simple doc, two worksheets -- the first is basic info common to all
recepients. the second is a simple data table that, depending on the
recepient, contains pre-populated rows of data. there are some blank
date fields in the table for the recepient to fill in. (and yep, after
they do so and save, it will eventually get sent back to us for parsing
of the values)

i wont really get into the why's of this project, but suffice it to say
this is the workflow we need.

i had hoped i could retrieve an ADO.NET datatable of my recepient's
rows, loop thru it, and add rows/cells to an in-memory Excel doc. then
stream it out to the user or email it.

as i understand VBA was designed to do this, but i would prefer to
avoid VBA and keep it all .NET. ive found some 3rd party components to
do such a thing, but at $7,500 a CPU, they seem pretty expensive. are
there any other alternatives?
thanks!
matt

Oct 3 '06 #4
"$7,500 a CPU, they seem pretty expensive"

Indeed expensive.

xlsgen : http://xlsgen.arstdesign.com
--

xlsgen - native Excel generator http://xlsgen.arstdesign.com
xlsgen RSS feed : http://www.arstdesign.com/BBS/rssxlsgen.php
<ma**@mailinator.coma écrit dans le message de
news:11**********************@i42g2000cwa.googlegr oups.com...
hello,

can anyone speak to some of the common or preferred methods for
building Excel .XLS files, programmatically thru the .NET framework?

i have an intranet app that needs to generate & email .xls files for
users. these are not reports, but rather more like forms. its a pretty
simple doc, two worksheets -- the first is basic info common to all
recepients. the second is a simple data table that, depending on the
recepient, contains pre-populated rows of data. there are some blank
date fields in the table for the recepient to fill in. (and yep, after
they do so and save, it will eventually get sent back to us for parsing
of the values)

i wont really get into the why's of this project, but suffice it to say
this is the workflow we need.

i had hoped i could retrieve an ADO.NET datatable of my recepient's
rows, loop thru it, and add rows/cells to an in-memory Excel doc. then
stream it out to the user or email it.

as i understand VBA was designed to do this, but i would prefer to
avoid VBA and keep it all .NET. ive found some 3rd party components to
do such a thing, but at $7,500 a CPU, they seem pretty expensive. are
there any other alternatives?
thanks!
matt

Oct 3 '06 #5
VB/VBA is COM based. .Net is not.
You would need a COM wrapper for any .Net components and call the wrapper
from Excel/VBA.

But why not use the standard ADO library from VBA and avoid the .Net
completely.
Also the email side is striaght forward :
http://www.rondebruin.nl/sendmail.htm

If you want to "keep it all .NET", you can't use VBA.

NickHK

<ma**@mailinator.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
hello,

can anyone speak to some of the common or preferred methods for
building Excel .XLS files, programmatically thru the .NET framework?

i have an intranet app that needs to generate & email .xls files for
users. these are not reports, but rather more like forms. its a pretty
simple doc, two worksheets -- the first is basic info common to all
recepients. the second is a simple data table that, depending on the
recepient, contains pre-populated rows of data. there are some blank
date fields in the table for the recepient to fill in. (and yep, after
they do so and save, it will eventually get sent back to us for parsing
of the values)

i wont really get into the why's of this project, but suffice it to say
this is the workflow we need.

i had hoped i could retrieve an ADO.NET datatable of my recepient's
rows, loop thru it, and add rows/cells to an in-memory Excel doc. then
stream it out to the user or email it.

as i understand VBA was designed to do this, but i would prefer to
avoid VBA and keep it all .NET. ive found some 3rd party components to
do such a thing, but at $7,500 a CPU, they seem pretty expensive. are
there any other alternatives?
thanks!
matt

Oct 3 '06 #6
The MicrosoftExcelClient is a simple class any developer can use in their
codes to interface to Excel Documents.
It has been created in C#.NET and uses OleDb.

http://www.codeproject.com/useritems...xcelclient.asp

I use it with success

Regards
Nicolas Guinet

<ma**@mailinator.coma écrit dans le message de news:
11**********************@i42g2000cwa.googlegroups. com...
hello,

can anyone speak to some of the common or preferred methods for
building Excel .XLS files, programmatically thru the .NET framework?

i have an intranet app that needs to generate & email .xls files for
users. these are not reports, but rather more like forms. its a pretty
simple doc, two worksheets -- the first is basic info common to all
recepients. the second is a simple data table that, depending on the
recepient, contains pre-populated rows of data. there are some blank
date fields in the table for the recepient to fill in. (and yep, after
they do so and save, it will eventually get sent back to us for parsing
of the values)

i wont really get into the why's of this project, but suffice it to say
this is the workflow we need.

i had hoped i could retrieve an ADO.NET datatable of my recepient's
rows, loop thru it, and add rows/cells to an in-memory Excel doc. then
stream it out to the user or email it.

as i understand VBA was designed to do this, but i would prefer to
avoid VBA and keep it all .NET. ive found some 3rd party components to
do such a thing, but at $7,500 a CPU, they seem pretty expensive. are
there any other alternatives?
thanks!
matt

Oct 5 '06 #7
thanks all for the replies.

i am not interested in merely exporting data to excel, thats easy. but
rather in designing mid-complexity documents, some of which is based on
dynamic data.

i will investigate your suggested links.
thanks!
matt

Oct 5 '06 #8

Tim Johnson wrote:
The trick is to build a real Excel spreadsheet the way you want then save it
as XML to see what tricks are involved in using the XML. Then just do that
in code.
thanks tim!

that sounds really sweet. however, i didnt see "XML Spreadsheet" as a
"Save As..." option. then i realized we're on Excel 2000, and it
appears the xml format was introduced in Excell 2002. doh.

looks like i cant do that then, huh?
matt

Oct 5 '06 #9

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

Similar topics

0
2461
by: Oci-One Kanubi | last post by:
Everything works fine in Access, but when I double-click on the resultant Excel files the first one opens correctly, but subsequent ones, and any other Excel files I try to open, fail to display at...
0
1419
by: Raghavendra | last post by:
hi, we r using forms authetication. problem :- i am using the below code to generate excel report but since we r using forms authetication.. after generating excel report the browser directs...
3
1968
by: Boris Condarco | last post by:
Hi gurus, I'm using excel 2000 to show data that comes from datagrid. The problem is that for any reason the asp.net application maintains the excel open, even though, i do close it. Besides,...
3
1881
by: daniele.balducci | last post by:
Hi All, I'm generating XLS files from ASP(.Net) code using the usual code chunks ... Response.ContentType = "application/vnd.ms-excel" Response.AppendHeader("Content-Disposition", "attachment;...
1
1428
by: Barbara Alderton | last post by:
I’m relatively new to .NET. I would like to write a VB.NET app (I read that using C# isn't as elegant when dealing with Office products) that reads in Excel files (forms) of both 2000 and 2003...
5
5594
by: mwazir | last post by:
Dear all, I have a .NET app that processes some excel file and it was working in all scenarios. Recently however we received excel files from a new client which my application has been unable...
18
4375
by: gonzlobo | last post by:
No, I don't want to destroy them (funny how the word 'decimate' has changed definition over the years) :). We have a data acquisition program that saves its output to Excel's ..xls format....
2
1214
by: c83 | last post by:
some time ago i wrote an asp.net application which generated excel files using Microsoft.Office.Interop.Excel. It is working but it takes a lot of time to generate the excel file. Yeasterday i...
1
5116
by: helplakshmi | last post by:
Hi All, I wish you a Happy new year!!!.. My requirement is to upload an excel file using PHP and to update the data in MSSQL. For which i have designed a screen so that the user can browse 2...
0
6967
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...
0
7142
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
7181
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...
1
6847
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
5445
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,...
1
4875
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
4565
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...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.