Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1.   public void HowFileSave(string path,bool append)
  2.         {
  3.             if (!path.ToLower().EndsWith(".csv"))
  4.             {
  5.                 path += ".csv";
  6.             }
  7.  
  8.             var correctPath = Directory.GetParent(path).ToString();
  9.  
  10.             Directory.CreateDirectory(correctPath);
  11.  
  12.             FileSave(path, append);
  13.         }
  14.  
  15.         private void FileSave(string path, bool appendFile)
  16.         {
  17.             var files = SeparateCsvToFiles(Rows, path);
  18.  
  19.             foreach (var file in files)
  20.             {
  21.                 try
  22.                 {
  23.                     using (StreamWriter writer = new StreamWriter(file.Key, appendFile, Encoding.UTF8))
  24.                     {
  25.                         foreach (var row in file.Value)
  26.                         {
  27.                             writer.WriteLine(BuildCsvRow(row));
  28.                         }
  29.                     }
  30.                 }
  31.                 catch (Exception) { }
  32.                
  33.             }
  34.         }
  35.  
  36.         private List<KeyValuePair<string, List<List<string>>>> SeparateCsvToFiles(List<List<string>> rows, string originalPath)
  37.         {
  38.             List<List<string>> rowsCopy = new List<List<string>>(rows);
  39.  
  40.             var files = new List<KeyValuePair<string, List<List<string>>>>();
  41.  
  42.             int counter = 1;
  43.             Func<string> newpath = () => { return originalPath.Replace(".csv", counter++ + ".csv"); };
  44.  
  45.             if (rowsCopy.Count < MaxRowsInOneFile)
  46.             {
  47.                 files.Add(new KeyValuePair<string, List<List<string>>>(originalPath, rowsCopy));
  48.             }
  49.             else
  50.             {
  51.                 List<List<string>> fileRows;
  52.  
  53.                 while (rowsCopy.Count > MaxRowsInOneFile)
  54.                 {
  55.                     fileRows = rowsCopy.GetRange(0, MaxRowsInOneFile);
  56.                     rowsCopy.RemoveRange(0, MaxRowsInOneFile);
  57.  
  58.                     files.Add(new KeyValuePair<string, List<List<string>>>(newpath(), fileRows));
  59.                 }
  60.  
  61.                 fileRows = rowsCopy.GetRange(0, rowsCopy.Count());
  62.  
  63.                 files.Add(new KeyValuePair<string, List<List<string>>>(newpath(), fileRows));
  64.             }
  65.  
  66.             return files;
  67.         }
  68.  
  69.         private string BuildCsvRow(List<string> rowCells)
  70.         {
  71.             StringBuilder builder = new StringBuilder();
  72.  
  73.             bool firstColumn = true;
  74.  
  75.             foreach (string value in rowCells)
  76.             {
  77.                 if (value != null)
  78.                 {
  79.                     if (!firstColumn)
  80.                         builder.Append(SeparatorChar);
  81.                     if (value.IndexOfAny(new char[] { '"', SeparatorChar }) != -1)
  82.                     {
  83.                         builder.AppendFormat("\"{0}\"", value.Replace("\"", "\"\""));
  84.                     }
  85.                     else
  86.                     {
  87.                         builder.Append(value);
  88.                     }
  89.  
  90.                     firstColumn = false;
  91.                 }
  92.             }
  93.             return builder.ToString();
  94.         }
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement