gridview转换为excel格式 [英] gridview convert to excel format
本文介绍了gridview转换为excel格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建一个gridview,然后成功添加连接字符串.
我创建一个按钮button_text是convert_excel_format.,
我需要点击按钮,然后将Excel格式的数据转换为此gridview数据
请帮帮我.....
I am create one gridview then add connection string sucessfully.
i create one button button_text is convert_excel_format.,
i need i click the button then convert excel format for this gridview data
ple help me.....
推荐答案
以下是单击btn_convert_excel_format
时将gridview1数据转换为excel格式的代码.
Here is the code to convert gridview1 data to excel format on clicking btn_convert_excel_format
Protected Sub btn_convert_excel_format_Click(ByVal sender As Object, ByVal e As EventArgs)
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.AllowPaging = False
GridView1.DataBind()
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")
GridView1.HeaderRow.Cells(0).Style.Add("background-color", "green")
GridView1.HeaderRow.Cells(1).Style.Add("background-color", "green")
GridView1.HeaderRow.Cells(2).Style.Add("background-color", "green")
GridView1.HeaderRow.Cells(3).Style.Add("background-color", "green")
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row As GridViewRow = GridView1.Rows(i)
row.BackColor = System.Drawing.Color.White
row.Attributes.Add("class", "textmode")
If i Mod 2 <> 0 Then
row.Cells(0).Style.Add("background-color", "#C2D69B")
row.Cells(1).Style.Add("background-color", "#C2D69B")
row.Cells(2).Style.Add("background-color", "#C2D69B")
row.Cells(3).Style.Add("background-color", "#C2D69B")
End If
Next
GridView1.RenderControl(hw)
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转换为excel格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文