Excel中的出口问题 [英] exporting problem in excel

查看:69
本文介绍了Excel中的出口问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



参考您有关将gridview导出到Word/Excel/PDF/CSV的文章,我修改了代码以动态填充GV,因为根据选择它们具有不同的数据和不同的列.但是在数据填充之后,当我尝试导出数据时,它不会导出任何数据,例如.在Word中,它只是导出.
任何帮助将不胜感激.

该代码粘贴在下面.

****以下是填充GV的代码********

Hi,

Refering to your article on exporting the gridview to Word/Excel/PDF/CSV, I modified to code to populate the GV dynamically coz they have different data and different columns as per the selection. But after the data population, when I try to export the data it does not export any data instead for eg. in Word it just export.
Any help would be appreciated.

The code is pasted below.

**** The below is the code for populating GV ********

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As DataTable = GetData()
        Dim dcol As DataColumn
        Dim bfields As BoundField
 
        GridView1.Columns.Clear()
        For Each dcol In dt.Columns
            bfields = New BoundField
            bfields.DataField = dcol.ColumnName
            bfields.HeaderText = dcol.ColumnName
            GridView1.Columns.Add(bfields)
        Next
 
        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub


****以下代码摘自您的示例,用于导出GV数据******


**** Below Code is from your samples for exporting GV data ******

Protected Sub btnExportWord_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-word "
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        GridView1.AllowPaging = False
        GridView1.DataBind()
        GridView1.RenderControl(hw)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.End()
    End Sub

推荐答案

公共共享子ExportDataSetToExcel(ds作为数据集,文件名作为字符串)
昏暗的响应As HttpResponse = HttpContext.Current.Response

''清理响应对象
response.Clear()
response.Charset ="

''设置响应头
response.ContentType =应用程序/vnd.ms-excel"
response.AddHeader("Content-Disposition","attachment; filename =""&文件名&"")

''创建StringWriter并用于创建CSV
将sw用作New StringWriter()
使用htw作为新的HtmlTextWriter(sw)
''实例化DataGrid
昏暗的dg作为新的DataGrid()
dg.DataSource = dt
dg.DataBind()
dg.RenderControl(htw)
response.Write(sw.ToString())
回应.[结束]()
最终使用
最终使用
结束Sub
Public Shared Sub ExportDataSetToExcel(ds As DataSet, filename As String)
Dim response As HttpResponse = HttpContext.Current.Response

''Clean response object
response.Clear()
response.Charset = ""

''Set response header
response.ContentType = "application/vnd.ms-excel"
response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & """")

''Create StringWriter and use to create CSV
Using sw As New StringWriter()
Using htw As New HtmlTextWriter(sw)
''Instantiate DataGrid
Dim dg As New DataGrid()
dg.DataSource = dt
dg.DataBind()
dg.RenderControl(htw)
response.Write(sw.ToString())
response.[End]()
End Using
End Using
End Sub


这篇关于Excel中的出口问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆