zoukankan      html  css  js  c++  java
  • ASP.NET gridview导出excel,防止繁体产生有乱码的方式

    //1.先引用比如 :
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Diagnostics;
    using System.Data.SqlClient;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
     
       /// <summary>
        /// 清除控件中的所有控件,以便导出Excel
        /// </summary>
        /// <param name="control"></param>
        public static void ClearControls(Control control)
        {
            for (int i = control.Controls.Count - 1; i >= 0; i--)
            {
                ClearControls(control.Controls[i]);
    
            }
    
            if (!(control is TableCell))
            {
                if (control.GetType().GetProperty("SelectedItem") != null)
                {
                    LiteralControl literal = new LiteralControl();
                    control.Parent.Controls.Add(literal);
                    try
                    {
                        literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
                    }
                    catch
                    {
                    }
                    control.Parent.Controls.Remove(control);
                }
                else if (control.GetType().GetProperty("Text") != null)
                {
                    LiteralControl literal = new LiteralControl();
                    control.Parent.Controls.Add(literal);
                    literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
                    control.Parent.Controls.Remove(control);
                }
            }
    
    
            return;
        }
    
    
    //重点导出功能,采用utf8 编码
    
        public static void ExportToExcelUnicode(GridView GV_PO,string ExcelName)
        {
            
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + ExcelName + ".xls");
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
            GV_PO.RenderControl(hw);
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
    
        }
    
    //把上方两个功能建立在 APPCODE 中 ,单独的类,比如 ebs 
    
    //然后可以调用:
    
    EBS.ClearControls(GV_PO);
    EBS.ExportToExcelUnicode(GV_PO, "ParmSetPallet" + DateAndTime.Now.ToString("yyyyMMddhhmmss"));
    
     
  • 相关阅读:
    eclipse部署web项目至本地的tomcat但在webapps中找不到
    tomcat使用jdbc连接mysql出现的错误
    MySQL-5.6.13解压版(zip版)安装配置教程
    mysql简单用法
    关于java代理(静态代理和动态代理)
    shell 基础
    一、Django入门
    java 强制类项转换
    Java多态性详解——父类引用子类对象
    Java中抽象类和接口的区别
  • 原文地址:https://www.cnblogs.com/cnishop/p/10721772.html
Copyright © 2011-2022 走看看