将ASP.NET表单数据导出到Excel文件的最佳方法?

时间:2013-06-13 12:02:12

标签: html asp.net excel

我很确定这是可能的,我只是不知道如何。

我有一个包含许多选择下拉菜单和输入框的网络表单。

如何将这些asp控件中的数据导出到Excel文件中。

我需要一些建议才能开始。

2 个答案:

答案 0 :(得分:1)

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

    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();

为您要导出的每个控件添加controlname.rendercontrol

答案 1 :(得分:0)

对于导出到xls的数据,我使用此代码。它是用Visual Basic编写的,但我认为它对你有用。复制 - 粘贴,之后只需为它提供一个DataTable,它就会起作用。

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