Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > Other ASP.NET > ASP.NET 1.x and 2.0 Application Design
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 14th, 2004, 01:32 PM
Authorized User
Join Date: Dec 2004
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default Export data from a datalist to an Excel sheet

I’m using this code to export data from a datagrid to an Excel sheet:


Response.Buffer= true;

Response.ContentType = "application/vnd.ms-excel";

Response.AddHeader("Content-Disposition", "inline;filename=Clientes.xls");

Response.Charset = "";

this.EnableViewState = false;

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);




And this is working fine.

But now I want to do same with a datalist and thus I changed RenderControl line thus:


Yet when Excel sheet is showing on screen, all data are displayed in one simple column “A”, with one row for each data in datalist.

Does somebody know how can I attain it work as I did as datagrd (each column in datalist into its respective column in Excel)?

I’ll appreciate your suggestions.



El Hombre que tiene Amigos ha de mostrarse Amigo
El Hombre que tiene Amigos ha de mostrarse Amigo
Old February 23rd, 2007, 05:55 PM
Friend of Wrox
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing

Has anyone figured out how to do this?

Old February 23rd, 2007, 06:29 PM
Wrox Author
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

A datalist is like a repeater (more or less) where you can format you data any which way you choose which may not necessarily be in a table format. (Rows/columns)

A datalist uses the ItemTemplate, however, not the TemplateColumn tag the datagrid control on the other hand, does. By design a datagrid exports to excel fine because it has its rows and columns defined.

What you may want to do is when you go to export your data, take your datalist Datasource and bind it to an in memory datagrid, then export that datagrid to excel. I think it would work out better for you.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET

Old February 23rd, 2007, 07:20 PM
Friend of Wrox
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing

This would work great if I wanted the actual data that was returned from the data source, but I actually tweak the values being returned and then post to the datalist.

For example, I am pulling back from my query a "totalSumMinutes" value...in my DataList, however, my binding is personalized to go through a personalized function that has two methods:

<div style="float:left; width:40px"><asp:Label ID="Label6" runat="server" Text='<%# EventStruct.CalculateHours(Eval("total")) %>'></asp:Label></div>

<div style="float:left; width:40px"><asp:Label ID="Label1" runat="server" Text='<%# EventStruct.CalculateMinutes(Eval("total")) %>' ></asp:Label> </div>

..where the methods are below:

Public Shared Function CalculateHours(ByVal total As Object) As Integer
        Dim returnInt As Integer = 0
        If total IsNot DBNull.Value Then
            returnInt = (total / 60) - 0.5
        End If
        Return returnInt
    End Function

    Public Shared Function CalculateMinutes(ByVal total As Object) As Integer
        Dim returnInt As Integer = 0
        If total IsNot DBNull.Value Then
            returnInt = total Mod 60
        End If
        Return returnInt
    End Function

So, you can see, that simply setting the Datagrid's datasource will not give me the output I need.

I actually need to figure out how to render the datalist somehow.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export Values to Excel Sheet from Sub form ayazhoda Access VBA 3 June 12th, 2012 03:18 PM
Gridview data export to excel sheet abhishekkashyap27 C# 2005 1 August 1st, 2008 10:10 AM
Export DataList to Excel rsearing ASP.NET 2.0 Professional 0 February 23rd, 2007 03:31 PM
How To Export a specific Excel Sheet as a csv file mrjits Excel VBA 5 August 1st, 2006 03:04 PM
Export Image to Excel sheet haribala.raj General .NET 0 September 13th, 2005 11:56 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.