-1

I am trying to export from stored procedure into excel but char Arabic not recognized so here the code which i used on my button

try
{
       // Bind table data to Stream Writer to export data to respective folder
       StreamWriter wr = new StreamWriter(@"C:\\AllValidateReturn.xls");
       // Write Columns to excel file
       for (int i = 0; i < datatable.Columns.Count; i++)
       {
           wr.Write(datatable.Columns[i].ToString().ToUpper() + "\t");
       }
       wr.WriteLine();
       //write rows to excel file
       for (int i = 0; i < (datatable.Rows.Count); i++)
       {
            for (int j = 0; j < datatable.Columns.Count; j++)
            {
                 if (datatable.Rows[i][j] != null)
                 {
                    wr.Write(Convert.ToString(datatable.Rows[i][j]) + "\t");
                 }
                 else
                 {
                    wr.Write("\t");
                 }
            }
            wr.WriteLine();
       }
       wr.Close();
       const string message = "Export Done to  C:\\AllValidateReturn.xls file";
       const string caption = "Exprot Total Counting";
       var result = MessageBox.Show(message, caption,
       MessageBoxButtons.OK,
       MessageBoxIcon.Information);
}

could you please help me to solve this problem

Filburt
  • 17,626
  • 12
  • 64
  • 115
mohammed
  • 5
  • 6
  • 1
    What does *"not recognised"* mean? Where is your SQL in relation to this? – Thom A Oct 28 '19 at 09:29
  • Make sure to set the encoding on your StreamWriter: [StreamWriter(System.IO.Stream stream, System.Text.Encoding encoding)](https://learn.microsoft.com/en-us/dotnet/api/system.io.streamwriter.-ctor?view=netframework-4.8#System_IO_StreamWriter__ctor_System_IO_Stream_System_Text_Encoding_) – Filburt Oct 28 '19 at 09:31
  • Please attach an example output file (a **real one**) so we can download and have a look? – mjwills Oct 28 '19 at 09:31
  • 2
    Why are you saving a tab delimited file with the extension of `xls`? `xls` is a quite specific binary format. I mean it might _work_ (since Excel is very tolerant), but it is _weird._ – mjwills Oct 28 '19 at 09:31
  • I guess you need to set the encoding of the 'xls' file you are creating (BTW, you are creating a TAB separated CSV file there, not a 'xls' file.) – r41n Oct 28 '19 at 09:33
  • Which encoding are you using? – Mohammed Noureldin Oct 28 '19 at 09:34
  • 1
    Have you tried https://stackoverflow.com/a/8214534/34092 ? – mjwills Oct 28 '19 at 09:35

1 Answers1

0

You can pass getcoding type to streamwriter and reader as below

StreamWriter(@"C:\\AllValidateReturn.xls", true, System.Text.Encoding.GetEncoding("Arabic"))
SJN
  • 377
  • 2
  • 8
  • 18
  • still same but when i used this StreamWriter(@"C:\\AllValidateReturn.xls",true, System.Text.Encoding.UTF8) word appear fine but show me table all as one column – mohammed Oct 28 '19 at 12:04