Basically this example will split huge .csv file into several file. So that you can open the csv file faster rather than open the huge one.
The Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.VisualBasic; using System.Collections; using System.Diagnostics; using System.IO; using System.Threading; namespace CSV_Splitter { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate void UpdateProgressSub(int CurrentLine); private bool _IsAbort; public void SplitCSV(string FilePath,int lineStart, int lineEnd, int LineCount, int MaxOutputFile, UpdateProgressSub Status, ref bool IsAbort) { // Validate first if (LineCount < 100) throw new Exception("Number of lines must be more than 100."); // Open the csv file for reading System.IO.StreamReader Reader = new System.IO.StreamReader(FilePath); // Create the output directory string OutputFolder = FilePath + "_Pieces"; if (Directory.Exists(FilePath) == false) { Directory.CreateDirectory(OutputFolder); } // Read the csv column's header string strHeader = Reader.ReadLine(); // Start splitting int FileIndex = 0; do { // Update progress FileIndex += 1; if ((Status != null)) { Status.Invoke((FileIndex - 1) * LineCount); } // Check if the number of splitted files doesn't exceed the limit if ((MaxOutputFile < FileIndex) && (MaxOutputFile > 0)) break; // TODO: might not be correct. Was : Exit Do // Create new file to store a piece of the csv file string PiecePath = OutputFolder + "\\" + Path.GetFileNameWithoutExtension(FilePath) + "_" + FileIndex + Path.GetExtension(FilePath); StreamWriter Writer = new StreamWriter(PiecePath, false); Writer.AutoFlush = false; Writer.WriteLine(strHeader); // Read and writes precise number of rows for (int i = 1; i <= LineCount; i++) { string s = Reader.ReadLine(); if (s != null & _IsAbort == false) { Writer.WriteLine(s); } else { Writer.Flush(); Writer.Close(); break; // TODO: might not be correct. Was : Exit Do } } // Flush and close the splitted file Writer.Flush(); Writer.Close(); } while (true); Reader.Close(); } public void UpdateProgress(int CurrentLine) { lblStatus.Text = "Aprox. " + CurrentLine.ToString() + " lines splitted"; } private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { _IsAbort = true; } private void saveFileDialog1_FileOk(object sender, CancelEventArgs e) { } private void btnSplit_Click_1(object sender, EventArgs e) { System.Threading.Thread th = new System.Threading.Thread(SplitIt); th.Start(); } private void btnBrowse_Click_1(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; } } private void btnAbort_Click_1(object sender, EventArgs e) { _IsAbort = true; } public void SplitIt() { btnSplit.Enabled = false; btnAbort.Enabled = true; try { SplitCSV(textBox1.Text,Convert.ToInt32(LinesFrom.Value), Convert.ToInt32(EndLines.Value), Convert.ToInt32(numLinesCount.Value), Convert.ToInt32(numMaxFiles.Value), UpdateProgress, ref _IsAbort); if (!_IsAbort) { Interaction.MsgBox("Completed Successfully!", MsgBoxStyle.Information,""); } else { _IsAbort = false; Interaction.MsgBox("Spliting proccess was aborted by user.", MsgBoxStyle.Critical,""); } } catch (Exception ex) { Interaction.MsgBox("Unable to split the CSV File. Reason: " + ex.Message, MsgBoxStyle.Critical,""); } finally { btnSplit.Enabled = true; btnAbort.Enabled = false; } } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Environment.Exit(0); } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { } } }
By Mohd Zulkamal NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)
0 comments:
Post a Comment