网格视图excel不能正常工作 [英] grid view to excel not working properly
本文介绍了网格视图excel不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我在更新面板内的内容页面中有一个gridview
i已经写入导出到excel代码但是当我下载excel文件时它不会显示gird数据显示
Hi i have an gridview in Content Page inside update Panel
i have written export to excel code but when i excel file is downloaded it wont show gird data it only show
<div> </div>
这是我的代码
here is my code
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" CssClass="PanMain">
<asp:Button ID="btnExport" runat="server" Text="Export" CssClass="CmdBtn" />
<asp:GridView ID="GVDetails" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
GridLines="Vertical" Width="100%" CssClass="GridView">
<AlternatingRowStyle BackColor="#CCCCCC" />
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="" ItemStyle-Width="60px">
<ItemTemplate>
<asp:LinkButton ID="LnkDetails" runat="server" Text="EDIT ->" CommandArgument='<%#Eval("nVP_Id") %>' />
</ItemTemplate>
<ItemStyle Width="60px" />
</asp:TemplateField>
<asp:BoundField HeaderText="VPO No" DataField="sVPO_Id"
ItemStyle-Width="120px" >
<ItemStyle Width="120px" />
</asp:BoundField>
<%-- <asp:BoundField HeaderText="Region" DataField="sRegion" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="VPO Type" DataField="sVPO_Type" ItemStyle-Width="120px" />--%>
<asp:BoundField HeaderText="Customer" DataField="sName" />
<asp:BoundField HeaderText="Ref No" DataField="sRef_no"
ItemStyle-Width="120px" >
<ItemStyle Width="120px" />
</asp:BoundField>
<asp:BoundField HeaderText="VPO Date" DataField="dtVPO_Dt"
ItemStyle-Width="150px" >
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField HeaderText="VPO amount" DataField="dTotal"
ItemStyle-Width="150px" >
<ItemStyle Width="150px" />
</asp:BoundField>
</Columns>
</asp:GridView>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnExport" />
</Triggers>
</asp:UpdatePanel>
,这是我的导出到Excel代码
and here is my Export to Excel Code
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
' removing Template field which is inside grid view in column 0 template is a link button
GVDetails.Columns.RemoveAt(0)
GVDetails.DataBind()
Dim ReportName As String = ""
'ReportName = ddlReportFor.SelectedItem.Text
ReportName = "VPO_Report_" & Format(Now, "yyyy_MM_dd_HH_mm_ss")
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" & ReportName & ".xls")
'HttpContext.Current.Response.ContentEncoding = Encoding.UTF8
Dim tw As New StringWriter()
Dim hw As New HtmlTextWriter(tw)
'tbl1.RenderControl(hw)
'TPOOutPut.RenderControl(hw)
Dim frm As HtmlForm
frm = New HtmlForm
frm.Attributes("runat") = "server"
frm.Controls.Add(GVDetails)
'GVDetails.Parent.Controls.Add(frm)
'frm.RenderControl(hw)
GVDetails.RenderControl(hw)
HttpContext.Current.Response.Write(tw.ToString())
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.[End]()
End Sub
哪里错误的正确对我
where its wrong kindly correct me
推荐答案
使用它的作品对我来说
Use this its works for me
public void ExportToExcel(System.Web.UI.Control ctl)
{
using (Control myCtl = ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition", attachment);
HttpContext.Current.Response.Charset = charSet;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = content;
myCtl.Page.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//Renders the control here.
myCtl.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
和拨打此
and for calling this
dt = GetData();
myGridView.DataSource = dt;
myGridView.DataBind();
ExportToExcel(myGridView);
这里GetData()方法用于获取gridview数据
here the GetData() method is used to get the gridview data
Actually when i was removing template field in code behind and again i was binding but grid was losing its data source
So i removed the template Field and than re-binned that grid view whose data source was in View state
my code is as follows in
Actually when i was removing template field in code behind and again i was binding but grid was losing its data source
So i removed the template Field and than re-binned that grid view whose data source was in View state
my code is as follows in
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
' removing Template field which is inside grid view in column 0 template is a link button
viewstate("GVsearch") = GVDetails.datasource
GVDetails.Columns.RemoveAt(0)
dtCurrentTable = ViewState("GVSearch")
GVDetails.DataSource = dtCurrentTable 'GVdeta.DataSource
GVDetails.DataBind()
Dim ReportName As String = ""
ReportName = ddlReportFor.SelectedItem.Text
ReportName = "VPO_Report_" & Format(Now, "yyyy_MM_dd_HH_mm_ss")
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" & ReportName & ".xls")
'HttpContext.Current.Response.ContentEncoding = Encoding.UTF8
Dim tw As New StringWriter()
Dim hw As New HtmlTextWriter(tw)
'tbl1.RenderControl(hw)
'TPOOutPut.RenderControl(hw)
Dim frm As HtmlForm
frm = New HtmlForm
frm.Attributes("runat") = "server"
frm.Controls.Add(GVDetails)
'GVDetails.Parent.Controls.Add(frm)
'frm.RenderControl(hw)
GVDetails.RenderControl(hw)
HttpContext.Current.Response.Write(tw.ToString())
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.[End]()
End Sub</pre>
And Thanks for your support every one
And Thanks for your support every one
这篇关于网格视图excel不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文