2

I am exporting data from the DB to excel in C# and it has a phone number column eg +22200073886. How do I Export to excel without converting the number to scientific notation?

var csv = new StringBuilder();
        string strContent = "";
        int counter = DS.Tables[0].Columns.Count;
        // Build the file content
        for (int i = 0; i <= DS.Tables[0].Rows.Count; i++)
        {
            for (int j = 0; j < counter; j++)
            {
                if (i == 0)
                {
                  strContent += DS.Tables[0].Columns[j].ToString() + ",";
                }
                else
                {
              strContent += DS.Tables[0].Rows[i - 1][j].ToString() + ",";
                }
            }
          csv.AppendLine(strContent.Substring(0, strContent.Length - 1));
            strContent = "";
        }

  File.WriteAllText(new Page().Server.MapPath(filePath), csv.ToString());
purple
  • 43
  • 3

1 Answers1

0

First you must not to save a CSV file with XLS extension. You must export real Excel file, like xls or xlsx, using OpenXML or any other library for Excel that can be used from C#.

Then, you must save your data in cell as string or set the number format of the cell as text. The number format for text is "@". The code depends on the solution you choose.

alex.pulver
  • 2,107
  • 2
  • 31
  • 31