The problem is not the exporting itself but how it separates the fields with comma’s and I have commas in the fields. All works fine until it gets to the last field where the string contains a comma, it delimits it
My code:
foreach (var item in jobSortedReportData)
{
sb.Append("\"").Append(item.Title).Append("\",\"").Append(item.Total).Append("\",\"").Append(item.Week6).Append("\",\"").Append(item.Week5).Append("\",\"").Append(item.Week4).Append("\",\"").Append(item.Week3).Append("\",\"").Append(item.Week2).Append("\",").Append(item.Week1).Append(Environment.NewLine);
}
byte[] textBytes2 = Encoding.UTF8.GetBytes(sb.ToString());
the output this generates:
Number 1 Job Search 0 **Bathroom Fitter, Plumbing: 100** Carpenter, Joinery: 99 Carpenter, Joinery: 81 Carpenter, Joinery: 209 Electrician, Domestic Electrical: 495 **Bathroom Fitter Plumbing: 112**
Number 2 Job Search 0 Carpenter, Joinery: 97 Bathroom Fitter, Plumbing: 99 Bathroom Fitter, Plumbing: 79 Bathroom Fitter, Plumbing: 150 Bathroom Fitter, Plumbing: 453 Builder Window Installation: 75
Number 3 Job Search 0 Builder, Window Installation: 85 Building Contractor, Kitchen Fitting: 97 Plumber, Bathroom Fitting: 51 Builder, Window Installation: 134 Heating Engineer, Gas Installation: 451 Bathroom Fitter Bathroom Fitting: 74
raw view:
"Number 3 Job Search","0","Builder, Window Installation: 85","Building Contractor, Kitchen Fitting: 97","Plumber, Bathroom Fitting: 51","Builder, Window Installation: 134","Heating Engineer, Gas Installation: 451",Bathroom Fitter, Bathroom Fitting: 74
"Number 1 Tradesmen Search","0","Painter & Decorator: 316","Painter & Decorator: 229","Painter & Decorator: 146","Builder: 324","TV Aerials & In-home Entertainment Installer: 1378",Electrician: 247
"Number 2 Tradesmen Search","0","Builder: 171","Builder: 191","Plumber: 114","Plumber: 295","Builder: 551",Builder: 205
"Number 3 Tradesmen Search","0","Plumber: 123","Plumber: 160","Builder: 108","Painter & Decorator: 231","Domestic Electrical: 495",Painter & Decorator: 160
In the above, "Bathroom Fitter, Plumbing: 100" in the 3rd column contains a , but it keeps the whole string in one excel field. When it comes to the last column "Bathroom Fitter Plumbing: 112" there is a , in the string between Fitter and Plumbing, so what happens now is this , is seen as a separator so when in excel it split this in TWO cells, thus I have Bathroom Fitter in one cell and Plumbing:112 in the next. The whole string should be in ONE cell. It does this only on the last column?? please help me correct this.
Thanks
Its hard to see with out the raw csv but rememeber that you need to use text qualifiers if a CSV field contains commas to prevent it thinking it is a column. So the raw CSV should look like this to get consistent results:
“Bathroom Fitter, Plumbing: 100”,next column, “column with comma”.
EDIT: Now I have seen the raw shouldn’t the code look like this at the end? I don’t think your adding the text qualifiers on the last field: