-1

こんにちは皆さん、キーと値のペアを介しcreate multiple CSV filesて渡される関数があります。DataTablesmaller chunks based on sizeapp.config

以下のコードの問題:

  1. ファイル サイズを 1 kb にハードコーディングしました。値 を渡すと、20.csv ファイルが作成されます20kb。現在5kb、同じ値に対して のファイル サイズを作成しています。
  2. 最後に残ったレコードについては、ファイルを作成していません。

これを修正するのを手伝ってください。ありがとう!

コード:

public static void CreateCSVFile(DataTable dt, string CSVFileName)
    {

        int size = Int32.Parse(ConfigurationManager.AppSettings["FileSize"]);
        size *= 1024; //1 KB size
        string CSVPath = ConfigurationManager.AppSettings["CSVPath"];

        StringBuilder FirstLine = new StringBuilder();
        StringBuilder records = new StringBuilder();

        int num = 0;
        int length = 0;

        IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
        FirstLine.AppendLine(string.Join(",", columnNames));
        records.AppendLine(FirstLine.ToString());

        length += records.ToString().Length;

        foreach (DataRow row in dt.Rows)
        {
            //Putting field values in double quotes
            IEnumerable<string> fields = row.ItemArray.Select(field =>
                string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\""));

            records.AppendLine(string.Join(",", fields));
            length += records.ToString().Length;

            if (length > size)
            {
                //Create a new file
                num++;
                File.WriteAllText(CSVPath + CSVFileName + DateTime.Now.ToString("yyyyMMddHHmmss") + num.ToString("_000") + ".csv", records.ToString());
                records.Clear();
                length = 0;
                records.AppendLine(FirstLine.ToString());
            }

        }            
    }  
4

2 に答える 2

2

UseFile.ReadLinesは、遅延実行Linqが実行されることを意味します。

foreach(var line in File.ReadLines(FilePath))
{
   // logic here.
}

MSDN から

ReadLines メソッドReadAllLinesメソッドの違いは次のとおりです。 ReadLines を使用すると、コレクション全体が返される前に文字列のコレクションの列挙を開始できます。ReadAllLines を使用する場合 、配列にアクセスする前に、文字列の配列全体が返されるまで待機する必要があります。したがって、非常に大きなファイルを扱う場合は、ReadLines の方が効率的です。

これで、以下のようにメソッドを書き直すことができます。

    public static void SplitCSV(string FilePath, string FileName)
    {
        //Read Specified file size
        int size = Int32.Parse(ConfigurationManager.AppSettings["FileSize"]);

        size *= 1024 * 1024;  //1 MB size

        int total = 0;
        int num = 0;
        string FirstLine = null;   // header to new file                  
        var writer = new StreamWriter(GetFileName(FileName, num));

        // Loop through all source lines
        foreach (var line in File.ReadLines(FilePath))
        {
            if (string.IsNullOrEmpty(FirstLine)) FirstLine = line;
            // Length of current line
            int length = line.Length;

            // See if adding this line would exceed the size threshold
            if (total + length >= size)
            {
                // Create a new file
                num++;
                total = 0;
                writer.Dispose();
                writer = new StreamWriter(GetFileName(FileName, num));
                writer.WriteLine(FirstLine);
                length += FirstLine.Length;
            }

            // Write the line to the current file                
            writer.WriteLine(line);

            // Add length of line in bytes to running size
            total += length;

            // Add size of newlines
            total += Environment.NewLine.Length;
        }
   }
于 2016-04-06T08:36:45.703 に答える