我电脑上导出没乱码,但是同事的却乱码了!
粘出部分核心代码于2图片,帮忙看看:
page.Response.Clear();
page.Response.Buffer = true;
page.Response.Charset = "UTF-8";
page.Response.ContentEncoding = System.Text.Encoding.UTF8;
page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8) + ".xls");
page.Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw); hw.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");效果图:
1、2、http://b95.photo.store.qq.com/psb?/7df597a5-2d35-4add-845a-9fefa325cc10/3nOTgDw7Alt*6veNfPwwcCIRWPCQD9C9T4HrRyUDZV4!/b/YSj3pTj0KgAAYgOUqjjwKgAA
粘出部分核心代码于2图片,帮忙看看:
page.Response.Clear();
page.Response.Buffer = true;
page.Response.Charset = "UTF-8";
page.Response.ContentEncoding = System.Text.Encoding.UTF8;
page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8) + ".xls");
page.Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw); hw.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");效果图:
1、2、http://b95.photo.store.qq.com/psb?/7df597a5-2d35-4add-845a-9fefa325cc10/3nOTgDw7Alt*6veNfPwwcCIRWPCQD9C9T4HrRyUDZV4!/b/YSj3pTj0KgAAYgOUqjjwKgAA
是内容,中文的都乱了这是地址: 2、http://b95.photo.store.qq.com/psb?/7df597a5-2d35-4add-845a-9fefa325cc10/3nOTgDw7Alt*6veNfPwwcCIRWPCQD9C9T4HrRyUDZV4!/b/YSj3pTj0KgAAYgOUqjjwKgAA你看下!
hw.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("</body></html>");
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.IO;/// <summary>
///ExcelHelper 的摘要说明
/// </summary>
namespace DBUtility
{
public class ExportHelper
{ public static void ExportToExcel(IList dataList, string[] fields, string[] headTexts, string title)
{
GridView gvw = new GridView();
int ColCount, i; //如果筛选的字段和对应的列头名称个数相对的情况下只导出指定的字段
if (fields.Length != 0 && fields.Length == headTexts.Length)
{
ColCount = fields.Length;
gvw.AutoGenerateColumns = false; for (i = 0; i < ColCount; i++)
{
BoundField bf = new BoundField();
bf.DataField = fields[i];
bf.HeaderText = headTexts[i];
gvw.Columns.Add(bf);
}
}
else
{
gvw.AutoGenerateColumns = true;
} SetStype(gvw);
gvw.DataSource = dataList;
gvw.DataBind(); ExportToExcel(gvw, title);
}
/// <summary>
/// 导出数据到Excel
/// </summary>
/// <param name="DataList">IList Data</param>
/// <param name="Fields">要导出的字段</param>
/// <param name="HeadName">字段对应显示的名称</param>
public static void ExportToExcel(IList dataList, string[] fields, string[] headTexts)
{
ExportToExcel(dataList, fields, headTexts, string.Empty);
} /// <summary>
/// 设置样式
/// </summary>
/// <param name="gvw"></param>
private static void SetStype(GridView gvw)
{
gvw.Font.Name = "Verdana";
gvw.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
gvw.HeaderStyle.BackColor = System.Drawing.Color.LightCyan;
gvw.HeaderStyle.ForeColor = System.Drawing.Color.Black;
gvw.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
gvw.HeaderStyle.Wrap = false;
gvw.HeaderStyle.Font.Bold = false;
gvw.HeaderStyle.Font.Size = 15;
gvw.RowStyle.Font.Size = 12;
gvw.RowStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
}
/// <summary>
/// 导出GridView中的数据到Excel
/// </summary>
/// <param name="gvw"></param>
/// <param name="DataList"></param>
private static void ExportToExcel(GridView gvw, string title)
{
string fileName;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Charset = "GB2312";
// HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
fileName = string.Format("{0:yyyy-MM-dd_HH_mm_ss}.xls", DateTime.Now);
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
gvw.RenderControl(hw);
if (!string.IsNullOrEmpty(title))
{
HttpContext.Current.Response.Write("<b><center><font size=5 face=Verdana color=#000>" + title + "</font></center></b>");
}
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End(); gvw.Dispose();
tw.Dispose();
hw.Dispose(); gvw = null;
tw = null;
hw = null; }
}
}
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename=" +System.Web.HttpUtility.UrlEncode("ceshi1", System.Text.Encoding.UTF8) + ".xls");
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
// hw.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
hw.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("</body></html>");
http://download.csdn.net/detail/glyphvectory/2647486
有例子,以后生成excel很简单。不必那么写。
作为一个好的程序员,想的是:一切都应该被重用。