这是我的ASPX代码:
<%@ Page Title="" EnableEventValidation="false" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Button ID="Button3" runat="server" Text="Export GridView1"
onclick="Button3_Click" />
<asp:Button ID="Button4" runat="server" Text="Export GridView1"
onclick="Button4_Click" />
<br /><br />
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="View1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Export GridView1"
onclick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="f_casenum" HeaderText="Case Number"
SortExpression="f_casenum" />
<asp:BoundField DataField="f_invoice" HeaderText="Invoice Number"
SortExpression="f_invoice" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</asp:View>
<asp:View ID="View2" runat="server">
<asp:Button ID="Button2" runat="server" Text="Export GridView2"
onclick="Button2_Click" />
<asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="f_casenum" HeaderText="Case Number"
SortExpression="f_casenum" />
<asp:BoundField DataField="f_invoice" HeaderText="Invoice Number"
SortExpression="f_invoice" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</asp:View>
</asp:MultiView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:paconn %>"
SelectCommand="SELECT * FROM [c_emi]">
</asp:SqlDataSource>
</asp:Content>
和我的c#代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using System.Web.SessionState;
using System.Drawing;
using System.IO;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (GridView1.Rows.Count > 0)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=ExportFile.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw1 = new StringWriter())
{
HtmlTextWriter hw1 = new HtmlTextWriter(sw1);
GridView1.AllowPaging = false;
this.GridView1.DataBind();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw1);
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw1.ToString());
Response.Flush();
Response.End();
}
}
else
{
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (GridView2.Rows.Count > 0)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=ExportFile.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw1 = new StringWriter())
{
HtmlTextWriter hw1 = new HtmlTextWriter(sw1);
GridView2.AllowPaging = false;
this.GridView2.DataBind();
GridView2.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView2.HeaderRow.Cells)
{
cell.BackColor = GridView2.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView2.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView2.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView2.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView2.RenderControl(hw1);
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw1.ToString());
Response.Flush();
Response.End();
}
}
else
{
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//Verifies that the control is rendered
}
protected void Button3_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 0;
}
protected void Button4_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 1;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["paconn"].ToString());
con.Open();
SqlCommand com = new SqlCommand("SELECT * FROM c_emi", con);
DataTable dt = new DataTable();
SqlDataAdapter sqlDa = new SqlDataAdapter(com);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView2.DataSource = dt;
GridView2.DataBind();
}
else
{
GridView2.DataBind();
}
con.Close();
}
}
问题是,当我点击导出GridView1按钮时,它会完美地导出GridView1的所有数据,但是当我点击导出GridView2按钮时,它会将数据导出到excel,而我的excel文件只包含这个&#34; .textmode {}&#34;。 我认为当我使用c#绑定gridview2时会发生错误。
我正在使用asp.net c#visual studio 2010。
答案 0 :(得分:0)
我猜你没有将你的GridView2
与任何DataSource
绑定为GridView1
。您的GridView2
标记必须包含DataSource
,且必须Bind
到GridView2
希望这能帮到你!