GridView数据导入Excel
2008-09-26 17:41
405 查看
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>GridView数据导入Excel</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333" PageSize="3" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" />
<asp:BoundField DataField="Name" HeaderText="用户姓名" />
<asp:BoundField DataField="Sex" HeaderText="性别" />
<asp:BoundField DataField="Birthday" HeaderText="出生日期" DataFormatString="{0:yyyy年M月dd日}" HtmlEncode="False" />
<asp:BoundField DataField="Salary" HeaderText="薪水" />
<asp:BoundField DataField="Address" HeaderText="家庭住址" />
<asp:BoundField DataField="PostCode" HeaderText="邮政编码" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
</div>
</form>
</body>
</html>
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.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlConn;
SqlCommand sqlComm;
string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
}
private void databind()
{
string strSql = "select * from myDt";
sqlConn = new SqlConnection(strConn);
SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
DataSet ds = new DataSet();
sqlConn.Open();
sqlDa.Fill(ds, "myDt");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
DataRowView drv = ds.Tables["myDt"].DefaultView[i];
string strSalary = Convert.ToString(drv["Salary"]);
if (Convert.ToDouble(strSalary) < 2500)
{
GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
}
}
sqlConn.Close();
}
double sum = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[4].Text);
}
else if(e.Row.RowType==DataControlRowType.Footer)
{
e.Row.Cells[3].Text = "总薪水为:";
e.Row.Cells[4].Text = sum.ToString();
e.Row.Cells[5].Text = "平均薪水为:";
e.Row.Cells[6].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
}
}
private void Export(string strFileType, string strFileName)
{
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8).ToString());
Response.ContentType = strFileType;
this.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "学生成绩报表.xls");
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>GridView数据导入Excel</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333" PageSize="3" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" />
<asp:BoundField DataField="Name" HeaderText="用户姓名" />
<asp:BoundField DataField="Sex" HeaderText="性别" />
<asp:BoundField DataField="Birthday" HeaderText="出生日期" DataFormatString="{0:yyyy年M月dd日}" HtmlEncode="False" />
<asp:BoundField DataField="Salary" HeaderText="薪水" />
<asp:BoundField DataField="Address" HeaderText="家庭住址" />
<asp:BoundField DataField="PostCode" HeaderText="邮政编码" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
</div>
</form>
</body>
</html>
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.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlConn;
SqlCommand sqlComm;
string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
}
private void databind()
{
string strSql = "select * from myDt";
sqlConn = new SqlConnection(strConn);
SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
DataSet ds = new DataSet();
sqlConn.Open();
sqlDa.Fill(ds, "myDt");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
DataRowView drv = ds.Tables["myDt"].DefaultView[i];
string strSalary = Convert.ToString(drv["Salary"]);
if (Convert.ToDouble(strSalary) < 2500)
{
GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;
}
}
sqlConn.Close();
}
double sum = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[4].Text);
}
else if(e.Row.RowType==DataControlRowType.Footer)
{
e.Row.Cells[3].Text = "总薪水为:";
e.Row.Cells[4].Text = sum.ToString();
e.Row.Cells[5].Text = "平均薪水为:";
e.Row.Cells[6].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
}
}
private void Export(string strFileType, string strFileName)
{
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8).ToString());
Response.ContentType = strFileType;
this.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "学生成绩报表.xls");
}
}
相关文章推荐
- GridView数据导入Excel/Excel数据读入GridView
- asp.net中实现GridView中的数据导入到Excel中
- 多个GridView数据导入Excel中
- GridView数据导入Excel/Excel数据读入GridView
- 把Excel中的数据导入gridView显示,再导入数据库
- GridView数据导入Excel/Excel数据读入GridView
- Gridview往Excel中导入数据出现中文乱码的解决办法?
- GridView数据导入Excel
- gridView数据导入到Excel
- asp.net 将Excel数据导入到GridView和SQL数据库
- 发布一个Excel导入数据到GridView的类
- 将gridview的数据导入excel
- 将GridView中的数据导入到EXCEL中!解决方案
- 把Excel中的数据导入gridView显示,再导入数据库
- 使用GridView将数据导入或导出Excel中
- GridView数据导入Excel/Excel数据读入GridView
- GridView数据导入Excel/Excel数据读入GridView
- GridView数据导入Excel/Excel数据读入GridView
- GridView数据导入Excel/Excel数据读入GridView
- GridView数据导入Excel