http://www.cnblogs.com/stswordman/archive/2006/08/24/485641.html
导出中文乱码,0123,去了0还未处理。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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;
//为导出excel增加:
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
private void BindData()
{
SqlConnection myConnection = new SqlConnection("Server=.\\sqlexpress;Database=kyj2008;Trusted_Connection=true");
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM zzc", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
//修改为GridView1
//GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Btn_ExportClick(object sender, EventArgs e)
{
string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//修改为GridView1
GridView1.RenderControl(htw);
// Style is added dynamically
Response.Write(style);
Response.Write(sw.ToString());
//HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");
//string fileName = HttpUtility.UrlEncode(ExcelFileName + ".xls", Encoding.GetEncoding("GB2312"));
//HttpContext.Current.Response.AddHeader("content-disposition",
//"attachment;filename=" + fileName);
Response.End();
}
protected void Btn_ExportExcelPaging(object sender, EventArgs e)
{
//修改为GridView1
DisableControls(GridView1);
Response.ClearContent();
Response.Charset = "GB2312";
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//修改为GridView1
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
private void DisableControls(Control gv)
{
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof(LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls())
{
DisableControls(gv.Controls[i]);
}
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
//修改为GridView1
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("class", "text");
}
}
}