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

时间:2011-09-13 11:53:04

标签: asp.net excel character-encoding asp.net-ajax

我的代码就是这个..

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();

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

它显示了这个:

ÙاØ'رکاÙ...Ù“COROLLA 04111-22360 1 1312000Ùابریک2005~2007 1800 cc1عد¯¯1.27303.8 0

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)