列值转换为 formula/exponent
Column value is converted to formula/exponent
我在写入 CSV 文件时遇到了一些特定数据列的问题。大多数时候,该值都长于 11 characters/digits。所以它在 CSV 中生成为 7.47313E+11
,而实际上它应该读作 747313016112
。我最初使用 作为参考来尝试解决我的问题,但它不起作用。
需要注意的一件事是数据库中的列(标题为 Barcode
)是一个 varchar。但是,只有数字值会保存到此列。所以我想在 CSV 生成过程中(或生成之后)的某个时刻,Excel 将其识别为数字,而不是字符串。
到目前为止,这是我的工作代码:
public static void GenerateChartsCSVFile(IEnumerable<ChartsData> chartsData)
{
string fullPath = "C:\chart-data.csv";
using (var writer = new StreamWriter(fullPath, true, Encoding.GetEncoding("iso-8859-1")))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(chartsData);
writer.Flush();
}
if (File.Exists(fullPath))
Console.WriteLine("Charts CSV file " + csvFile + " successfully created!");
else
Console.WriteLine("Oops, something went wrong with generating the Charts CSV file.");
}
数据模型:
public class ChartsData
{
public string CatalogueID { get; set; }
public string Barcode { get; set; }
public string Title { get; set; }
public string ReleaseDate { get; set; }
public int DiscNo { get; set; }
public int TrackNo { get; set; }
public string TrackTitle { get; set; }
public string ISRC { get; set; }
public string Duration { get; set; }
public string LabelID { get; set; }
public string Label { get; set; }
public string TrackContributors { get; set; }
}
非常感谢您!
只需添加一组双引号。 CsvHelper 自动将字符串中的引号转义为另一组双引号。字符串 =""747313016112""
将以 =""""747313016112""""
结束,因为 CsvHelper 将转义每个双引号。这就是为什么您的值在 Excel.
中看起来像 "747313016112"
chartsData.Barcode = "=\"" + chartsData.Barcode + "\"";
CsvHelper 正在关注 RFC 4180
- If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote.
例如:
"aaa","b""bb","ccc"
我在写入 CSV 文件时遇到了一些特定数据列的问题。大多数时候,该值都长于 11 characters/digits。所以它在 CSV 中生成为 7.47313E+11
,而实际上它应该读作 747313016112
。我最初使用
需要注意的一件事是数据库中的列(标题为 Barcode
)是一个 varchar。但是,只有数字值会保存到此列。所以我想在 CSV 生成过程中(或生成之后)的某个时刻,Excel 将其识别为数字,而不是字符串。
到目前为止,这是我的工作代码:
public static void GenerateChartsCSVFile(IEnumerable<ChartsData> chartsData)
{
string fullPath = "C:\chart-data.csv";
using (var writer = new StreamWriter(fullPath, true, Encoding.GetEncoding("iso-8859-1")))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(chartsData);
writer.Flush();
}
if (File.Exists(fullPath))
Console.WriteLine("Charts CSV file " + csvFile + " successfully created!");
else
Console.WriteLine("Oops, something went wrong with generating the Charts CSV file.");
}
数据模型:
public class ChartsData
{
public string CatalogueID { get; set; }
public string Barcode { get; set; }
public string Title { get; set; }
public string ReleaseDate { get; set; }
public int DiscNo { get; set; }
public int TrackNo { get; set; }
public string TrackTitle { get; set; }
public string ISRC { get; set; }
public string Duration { get; set; }
public string LabelID { get; set; }
public string Label { get; set; }
public string TrackContributors { get; set; }
}
非常感谢您!
只需添加一组双引号。 CsvHelper 自动将字符串中的引号转义为另一组双引号。字符串 =""747313016112""
将以 =""""747313016112""""
结束,因为 CsvHelper 将转义每个双引号。这就是为什么您的值在 Excel.
"747313016112"
chartsData.Barcode = "=\"" + chartsData.Barcode + "\"";
CsvHelper 正在关注 RFC 4180
- If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote.
例如:
"aaa","b""bb","ccc"