Grid数据导出到Excel

  //Grid数据导出到Excel

private void ExportToExcel()
  {
   string strFileName;

   strFileName = HttpUtility.UrlEncode("导出名称.xls");

   Response.Clear();  //清空
   Response.Buffer = true;  //缓冲输出
   Response.Charset = "GB2312";

   Response.AppendHeader("Content-Disposition","attachment;filename=" + strFileName);
   Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
   Response.ContentType = "application/ms-excel";
   this.EnableViewState = false;
   //定义一个输入流
   System.IO.StringWriter swStringWriter = new System.IO.StringWriter();
   System.Web.UI.HtmlTextWriter htwHtmlTextWriter = new System.Web.UI.HtmlTextWriter(swStringWriter);

   ResetDataGrid(DataGrid1);//去除其它格式

   Response.Write ("<p align=/"center/"><b><font face=/"Verdana/" size=/"18px/">导出名称</b></p>");
   DataGrid1.Columns[13].Visible = false;
   DataGrid1.RenderControl(htwHtmlTextWriter);
   Response.Write (swStringWriter.ToString());
   Response.End();
  }


//去除其它格式
  private void ResetDataGrid(C1WebGrid grid)
  {
   int i;
   int intItemCount;
//   string strPlanName;
   HyperLink lnkPlanName;

   intItemCount = grid.Items.Count;

   for (i = 0; i < intItemCount; i++)
   {
    lnkPlanName = (HyperLink)grid.Items[i].Cells[6].FindControl("lnkPlanName");

    if (lnkPlanName != null)
    {
     lnkPlanName.NavigateUrl = "";

//     strPlanName = grid.Items[i].Cells[5].Text;
//
//     strPlanName = lnkPlanName.Text;
//
//     grid.Items[i].Cells[5].Controls.Clear();
//     
//     grid.Items[i].Cells[5].Text = strPlanName;
    }
   }
  } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值