|
-
Nov 17th, 2011, 03:25 AM
#1
Thread Starter
New Member
[Excel] CSV Correct/Delete Rows/Split File
Hey everyone, I am new to the forums and kind of hope to grow and get some insight from everyone here. I am having a little bit of trouble I would really love some some kind of help with something small I have been writing in Visual Studios 2010.
At my job, we deal with a lot of excel files, mainly CSV but others as well. We import these CSV files into our databases at work and we can only import 1000 lines at a time. I have already created something that can split a header line and 999 lines to equal 1000 lines. So now I am having a problem by simply implementing a simple Replace string and delete row. So what I have so far is:
Code:
Sub SplitCSV(ByVal FilePath As String, ByVal LineCount As Integer, ByVal MaxOutputFile As Integer, ByVal Status As UpdateProgressSub, ByRef IsAbort As Boolean)
' Validate first
If LineCount < 100 Then Throw New Exception("Number of lines must be more than 100.")
' Open the csv file for reading
Dim Reader As New IO.StreamReader(FilePath)
' Create the output directory
Dim OutputFolder As String = FilePath & "_Splits"
If Directory.Exists(FilePath) = False Then
Directory.CreateDirectory(OutputFolder)
End If
' Read the csv column's header
Dim strHeader As String = Reader.ReadLine
' Start splitting
Dim FileIndex As Integer
Do
' Update progress
FileIndex += 1
If Not Status Is Nothing Then
Status.Invoke((FileIndex - 1) * LineCount)
End If
' Check if the number of splitted files doesn't exceed the limit
If (MaxOutputFile < FileIndex) And (MaxOutputFile > 0) Then Exit Do
' Create new file to store a piece of the csv file
Dim PiecePath As String = OutputFolder & "\" & Path.GetFileNameWithoutExtension(FilePath) & "_" & FileIndex & Path.GetExtension(FilePath)
Dim Writer As New StreamWriter(PiecePath, False)
Writer.AutoFlush = False
Writer.WriteLine(strHeader)
' Read and writes precise number of rows
For i As Integer = 1 To LineCount
Dim s As String = Reader.ReadLine()
If s <> Nothing And _IsAbort = False Then
Writer.WriteLine(s)
Else
Writer.Flush()
Writer.Close()
Exit Do
End If
Next
' Flush and close the splitted file
Writer.Flush()
Writer.Close()
Loop
Reader.Close()
End Sub
(Some of my code is missing, mostly my visual form data)
What I need is to somehow replace invalid characters within my CSV file for example = , ' " .
What I also need is to somehow delete a entire row if a cell is empty in Column A,B,C
(Then I need that row to shift up, for example: Row 16 has a empty cell in Column B so the row needs to be deleted and Row 17+ that is correct shifts up)
I can already tell this is most likely a simple process and somehow its just blowing through me. I am still a beginner and in school in the learning process. This is also a separate program, not a Macro in Excel itself, basically trying to make this a one click process.
Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|