-1

我有以下代码

using (StreamWriter streamwriter = new StreamWriter(savetopath))
{
    using (CsvWriter csv = new CsvWriter(streamwriter))
    {
        List<string> columns = new List<string>();
        foreach (DataColumn column in shopifyDatatable.Columns)
            csv.WriteField(column.ColumnName.ToString());

        csv.NextRecord();

        foreach (DataRow row in shopifyDatatable.Rows)
        {
            for (var i = 0; i < shopifyDatatable.Columns.Count; i++)
            {
                csv.WriteField(row[i].ToString());
            }
            csv.NextRecord();
        }
    }
}

一切正常,但诸如 017825892588 之类的字段最终为 17825892588。

谁能帮我解决这个问题?

4

1 回答 1

1

在我的机器上工作。完整的 MCVE

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using CsvHelper;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string savetopath = @"C:\Users\myusername\Desktop\test.csv";
            DataTable table = new DataTable();
            table.Columns.Add("Column A", typeof(string));
            table.Rows.Add("056");

            using (StreamWriter streamwriter = new StreamWriter(savetopath))
            {
                using (CsvWriter csv = new CsvWriter(streamwriter))
                {
                    List<string> columns = new List<string>();
                    foreach (DataColumn column in table.Columns)
                        csv.WriteField(column.ColumnName.ToString());

                    csv.NextRecord();

                    foreach (DataRow row in table.Rows)
                    {
                        for (var i = 0; i < table.Columns.Count; i++)
                        {
                            csv.WriteField(row[i].ToString());
                        }
                        csv.NextRecord();
                    }
                }
            }

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey(true);
        }
    }
}

输出:

Column A
056
于 2017-09-20T17:44:16.353 回答