John Porter
asked on
Remove the first (Empty) column from a CSV file using C#
Hello Experts,
I am receiving a CSV file from a third party. The file aways comes with the first Column of cells with no data. This is due to a comment at location A1 on the spreadsheet. Data starts at B2.
I would like to remove this column so that the data in the files starts at A2 instead of B2 using C#
Does anyone know how to do this??
Thanks!
I am receiving a CSV file from a third party. The file aways comes with the first Column of cells with no data. This is due to a comment at location A1 on the spreadsheet. Data starts at B2.
I would like to remove this column so that the data in the files starts at A2 instead of B2 using C#
Does anyone know how to do this??
Thanks!
ASKER
Hi Jamie,
I tried your code but I can't seem to get it to work...
I moved the line declaration above the begining of the do loop due to scope:
using(TextWriter writer = new StreamWriter("yourfile.csv .tmp"))
{
***
string line = reader.ReadLine();
***
do {
if (line != null)
writer.WriteLine(line.Subs tring(1));
} while (line != null);
}
When I run this code, the temp file grows out of hand. It looks like. The blank column is removed but it looks like an entire row of cells is saved back into each cell and repeated many times. The temp file does not stop growing until I break the process.
Any ideas what may be going on here?
Thanks!
I tried your code but I can't seem to get it to work...
I moved the line declaration above the begining of the do loop due to scope:
using(TextWriter writer = new StreamWriter("yourfile.csv
{
***
string line = reader.ReadLine();
***
do {
if (line != null)
writer.WriteLine(line.Subs
} while (line != null);
}
When I run this code, the temp file grows out of hand. It looks like. The blank column is removed but it looks like an entire row of cells is saved back into each cell and repeated many times. The temp file does not stop growing until I break the process.
Any ideas what may be going on here?
Thanks!
sorry, I am writing from my memory, it should be:
string line;
using(TextReader reader = new StreamReader("yourfile.csv "))
{
using(TextWriter writer = new StreamWriter("yourfile.csv .tmp"))
{
do {
line = reader.ReadLine();
if (line != null)
writer.WriteLine(line.Subs tring(1));
} while (line != null);
}
}
File.Delete("yourfile.csv" );
File.Move('yourfile.csv.tm p", "yourfile.csv");
string line;
using(TextReader reader = new StreamReader("yourfile.csv
{
using(TextWriter writer = new StreamWriter("yourfile.csv
{
do {
line = reader.ReadLine();
if (line != null)
writer.WriteLine(line.Subs
} while (line != null);
}
}
File.Delete("yourfile.csv"
File.Move('yourfile.csv.tm
ASKER
Thanks Jaime - ths is pretty close to what I am after. I want to delete the entire column even if there is text in one of the cells (sorry if I was not clear on that)
See attached Excel CSV example screenshots:
Sheet1 is how it looks after Vendor sends it to me
Sheet2 is how I want it to look
Sheet3 is how it looks after running your code.
I would like to remove the text "Note from Vendor here" as well as effectively moving all the data to the left one row. This would be the same as highlighting the entire A column (on Sheet1) and selecting delete from the context menu when in Excell spreadsheet view.
Any Ideas?
Thanks!
RemoveColumnFromCSV.doc
See attached Excel CSV example screenshots:
Sheet1 is how it looks after Vendor sends it to me
Sheet2 is how I want it to look
Sheet3 is how it looks after running your code.
I would like to remove the text "Note from Vendor here" as well as effectively moving all the data to the left one row. This would be the same as highlighting the entire A column (on Sheet1) and selecting delete from the context menu when in Excell spreadsheet view.
Any Ideas?
Thanks!
RemoveColumnFromCSV.doc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Jaime!
Open in new window