• ASP.NET导出Excel或Word文件格式


      //说明下 EnableEventValidation="false"的使用;
      //在页面上添加了输入型服务器控件时(如 TextBox),就需要设置为false了,否则会报错;也就是关闭页面验证,默认是开启的。
      //就是这个样子
    以下是代码片段:
    <%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 以下是代码片段:
        private void DBExport()
      {  www.szfuao.com
      HttpContext.Current.Response.Charset = "GB2312";
      HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
      //有部分文章里使用的是UTF7,是因为在特殊情况下中文会出现乱码;这里建议使用UTF8,MSDN中提到UTF7没有UTF8安全性高;
      //下面两行可以保证其正确性,使用方法见代码中
      //Response.Write("");
      //Response.Write("");
      //这里对文件名称时行了编码处理,以防止出现中文名称乱码的现象
      HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("文件名称.xls", Encoding.UTF8));
      //导出excel格式,因为格式不同,所以文件名称后缀要根据实际情况进行调整(.xls)
      HttpContext.Current.Response.ContentType = "vnd.ms-excel";
      //导出word格式,因为格式不同,所以文件名称后缀要根据实际情况进行调整(.doc)
      //HttpContext.Current.Response.ContentType = "vnd.ms-word";
      //导出html格式,因为格式不同,所以文件名称后缀要根据实际情况进行调整(.html)
      //HttpContext.Current.Response.ContentType = "text/HTML";
      //还有两种写法好像是可以直接输出图像,没来得及加以考证,不过应该不是像上边一样改下格式就可以的,应该是先创建图形对象才可以设置Response.ContentType才能输出
      //哪位有简单的方式希望贴出来,学习下,谢谢
      //HttpContext.Current.Response.ContentType = "image/GIF";
      //HttpContext.Current.Response.ContentType = "image/JPEG";
      //说明下 divid 是什么,这里应该是你要转出的控件,可以是服务器控件也可以的HTML控件(要加上 runat="server"否则这里是找不到控件的)
      //我的页面里是一个 div id="divid" runat="server" 里放了一个GridView用于显示数据
      divid.Page.EnableViewState = false;  www.yzjxsp.com
      System.IO.StringWriter tw = new System.IO.StringWriter();
      HtmlTextWriter hw = new HtmlTextWriter(tw);
      divid.RenderControl(hw);
      //下边的三行才是数据的输出
      Response.Write("");
      HttpContext.Current.Response.Write(tw.ToString());
      Response.Write("");
      Response.Flush();
      Response.Close();
      }
      //这个方法需要重写,否则会报错
      public override void VerifyRenderingInServerForm(Control control)
      {
      }
  • 相关阅读:
    「网易官方」极客战记(codecombat)攻略-沙漠-许愿井-wishing-well
    「网易官方」极客战记(codecombat)攻略-沙漠-一打宝石-diamond-dozen
    「网易官方」极客战记(codecombat)攻略-沙漠_士兵、食人魔和农民-soldier-ogre-and-peasant
    「网易官方」极客战记(codecombat)攻略-沙漠-空中桥梁-air-bridge
    「网易官方」极客战记(codecombat)攻略-沙漠-脆弱的士气-brittle-morale
    Linux——常用命令
    Linux——目录结构
    Vue 前端——逻辑:登录,注销 状态
    django——Auth 模块
    cookie 与 session
  • 原文地址:https://www.cnblogs.com/haosola/p/1956573.html
Copyright © 2020-2023  润新知