我有2個代碼塊,如果有人可以幫我把它們放在一起,我會得到我正在尋找的功能。第一個代碼塊下載一個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是正確的動態創建的報告。我不想將這個文件保存在eroot文件夾中,如果我沒有,我希望用戶只需打開下載到他們的客戶端。在創建之後woorkbook使用代碼IM是... 昏暗FN作爲字符串= 「RptCrd_」 & 「BUNDLE」 & 「的.xls」 昏暗EIO作爲字符串= 「〜/ ContentDisposition /」 & 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)
我需要的函數接受工作簿對象而不是工作表而不是gridview – 2010-04-06 16:21:23
也許我不明白你的問題。我以爲你想把數據發送到Excel。這是讓我失望的響應頭路徑。 – 2010-04-06 16:37:34
我只是使用保存的電子表格作爲測試來展示如何制定瀏覽器將解釋爲MS Excel電子表格的響應標頭。 – 2010-04-06 18:14:36