This project has moved. For the latest updates, please go here.

Export Table HTML to Excel

Feb 14, 2012 at 4:52 PM
Edited Feb 14, 2012 at 4:53 PM

Hello,

I'm working with web pages ASPX with SQL Server using VB language, I'm doing an inventory of equipment in a table as shown:

 
DETALLE
 
Fecha de Revisión:
 
Nombre del Servidor:
 
IP:
MAC Address:
IP 2:
 
IP 3:    
Servicio:

 

Ambiente:
   
Tipo de Servidor:
MEMORIA
Marca:
Capacidad Total (RAM):
Modelo:
Tipo :
Tipo-Modelo:
Ancho de Banda:
Serie:
Velocidad (MHz):
Límite de garantía:
Capacidad por Slot (MB):
Negocio:
Capacidad Total a soportar por Slot (MB):
Local:
Slots Usados / Slot Total:
 





My page works fine, but I want to put a button to export this table to an Excel file. Some people recommended me ClosedXML, I checked the documentation and it's very interesting but I'm a bit lost about code that corresponds to the button's Click event to export. 

I appreciate your response.

Coordinator
Feb 14, 2012 at 4:54 PM

Did you check the FAQ?

Feb 14, 2012 at 6:02 PM

Yes, I tested the code changing to VB language but shows me an error

        'Create the workbook

        Dim workbook As New XLWorkbook()
        workbook.Worksheets.Add("Sample").Cell(1, 1).SetValue("Hello World")

        'Prepare the response
        Dim httpResponse = Response
        httpResponse.Clear()
        httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        httpResponse.AddHeader("content-disposition", "attachment;filename=\'HelloWorld.xlsx\'")

        'Flush the workbook to the Response.OutputStream
        Using (MemoryStream memoryStream = New MemoryStream())
            workbook.SaveAs(MemoryStream)
            MemoryStream.WriteTo(httpResponse.OutputStream)
            MemoryStream.Close()
        End Using
        httpResponse.End()
 

Feb 14, 2012 at 6:22 PM
Edited Feb 14, 2012 at 6:24 PM

Sorry, I made a mistake, this is the correct code in VB language:

 

        'Create the workbook
        Dim workbook As New XLWorkbook()
        workbook.Worksheets.Add("Sample").Cell(1, 1).SetValue("Hello World")

        'Prepare the response
        Dim httpResponse = Response
        httpResponse.Clear()
        httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        httpResponse.AddHeader("content-disposition", "attachment;filename=HelloWorld.xlsx")

        'Flush the workbook to the Response.OutputStream
        Using memoryStream As New MemoryStream()
            workbook.SaveAs(memoryStream)
            memoryStream.WriteTo(httpResponse.OutputStream)
            memoryStream.Close()
        End Using
        httpResponse.End()

 

and works! But my question is how I can put information to export it in that format table?

Coordinator
Feb 14, 2012 at 6:25 PM

That's not valid VB code so it won't compile. Use the following instead:

        Using MyMemoryStream As New MemoryStream()
            workbook.SaveAs(MyMemoryStream)
            MyMemoryStream.WriteTo(httpResponse.OutputStream)
            MyMemoryStream.Close()
        End Using

Coordinator
Feb 14, 2012 at 6:27 PM
Edited Feb 14, 2012 at 6:43 PM

Have you looked at the "Inserting Tables" section or "Using Tables" under Ranges in the documentation?

Edit: If you don't want an Excel table you can use your own style. See "Styles" in the documentation.

Feb 16, 2012 at 1:17 PM

Thank you very much :)