zoukankan      html  css  js  c++  java
  • ASP.NET导出Excel或Word文件格式

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

    网上有好多关于asp.net导出的文章,我这里不是什么新的话题。

    这次整理材料正好把它发出来供大家参考。

    不多说了,直接上代码了 

    还有一句:

     

        //说明下 EnableEventValidation="false"的使用;
        //在页面上添加了输入型服务器控件时(如 TextBox),就需要设置为false了,否则会报错;也就是关闭页面验证,默认是开启的。

     //就是这个样子

    <%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

     

     

     

        private void DBExport()
        {
            HttpContext.Current.Response.Charset 
    = "GB2312";
            HttpContext.Current.Response.ContentEncoding 
    = Encoding.UTF8;
            
    //有部分文章里使用的是UTF7,是因为在特殊情况下中文会出现乱码;这里建议使用UTF8,MSDN中提到UTF7没有UTF8安全性高;
            
    //下面两行可以保证其正确性,使用方法见代码中
            
    //Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
            
    //Response.Write("</body></html>");
            
    //这里对文件名称时行了编码处理,以防止出现中文名称乱码的现象
            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;
            System.IO.StringWriter tw 
    = new System.IO.StringWriter();
            HtmlTextWriter hw 
    = new HtmlTextWriter(tw);
            divid.RenderControl(hw);
            
    //下边的三行才是数据的输出
            Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
            HttpContext.Current.Response.Write(tw.ToString());
            Response.Write(
    "</body></html>");

            Response.Flush();
            Response.Close();
        }
        
    //这个方法需要重写,否则会报错
        public override void VerifyRenderingInServerForm(Control control)
        {

        }
  • 相关阅读:
    java程序打包成jar 配置文件信息路径
    django 认证系统
    django 表单验证和字段验证
    python 面向对象编程
    Python new() 方法
    Django 分页 以及自定义分页
    django 自己编写admin
    Django CRM客户关系管理系统
    Django model中的 class Meta 详解
    Django CRM系统
  • 原文地址:https://www.cnblogs.com/hanmos/p/1939827.html
Copyright © 2011-2022 走看看