Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,

I would like to know how to be able to remove blank rows in a CSV file automatically in an app written in C#. My application currently is able to read a spreadsheet and convert this to the CSV file.

Any info will be gratefully appreciated.

Regards,

Glen.
Posted
Comments
[no name] 25-Sep-13 9:18am    
Don't write the blank lines to begin with. Or you would have to read the file remove the blank lines and then re-write the file.
Sergey Alexandrovich Kryukov 25-Sep-13 13:00pm    
Exactly. It's most likely that OP had written them in first place.
—SA

1 solution

That's more complex that it sounds: a "blank line" could be this:
1,2,3

4,5,6
Which is trivial to remove:
C#
File.WriteAllLines(fileName,File.ReadAllLines(fileName).Where(l => !string.IsNullOrWhiteSpace(l)));
Or it could refer to this:
1,2,3
,,,
4,5,6
Which is a lot harder - it requires "proper" parsing, because it could be
"1","2","3"
"","",""
"4","5","6"
In this case, you really want to look at the structures that you are reading this into, and remove empty instances of that directly before writing the CSV file.

And we can't help you with that: we have no idea how your software works!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900