datagrid 导出 excel;AJAX的页面的…

public override void VerifyRenderingInServerForm(Control control)
  {
   //base.VerifyRenderingInServerForm (control);
  }

 

 

public static void ExportToExcelForDataGrid(DataGrid dg,string aDate,string aFileName)
  {
    HttpContext.Current.Response.Clear();
   HttpContext.Current.Response.Buffer= true;
   HttpContext.Current.Response.Charset="GB2312";
   HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlPathEncode(aFileName) +".xls");
   HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF-7");  
   HttpContext.Current.Response.ContentType = "application/ms-excel";  
   
   StringWriter oStringWriter = new System.IO.StringWriter();
   HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
   dg.RenderControl(oHtmlTextWriter);
   
   HttpContext.Current.Response.Write(oStringWriter.ToString());
   HttpContext.Current.Response.End();
  }

使用了AJAX的页面的GridView导出Excel,以及中文乱码问题解决  

2011-01-24 21:03:01|  分类: .Net 标签: |字号大中小 订阅

1.首先在需要导出Excel的页面的<%@ Page Title="" Language="C#" MasterPageFile="~Master.Master" CodeBehind="List.aspx.cs" Inherits="List" enableEventValidation   ="false"%>中加入enableEventValidation   ="false",这个很重要。

2.在UpdatePannel中加入按钮的触发器  

</ContentTemplate>  
            <Triggers>  
                <asp:PostBackTrigger ControlID="export_excel" />//这里的ID指的是你单击导出Execel的控件,使这个控件回发。  
            </Triggers>  
            </asp:UpdatePanel>
3.  在按钮单击事件中写如下函数     

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            GridView1.AllowPaging = false;
            string strStyle = "<style>td{mso-number-format:"\\@";}</style>";
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls ");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/ms-excel ";
            StringWriter oStringWriter = new StringWriter();
            HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
            this.GridView1.RenderControl(oHtmlTextWriter);
            Response.Write(strStyle);
            Response.Write(oStringWriter.ToString());
            //ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "alert", "Response.Write(oStringWriter.ToString())", true);
            Response.Flush();
            Response.End();
            GridView1.AllowPaging = true;        }

            问题:字符编码?用哪个?UTF-8、UTF-7、GB2312 

如果表中有中文,则推荐使用GB2312,Web.config中配置全球化:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
      <globalization requestEncoding="gb2312" responseEncoding="gb2312"/>//添加
    </system.web>

如果表中数字是以0开始的形如:08125...的话,使用string strStyle = "<style>td{mso-number-format:"\\@";}</style>";然后Response.Write(strStyle); 如上面代码中所示。

4.同样的在后台代码中写如下代码,这行代码是要确认运行时指定的服务器控件呈现,一定要添加
     public override void VerifyRenderingInServerForm(Control control)
    {
    }

 

 

==============合计

#region qzf2011-12-29 自动创建列绑定datagrid 的合计
  public static void DataGridItemDataBoundCalcSumForAutoColumn(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e,ArrayList alSum)
  {
   if (e.Item.ItemType == ListItemType.Header)
   {
    for (int i = 0 ;i <e.Item.Cells.Count;i++)
    {
     alSum.Add(0M);
    }
   }
   else if (e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem)
   {
    for (int i = 0 ;i <e.Item.Cells.Count;i++)
    {
     Decimal dbl = 0M;
     try
     {
      dbl = Decimal.Parse(e.Item.Cells[i].Text.Trim());
     }
     catch
     {
     }
     alSum[i] = Convert.ToDecimal(alSum[i]) + dbl;
    }
   }
   else if(e.Item.ItemType == ListItemType.Footer )
   {
 
    for (int i = 0 ;i <e.Item.Cells.Count;i++)
    {
     try
     {
      if (Convert.ToDecimal(alSum[i])==0) continue;
      
      e.Item.Cells[i].Text = alSum[i].ToString();
     }
     catch
     {
     }
    }
   }
  }
  #endregion
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值