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"));
    
     
  • 相关阅读:
    下拉选择框:数字从n到m
    event.target返回事件的目标节点(触发该事件的节点)
    nextSibling使用注意(html的注释)
    mysql导出 csv文件
    ie打印去掉页眉页脚
    jsp标签循环标签体
    String.format字符串格式化方法
    [**奇文共赏**补充问题] 据说看五遍能懂的人智商 > 200
    [转] ARP的处理办法!
    [转] 一张废手机卡的作用大全 (没试过)
  • 原文地址:https://www.cnblogs.com/cnishop/p/10721772.html
Copyright © 2011-2022 走看看