0

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

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

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

我只需要一些建议就可以开始了。

4

2 回答 2

1

Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls"); Response.ContentType = "应用程序/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();

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

于 2013-06-13T12:10:40.163 回答
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
于 2013-06-14T17:17:16.360 回答