请问如何把GridView选中的某些数据导出到excel?
下面是Try.aspx页面显示的数据
Try.aspx.cs是后台代码,这个代码只能实现整个页面数据的导出(并且导出后是乱码),怎么才能导出选中的数据呢?
请各位指点下下,谢谢
Try.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EveryMend.aspx.cs" Inherits="EveryMend" %><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<link href="Style.css" rel="Stylesheet" type="text/css" />
<style>
body
{
scrollbar-base-color:White;
}
.style2
{
width: 85px;
}
.style3
{
width: 261px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
Width="100%" Font-Size="12px" AutoGenerateColumns="False" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" /></ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="Month_Date" HeaderText="月份" NullDisplayText="\" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Apply_User" HeaderText="申请人" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Apply_Date" HeaderText="申请时间" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="PartMent" HeaderText="申请部门" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Equip_ID" HeaderText="设备编码" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Equip_Name" HeaderText="设备名称" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Equip_Brand" HeaderText="设备品牌" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Start_Date" HeaderText="开始维修时间"
NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="End_Date" HeaderText="维修结束时间" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Stop_Date" HeaderText="影响停机时间" SortExpression="\" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Date" HeaderText="维修保养时间" NullDisplayText="\" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Sort1" HeaderText="维修分类1">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Sort2" HeaderText="维修分类2">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Place" HeaderText="维修地点">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Fault" HeaderText="人为/生产">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Result" HeaderText="维修结果">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Mend_User" HeaderText="维修负责人">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Complete_User" HeaderText="完工确认人">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Content" HeaderText="维修内容">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
</Columns>
<PagerStyle BackColor="#FFFFFF" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
<table border="0" cellpadding="0" cellspacing="0" class="css" width="100%">
<tr>
<td align="center" class="style2">
<asp:CheckBox ID="chkAll" runat="server" Text="全选" AutoPostBack="true"
oncheckedchanged="chkAll_CheckedChanged" />
</td>
<td class="style3">
<asp:Button ID="btnExcel" runat="server" Text="导出到Excel"
onclick="btnExcel_Click" />
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Try.apsx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;
using System.Text;public partial class EveryMend : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
Handy();
}
}
public void Handy()
{
DateTime Doday = DateTime.Today.Date;
SqlConnection strcon=new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
strcon.Open();
string sql = "select * from Mend where EveryDate='" + Doday + "' order by ID desc";
SqlDataAdapter sda = new SqlDataAdapter(sql, strcon);
DataSet ds=new DataSet();
sda.Fill(ds);
DataTable dt = ds.Tables[0];
this.GridView1.DataSource = dt;
this.GridView1.DataKeyNames = new string[] {"ID"};
this.GridView1.DataBind();
}
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("chk");
if (chkAll.Checked == true)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
}
protected void btnExcel_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "维修信息.xls");
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
base.VerifyRenderingInServerForm(control);
}
}
下面是Try.aspx页面显示的数据
Try.aspx.cs是后台代码,这个代码只能实现整个页面数据的导出(并且导出后是乱码),怎么才能导出选中的数据呢?
请各位指点下下,谢谢
Try.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EveryMend.aspx.cs" Inherits="EveryMend" %><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<link href="Style.css" rel="Stylesheet" type="text/css" />
<style>
body
{
scrollbar-base-color:White;
}
.style2
{
width: 85px;
}
.style3
{
width: 261px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4"
Width="100%" Font-Size="12px" AutoGenerateColumns="False" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" /></ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="Month_Date" HeaderText="月份" NullDisplayText="\" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Apply_User" HeaderText="申请人" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Apply_Date" HeaderText="申请时间" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="PartMent" HeaderText="申请部门" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Equip_ID" HeaderText="设备编码" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Equip_Name" HeaderText="设备名称" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Equip_Brand" HeaderText="设备品牌" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Start_Date" HeaderText="开始维修时间"
NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="End_Date" HeaderText="维修结束时间" NullDisplayText="\" >
<HeaderStyle Wrap="False" HorizontalAlign="Left" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Stop_Date" HeaderText="影响停机时间" SortExpression="\" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Date" HeaderText="维修保养时间" NullDisplayText="\" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Sort1" HeaderText="维修分类1">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Sort2" HeaderText="维修分类2">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Place" HeaderText="维修地点">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Fault" HeaderText="人为/生产">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Result" HeaderText="维修结果">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Mend_User" HeaderText="维修负责人">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Complete_User" HeaderText="完工确认人">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Mend_Content" HeaderText="维修内容">
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
</Columns>
<PagerStyle BackColor="#FFFFFF" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
<table border="0" cellpadding="0" cellspacing="0" class="css" width="100%">
<tr>
<td align="center" class="style2">
<asp:CheckBox ID="chkAll" runat="server" Text="全选" AutoPostBack="true"
oncheckedchanged="chkAll_CheckedChanged" />
</td>
<td class="style3">
<asp:Button ID="btnExcel" runat="server" Text="导出到Excel"
onclick="btnExcel_Click" />
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Try.apsx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;
using System.Text;public partial class EveryMend : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
Handy();
}
}
public void Handy()
{
DateTime Doday = DateTime.Today.Date;
SqlConnection strcon=new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
strcon.Open();
string sql = "select * from Mend where EveryDate='" + Doday + "' order by ID desc";
SqlDataAdapter sda = new SqlDataAdapter(sql, strcon);
DataSet ds=new DataSet();
sda.Fill(ds);
DataTable dt = ds.Tables[0];
this.GridView1.DataSource = dt;
this.GridView1.DataKeyNames = new string[] {"ID"};
this.GridView1.DataBind();
}
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("chk");
if (chkAll.Checked == true)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
}
protected void btnExcel_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "维修信息.xls");
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
base.VerifyRenderingInServerForm(control);
}
}
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
我的那个天啊,不出乱码才怪呢!。。
Response.Charset = System.Text.Encoding.Default.EncodingName;// "utf-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
导出选中的数据:
这个你肯定要另外写代码了
最好是先将你选中了的数据取出来,然后实例化一个gridview,然后绑定这个数据,最后用同样的方法将gridview的数据导出成excel
思路就是这样的了,代码自己写吧。