簡體   English   中英

導出到Excel-防止電話號碼用C#的科學記數法

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

我正在將數據從數據庫導出到C#中的excel,它具有電話號碼列,例如+22200073886。 如何在不將數字轉換為科學計數法的情況下導出為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());

首先,您不得保存帶有XLS擴展名的CSV文件。 您必須使用OpenXML或可以從C#使用的其他任何Excel庫導出真實的Excel文件 (如xls或xlsx)。

然后,您必須將單元格中的數據保存為字符串或將單元格的數字格式設置為文本。 文本的數字格式為“ @” 代碼取決於您選擇的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM