Karthik K

Karthik K

  • 935
  • 738
  • 186.7k

How to export Excel with chines character ?

Jun 12 2021 4:01 AM

Hi ,

      I having the issue when export excel in csv format when get the chines characters from the data base. if anyone knows suggest me wth sample code . i share my code . 

 protected void ExportCSV(object sender, EventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            //using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM che.Customers"))

            using (MySqlCommand cmd = new MySqlCommand("select I.Barcode, I.InventoryName, I.Description ,C.CategoryID,I.Price,I.Price2,I.Price3,I.Price4,I.onhand as Stock,ic.Unitcost,i.NoPriceShift,I.Group1,I.Group2,I.Group3,I.Group4,I.Group5,I.StockCode,I.Unit,I.CommissionPercent1,I.CommissionPercent2,I.CommissionPercent3,I.CommissionAmount1,I.CommissionAmount2,I.ERPItemCode1,I.ERPItemCode2,I.ERPItemDesc,I.Imagepath,sum(pquantity-(CutQuantity+RetailQty))as OnHand   from da1.inventory as I  join da1.inventorycategories  as c ON I.Categoryid=c.categoryid  join da1.inventorycostaverage ic on ic.inventoryid=i.inventoryid group by i.inventoryid ;"))
            {
                using (MySqlDataAdapter sda = new MySqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);

                        //Build the CSV file data as a Comma separated string.
                        string csv = string.Empty;

                        foreach (DataColumn column in dt.Columns)
                        {
                            //Add the Header row for CSV file.
                            csv += column.ColumnName + ',';
                        }

                        //Add new line.
                        csv += "\r\n";

                        foreach (DataRow row in dt.Rows)
                        {
                            foreach (DataColumn column in dt.Columns)
                            {
                                //Add the Data rows.
                                csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
                            }

                            //Add new line.
                            csv += "\r\n";
                        }

                        //Download the CSV file.
                        Response.Clear();
                        Response.Buffer = true;
                        Response.AddHeader("content-disposition", "attachment;filename=SqlExport.csv");
                        Response.Charset = "";
                        
                        Response.ContentType = "application/text";
                        
                        Response.Output.Write(csv);
                        Response.ContentEncoding = System.Text.Encoding.UTF8;
                        Response.Flush();
                        Response.End();
                    }
                }
            }
        }
    }

 


Answers (1)