Reputation: 555
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.
Upvotes: 0
Views: 3691
Reputation: 679
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
Upvotes: 0
Reputation: 11
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
Upvotes: 1