Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0how to export to excelhow to export to excel
Previous
 
Next
New Post
10/26/2006 12:16 PM
 
i'm trying to do a module to export some tables to excel, but i found a couple of problems

when i use this method:
---------------------------------
Response.Clear()
Response.Charset = ""
Me.EnableViewState = False
'set the response type to excel
Response.ContentType = "application/vnd.ms-excel"
'create a string writer anf HTMLTextWriter that uses it
Dim StringWrite As New System.IO.StringWriter
Dim HTMLWrite As New System.Web.UI.HtmlTextWriter(StringWrite)
'instantiate a datagrid
Dim dg As New DataGrid
'set the datagrid datasource to the dataset passed in and bind it
dg.DataSource = uTabla
dg.DataBind()
'tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(HTMLWrite)
'output the html
Response.Write(StringWrite.ToString)
Response.End()
----------------------------------
export ALL the page, not only the datatable


and when i use this method
----------------------------------
Dim xlsWB As Excel.Workbook
Dim xlsWS As Excel.Worksheet

Dim xlsApp As New Excel.ApplicationClass
'etc...
----------------------------------
i get this error, even if i have administrator and aspnet permissions:
DotNetNuke.Services.Exceptions.ModuleLoadException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005. ---> System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005. at d2bnetwork.Modules.Eventos.Inscriptos.ExportarExcel(Object sender, EventArgs e) --- End of inner exception stack trace ---


i'm using dnn version 4.3.5

any help would be appreciated

thanks,
Sebastian

 
New Post
10/26/2006 8:08 PM
 

heres the way i did it for a recent dnn 4.x module:

 ''' <summary>
    ''' get data based on combobox for the current portal
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Protected Function GetData() As IDataReader
        If ddlProfile.SelectedIndex = 0 Then Return Nothing
        Dim mySqlString As String = "SELECT UserProfile.PropertyValue, COUNT(*) AS CountResults FROM ProfilePropertyDefinition INNER JOIN UserProfile ON ProfilePropertyDefinition.PropertyDefinitionID = UserProfile.PropertyDefinitionID WHERE     (ProfilePropertyDefinition.PortalID = " & PortalSettings.PortalId.ToString & ") AND (UserProfile.PropertyDefinitionID = " & ddlProfile.SelectedItem.Value.ToString & ") GROUP BY UserProfile.PropertyValue, UserProfile.PropertyDefinitionID, ProfilePropertyDefinition.PropertyName order by CountResults desc"
        Return CType(DataProvider.Instance().ExecuteSQL(mySqlString.ToString()), IDataReader)
    End Function

    ''' <summary>
    ''' export results to excel
    ''' </summary>
    ''' <remarks></remarks>
    Protected Sub ExportToExcel()
        Dim dr As SqlDataReader = GetData()
        Dim sb As New StringBuilder

        If dr.HasRows Then
            Do While dr.Read
                sb.Append(dr.Item(0).ToString & "," & dr.Item(1).ToString)
                sb.Append(Environment.NewLine)
            Loop
        End If

        Response.ClearHeaders()
        Response.AppendHeader("Content-Disposition", "attachment; filename=data.csv")
        Response.AppendHeader("Content-Length", sb.Length.ToString())
        Response.ContentType = "text/csv"
        Response.Write(sb.ToString())
        Response.End()
    End Sub


Buy the new Professional DNN7: Open Source .NET CMS Platform book Amazon US
 
New Post
10/27/2006 4:03 PM
 
thanks for your reply

i give up trying to export a xls file, and i follow the way you did it
at first, the csv file contained part of the html code, but it fixed alone (?)

anyway, the export to csv now works

thanks
Sebastian
 
New Post
10/30/2006 1:14 PM
 
i have a new problem...

after click on the button export (the export to csv file works fine), when i click on any link, the page don't load
maybe it's something with the response, i don't know, i have no idea what's happening

here's my code:
ascx
asp:Button ID="btnInstituciones" runat="server" Text="Exportar" onclick="ExportarInstituciones"

ascx.vb
Protected Sub ExportarInstituciones(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Dim cInst As New d2bnetwork.Modules.Instituciones.InstitucionesController
Dim oInst As d2bnetwork.Modules.Instituciones.InstitucionesInfo
Dim ListaInst As List(Of d2bnetwork.Modules.Instituciones.InstitucionesInfo)

ListaInst = cInst.ObtenerInstituciones
Dim sb As New StringBuilder

sb.Append("""ID"";""Nombre"";""Descripcion""")
sb.Append(Environment.NewLine)

For Each oInst In ListaInst
sb.Append("""" & oInst.InstID.ToString & """;""" & oInst.Nombre & """;""" & oInst.Descripcion & """")
sb.Append(Environment.NewLine)
Next

Response.ClearHeaders()
Response.AppendHeader("Content-Disposition", "attachment; filename=Instituciones.csv")
Response.AppendHeader("Content-Length", sb.Length.ToString())
Response.ContentType = "text/csv"
Response.Write(sb.ToString())
Response.Flush()
Response.End()

Catch Ex As Exception
ProcessModuleLoadException(Me, Ex)
End Try
End Sub

if i don't put the response.flush() part of the html code of the page is put on the csv file
any help?

Sebastian

 
New Post
10/31/2006 7:13 AM
 
don't worry, i fix it
my way to do it

export to csv:
---------------

Dim cInst As New d2bnetwork.Modules.Instituciones.InstitucionesController
Dim oInst As d2bnetwork.Modules.Instituciones.InstitucionesInfo
Dim ListaInst As List(Of d2bnetwork.Modules.Instituciones.InstitucionesInfo)

ListaInst = cInst.ObtenerInstituciones
Dim sb As New StringBuilder

sb.Append("""ID"";""Nombre"";""Descripcion""")
sb.Append(Environment.NewLine)

For Each oInst In ListaInst
sb.Append("""" & oInst.InstID.ToString & """;""" & oInst.Nombre & """;""" & oInst.Descripcion & """")
sb.Append(Environment.NewLine)
Next

'Response.ClearContent()
'Response.Clear()
Response.ClearHeaders()
Response.AppendHeader("Content-Disposition", "attachment; filename=Instituciones.csv")
'Response.AppendHeader("Content-Length", sb.Length.ToString())
Response.ContentType = "text/csv"
Response.Write(sb.ToString())
Response.Flush()
Response.SuppressContent = True
Response.End()

---------------


export to xls:
---------------

Dim cInst As New d2bnetwork.Modules.Instituciones.InstitucionesController
Dim oInst As d2bnetwork.Modules.Instituciones.InstitucionesInfo
Dim ListaInst As List(Of d2bnetwork.Modules.Instituciones.InstitucionesInfo)

ListaInst = cInst.ObtenerInstituciones

Dim uTabla As New DataTable
uTabla.Columns.Add(New DataColumn("ID", GetType(Integer)))
uTabla.Columns.Add(New DataColumn("Nombre", GetType(String)))
uTabla.Columns.Add(New DataColumn("Descripcion", GetType(String)))

Dim dr As DataRow

For Each oInst In ListaInst
dr = uTabla.NewRow()
dr("ID") = oInst.InstID
dr("Nombre") = oInst.Nombre
dr("Descripcion") = oInst.Descripcion
uTabla.Rows.Add(dr)
Next

Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.Buffer = True
Response.Charset = ""

'set the response type to excel
Response.AppendHeader("Content-Disposition", "attachment; filename=Instituciones.xls")
Response.ContentType = "application/vnd.ms-excel"
'create a string writer anf HTMLTextWriter that uses it
Dim StringWrite As New System.IO.StringWriter
Dim HTMLWrite As New System.Web.UI.HtmlTextWriter(StringWrite)
'instantiate a datagrid
Dim dg As New DataGrid
'set the datagrid datasource to the dataset passed in and bind it
dg.DataSource = uTabla.DefaultView 'DataTable
dg.DataBind()
'tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(HTMLWrite)
'output the html
Response.Write(StringWrite.ToString)
Response.Flush()
Response.SuppressContent = True
Response.End()

 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0how to export to excelhow to export to excel


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out