p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.x and 2.0 Application Design (http://p2p.wrox.com/forumdisplay.php?f=67)
-   -   Export data from a datalist to an Excel sheet (http://p2p.wrox.com/showthread.php?t=22580)

see07 December 14th, 2004 01:32 PM

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

rsearing February 23rd, 2007 05:55 PM

Has anyone figured out how to do this?

dparsons February 23rd, 2007 06:29 PM

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


rsearing February 23rd, 2007 07:20 PM

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.


All times are GMT -4. The time now is 06:53 AM.

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