Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am pretty sure it is possible, I just don't know how.

I have a web form with many select dropdowns and input boxes.

How can I export the data from these asp controls into an Excel file.

I just need some advice to get started.

share|improve this question
2  
codeproject.com/Articles/5123/… –  Steve Jun 13 '13 at 12:21
    
To get started, did you try anything? Did you Google around? –  Nipun Ambastha Jun 13 '13 at 12:40
    
@Steve Thanks for that article, it looks very helpful –  Singh Jun 13 '13 at 13:18
    
@NipunAmbastha Yes, a lot of research but I have zero experience with this and cannot find suitable step by step instruction. –  Singh Jun 13 '13 at 13:18

2 Answers 2

up vote 0 down vote accepted

For data exporting to xls I use this code. It's written in Visual Basic, but I think it will work for you. Copy - Paste and after that just feed it a DataTable and it will work it's magic.

Public Shared Sub DataTableToExcelResponse(dt as DataTable, filename as String)
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename="+filename)
    HttpContext.Current.Response.Write(DataTableToExcel(dt))
    HttpContext.Current.Response.End
End Sub

Public Shared Function DataTableToExcel(dt As DataTable) As String
    Dim responseWriter As new StringWriter()
    responseWriter.Write("<html>")
    responseWriter.Write("<head><meta http-equiv=""Content-Type"" content=""text/html"" charset=""UTF-8"" /></head>")
    responseWriter.Write("<style>")
    responseWriter.Write("table.download{border-collapse:collapse;}")   
    responseWriter.Write("table.tr{vertical-align:top;}")  
    responseWriter.Write("table.th{text-align:left; font-weight:bold;}")       
    responseWriter.Write("table.td{}")
    responseWriter.Write("</style>")       
    responseWriter.Write("<body>")
    responseWriter.Write("<table class='download' border='1'>")
    responseWriter.Write("<tr>")
    For Each dc As DataColumn In dt.Columns
        responseWriter.Write("<th class='download'>")
        responseWriter.Write(dc.ColumnName)
        responseWriter.Write("</th>")
    Next
    responseWriter.Write("</tr>")

    For Each dr As DataRow In dt.Rows
        responseWriter.Write("<tr class='download'>")

        For i As Integer=0 to dt.Columns.Count-1 Step 1

            If dt.Columns(i).DataType.ToString()="System.String" Then
                responseWriter.Write("<td class='download' style='mso-number-format:\@;'>")
                responseWriter.Write(dr(i).ToString())
                ElseIf dt.Columns(i).DataType.ToString()="System.DateTime"  Then

                    responseWriter.Write("<td class='download' style='mso-number-format:""dd\/mm\/yyyy"";'>")
                    Try
                        responseWriter.Write(CType(dr(i),DateTime).ToString("dd/MMM/yyyy"))
                    Catch ex As Exception
                        responseWriter.Write(dr(i).ToString())
                    End Try
                Else
                responseWriter.Write("<td>")
                responseWriter.Write(dr(i).ToString())
            End If


            responseWriter.Write("</td>")
        Next
        responseWriter.Write("</tr>")

    Next

    responseWriter.Write("</table>")
    responseWriter.Write("</body>")
    responseWriter.Write("</html>")



    Return  responseWriter.ToString()
End Function
share|improve this answer

Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls"); Response.ContentType = "application/vnd.xlsx";

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

    System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    eachControl.RenderControl(htmlWriter);


    Response.Output.Write(stringWriter.ToString());
    Response.End();

add controlname.rendercontrol for every control you wan to export

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.