asp.net导出excel,excel是乱码
我要将gridview中的数据导出到一张excel中,但是导出后发现居然是乱码的,并且只有一行,想请问下高手们我这是什么情况
难道是excel还要先设置?
下面是我的后台代码:
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;using System.Data.SqlClient;using System.Drawing;using System.IO;using System.Text;namespace WebApplication5{ public partial class outexcel : System.Web.UI.Page { SqlConnection sqlcon; // SqlCommand sqlcom; string strCon = "Data Source=(local);Database=drop;Uid=sa;Pwd=sa123456"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } public void bind() { string sqlstr = "select id,createdate from test01"; sqlcon = new SqlConnection(strCon); SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds, "test01"); GridView1.DataSource = myds; GridView1.DataKeyNames = new string[] { "id" }; GridView1.DataBind(); sqlcon.Close(); } protected void Button1_Click(object sender, EventArgs e) { Export("application/ms-excel", "name.xls"); } private void Export(string FileType, string FileName) { Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF7; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); GridView1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { } }}