使动态创建的Excel报告可下载

时间:2010-04-06 15:11:29

标签: c# asp.net vb.net

我有两个代码块,如果有人可以帮我把它们放在一起,我会得到我正在寻找的功能。第一个代码块使用我正在寻找的下载对话框将gridview下载到excel:

Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
    ' Verifies that the control is rendered 
End Sub

Private Sub ExportToExcel(ByVal filename As String, ByVal gv As GridView, ByVal numOfCol As Integer)
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", filename))
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)

    gv.AllowPaging = False
    gv.DataBind()

    'Change the Header Row back to white color 
    gv.HeaderRow.Style.Add("background-color", "#FFFFFF")

    For i As Integer = 0 To numOfCol - 1
        gv.HeaderRow.Cells(i).Style.Add("background-color", "blue")
        gv.HeaderRow.Cells(i).Style.Add("color", "#FFFFFF")

    Next

    For i As Integer = 0 To gv.Rows.Count - 1
        Dim row As GridViewRow = gv.Rows(i)

        'Change Color back to white 
        row.BackColor = System.Drawing.Color.White

        For j As Integer = 0 To numOfCol - 1
            row.Cells(j).Style.Add("text-align", "center")
        Next


        'Apply text style to each Row 
        row.Attributes.Add("class", "textmode")

        'Apply style to Individual Cells of Alternating Row 
        If i Mod 2 <> 0 Then

            For j As Integer = 0 To numOfCol - 1
                row.Cells(j).Style.Add("background-color", "#CCFFFF")
                row.Cells(j).Style.Add("text-align", "center")
                '#C2D69B
                'row.Cells(j).Style.Add("font-size", "12pt")
            Next

        End If
    Next
    gv.RenderControl(hw)

    'style to format numbers to string 
    Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"
    Response.Write(style)
    Response.Output.Write(sw.ToString())
    Response.Flush()
    Response.End()
End Sub

第二段代码是我希望下载的示例报告。因此,我希望此函数不要下载gridview,而是接受工作表对象。

评论弗兰克的建议...... 弗兰克感谢您的帮助,这几乎对我有用。如果我的根文件夹中没有名为test.xls的虚拟文件,则问题是代码崩溃了。当我把它放在那里它然后加载2个工作簿test.xls [1]和第2册test.xls是一个空白工作簿,第2册是正确的dymanically创建报告。我不想将此文件保存在eroot文件夹中,如果我没有,我希望用户只需打开将其下载到他们的客户端。创建woorkbook后使用的代码是...... Dim fn As String =“RptCrd_”&amp; “捆绑”&amp;是 “.xls”         Dim eio As String =“〜/ ContentDisposition /”&amp; FN

    Dim exData As Byte() = File.ReadAllBytes(Server.MapPath(eio))
    Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fn))
    Response.ContentType = "application/x-msexcel"
    Response.BinaryWrite(exData)
    Response.Flush()
    Response.End()
    releaseObject(xlApp)
    releaseObject(xlWorkBook)

1 个答案:

答案 0 :(得分:1)

也许您需要在响应中添加正确的内容类型和标题,以便浏览器知道您要使用Excel处理下载。你可能会把它放在Response.Write(样式)之后;调用

尝试谷歌搜索MS Excel mime类型,以及如何将正确格式化的标题和内容类型添加到您的响应。

我只是将一些代码放在一起以向浏览器发送下载,以便它可以将其作为Excel电子表格处理。这应该为您提供如何格式化响应标头的示例。

这只是读入现有的电子表格,然后将其提供给响应流。这只是演示了如何制定响应标题。

  byte[] excelData = File.ReadAllBytes(Server.MapPath("test.xls"));

  Response.AddHeader("Content-Disposition", "attachment; filename=test.xls");
  Response.ContentType = "application/x-msexcel";
  Response.BinaryWrite(excelData);