GridView使用自定义表头和导出到Excel
2007-12-18 11:08
666 查看
如上图:查看列 是静态添加上的,表头和数据则是动态添加的。
代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class jjzbtjb_ck : System.Web.UI.Page
{
public int sh = 0;
string cosql = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cosql = "SELECT 年, max(_机构) AS 供电所, MAX(供电量累计) AS 供电量, MAX(售电量累计) AS 售电量, MAX(损失电量高低压累计) AS 损失电量高低压, MAX(损失电量高压累计) AS 损失电量高压, MAX(损失电量低压累计) AS 损失电量低压, MAX(损失率高低压累计) AS 损失率高低压, MAX(损失率高压累计) AS 损失率高压, MAX(损失率低压累计) AS 损失率低压, MAX(电费金额累计) AS 电费金额, MAX(平均售电电价累计) AS 平均售电电价 FROM td_jjzbtjb GROUP BY 年";
BindGridView(cosql);
GridView1.HeaderRow.Cells.Clear();
TableCell cell = new TableCell();
cell.Text = "<tr><td rowspan='2'>查看</td><td rowspan='2'>年份</td><td rowspan='2'>供电所</td><td rowspan='2'>供电量(万KWh)</td><td rowspan='2'>售电量(万KWh)</td><td colspan='3'>损失电量(万KWh)</td><td colspan='3'>损失率(%)</td><td rowspan='2'>电费金额(元)</td><td rowspan='2'> 平均售电电价(元/千KWh)</td></tr><tr><td>高低压</td><td>高压</td><td>低压</td><td>高低压</td><td>高压</td> <td>低压</td> </tr>";
GridView1.HeaderRow.Cells.Add(cell);
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "chakan")
{
Session["gongdiansuo"] = this.GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Values["供电所"].ToString();
if (Session["gongdiansuo"].ToString()!= "")
{
Server.Transfer("jjzbtjb_tq.aspx");
}
else
{
Response.Write("没供电所");
Response.End();
}
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//重载该方法是为避免下面的错误
// 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
//异常详细信息: System.Web.HttpException: 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
}
//下面导出到Excel
protected void Button1_Click(object sender, EventArgs e)
{
//string style = @"<style> .text { mso-number-format:/@; } </script> ";
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.AppendHeader("Content-Disposition", "attachment;filename=GongDianSuoBiao.xls");
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
protected void BindGridView(string sql)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString.ToString());
DataSet ds = new DataSet();
con.Open();
System.Data.SqlClient.SqlDataAdapter rs = new System.Data.SqlClient.SqlDataAdapter(sql, con);
rs.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
}
说明:我之前用了SqlDataSource 绑定到GridView,然后添加的表头,可是当导出到Excel之后,表头就丢失了!动态绑定数据源没出现这种情况,所以折中了下,静态加的查看列,动态加的数据源。效果达到^_^
相关文章推荐
- ASP.NET使用GridView导出Excel实现方法
- 如何使用html表格自定义或采用tableexport.js第三方插件以excel格式导出
- 关于Gridview中使用控件和图片导出到Excel的处理
- 在GridView中使用CSS固定上、左表头(似Excel冻结行列一样)(限IE)(含操作视频)
- java自定义注解在excel导出时的使用
- 【Vegas原创】GridView修改表头,并导出Excel(C#版)
- 使用ASP.NET的GridView导出/儲存資料至Excel,Word,Txt檔,Powerpoint
- C#使用RenderControl将GridView控件导出到EXCEL的方法
- 使用aspose.cell动态导出多表头 EXCEL
- 关于Gridview中使用控件和图片导出到Excel的处理
- 使用GridView控件导出数据源的数据为Excel、Word或Text
- 项目中使用POI导出Excel的一些心得(包含复杂表头和动态页数与列数)
- 使用GridView将数据导入或导出Excel中
- ASP.NET使用GridView导出Excel实现方法
- 使用ASP.NET的GridView导出/儲存資料至Excel,Word,Txt檔,Powerpoint
- SQL Server 2005使用BCP命令将数据表导出到Excel第一行包括表头
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- 使用GridView将数据导入或导出Excel中
- 如何使用html表格自定义或采用tableexport.js第三方插件以excel格式导出
- 使用ASP.NET的GridView导出/儲存資料至Excel,Word,Txt檔,Powerpoint