导出的excel文件已经生成未知的字符

我的代码是这个..

SqlConnection scn = new SqlConnection(clspublic.GetConnectionString()); SqlCommand scm = new SqlCommand("SELECT tblProduct.fName, tblCars.fCarType, tblProduct.fTechnicalNo, tblProduct.fProductionDate, tblProduct.fEngineType, tblProduct.fNoinStock, tblProduct.fNoforCar, tblProduct.fPrice, tblProduct.fImage, tblProduct.fDesc, tblParts.fPart, tblLevels.fLevel, tblProduct.fUnitType, tblProduct.fRatio, tblProduct.fDirham, tblProduct.fExtraMoney, tblProduct.fChanged FROM tblProduct INNER JOIN tblCars ON tblProduct.fxCarType = tblCars.fId INNER JOIN tblParts ON tblProduct.fxPartType = tblParts.fId INNER JOIN tblLevels ON tblProduct.fxLevel = tblLevels.fId", scn); scn.Open(); GridView3.DataSource = scm.ExecuteReader(); GridView3.DataBind(); scn.Close(); Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=OrderReport.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView3.AllowPaging = false; //Change the Header Row back to white color GridView3.HeaderRow.Style.Add("background-color", "#FFFFFF"); //Apply style to Individual Cells GridView3.HeaderRow.Cells[0].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[1].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[2].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[3].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[4].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[5].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[6].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[7].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[8].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[9].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[10].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[11].Style.Add("background-color", "green"); GridView3.HeaderRow.Cells[12].Style.Add("background-color", "green"); for (int i = 0; i < GridView3.Rows.Count; i++) { GridViewRow row = GridView3.Rows[i]; //Change Color back to white row.BackColor = System.Drawing.Color.White; //Apply text style to each Row row.Attributes.Add("class", "textmode"); //Apply style to Individual Cells of Alternating Row if (i % 2 != 0) { row.Cells[0].Style.Add("background-color", "#C2D69B"); row.Cells[1].Style.Add("background-color", "#C2D69B"); row.Cells[2].Style.Add("background-color", "#C2D69B"); row.Cells[3].Style.Add("background-color", "#C2D69B"); row.Cells[4].Style.Add("background-color", "#C2D69B"); row.Cells[5].Style.Add("background-color", "#C2D69B"); row.Cells[6].Style.Add("background-color", "#C2D69B"); row.Cells[7].Style.Add("background-color", "#C2D69B"); row.Cells[8].Style.Add("background-color", "#C2D69B"); row.Cells[9].Style.Add("background-color", "#C2D69B"); row.Cells[10].Style.Add("background-color", "#C2D69B"); row.Cells[11].Style.Add("background-color", "#C2D69B"); row.Cells[12].Style.Add("background-color", "#C2D69B"); } } GridView3.RenderControl(hw); //style to format numbers to string string style = @"<style> .textmode { mso-number-format:\@; } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); 

但是当我点击导出button,它会在列中生成一些未知的字符..我的语言是波斯语,我出口波斯语字符。

它显示了这一点:

“COROLLA 04111-22360 1 1312000Ùاب±ÚÚ©2005〜2007 1800 cc 1Ølدد1.27 303.8 0

我怎样才能解决这个问题 ?

您需要在Response对象上设置您的代码页和字符集。

http://msdn.microsoft.com/en-us/library/ms524628(v=vs.90).aspx

http://msdn.microsoft.com/en-us/library/aa752010.aspx