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"));
    
     
  • 相关阅读:
    vue---mixins的用法
    vue---slot,slot-scoped,以及2.6版本之后插槽的用法
    Java实现DDD中UnitOfWork
    redis基础及redis特殊场景使用描述
    网易一千零一夜 读后初感
    产品经理与众不同的思维方式与“职业病”——《人人都是产品经理》
    【Ubuntu14】Nginx+PHP5+Mysql记录
    A标签/按钮防止重复提交&页面Loading制作
    PHPCMS v9 二次开发_验证码结合Session开发
    eclipse 编码设置【转】
  • 原文地址:https://www.cnblogs.com/cnishop/p/10721772.html
Copyright © 2011-2022 走看看