导出成xls文件用excel打开。通用的代码如下:

 

 #region 导出
        
//common
        public void ExcuteOut(GridView gv)
        {
            
if (gv.Rows.Count > 0)
            {
                Response.ClearContent();
                Response.AddHeader(
"content-disposition", "attachment; filename=DMReport" + DateTime.Now.ToString("_yyyyMMdd_HHmmss") + ".xls");
                Response.ContentEncoding 
= System.Text.Encoding.UTF8;
                Response.ContentType 
= "application/excel";
                StringWriter sw 
= new StringWriter();
                HtmlTextWriter htw 
= new HtmlTextWriter(sw);
                gv.RenderControl(htw);
                Response.Write(sw.ToString());
                Response.End();
            }
            
else
            {
                Tools.Alert(Page, 
"没有数据!");
            }
        }
        
public override void VerifyRenderingInServerForm(Control control)
        { }
        
#endregion

 

 必须加上后面的VerifyRenderingInServerForm函数,否则会报错。

注意要使用utf8编码,不能用default。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-02
  • 2022-12-23
  • 2021-12-05
  • 2021-10-14
  • 2021-09-10
  • 2021-12-29
相关资源
相似解决方案