开发者

exported excel file has generate unknown characters

开发者 https://www.devze.com 2023-04-04 07:38 出处:网络
My code is this .. SqlConnection scn = new SqlConnection(clspublic.GetConnectionString()); SqlCommand scm = new SqlCommand(\"SELECTtblProduct.fName, tblCars.fCarType, tblProduct.fTechnicalNo, tblProd

My code is this ..

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

but when I click on export button it generate some unknown characters in columns .. my language is Persian language and I export Persian characters .

it shows this :

واشر کامل COROLLA 04111-22360 1 1312000 Ùابر یک 2005~2007 1800 cc 1 عدد 1.27 303.8 0

how can I fix this ?


You'll need to set your Code Page and Character Set on the Response object.

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消