简体   繁体   English

导出到Excel-防止电话号码用C#的科学记数法

[英]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. 我正在将数据从数据库导出到C#中的excel,它具有电话号码列,例如+22200073886。 How do I Export to excel without converting the number to scientific notation? 如何在不将数字转换为科学计数法的情况下导出为ex​​cel?

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. 首先,您不得保存带有XLS扩展名的CSV文件。 You must export real Excel file , like xls or xlsx, using OpenXML or any other library for Excel that can be used from C#. 您必须使用OpenXML或可以从C#使用的其他任何Excel库导出真实的Excel文件 (如xls或xlsx)。

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. 代码取决于您选择的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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