简体   繁体   中英

Export to Excel - preventing phone numbers from scientific notation in C#

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM