how to process the CSV file if it already contain a contains: comma, aposthrope, semi-colonin in 1 of th column?
The end-user will receive 3 x p/week an Excel file. I can't process the excel file in dotnet, some reason. I have to use convert it to CSV file. When the user receives the excel file (s)he needs to do SAVE as and choose CSV file, then the DOTNET application (my custom app) should read this and process it.
The problem is when the CSV already contains comma the applications break. as you below can see the a column starts with XBegin and ends with Xend. between them it may contain : comma, aposthrope, semi-colon etc. so I think when you do SAVE as Microsoft put's them in double quotes..
the question is how to process this code...? I'm stuck , please advice? Below is my piece of code.
private DataSet GetData(byte[] csvcontent)
{
try
{
//for the header (Column HEADING)
string strLine;
string[] strArray;
char[] charArray = new char[] { ',' };
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("TheData");
MemoryStream reader = new MemoryStream(csvcontent);
StreamReader sr = new StreamReader(reader);
//skip the first line it's always empty.
strLine = sr.ReadLine();
//this is the heading, will become column names
strLine = sr.ReadLine();
strArray = strLine.Split(charArray);
// bool firstRow = true;
for (int x = 0; x <= strArray.GetUpperBound(0); x++)
{
switch (x)
{
case 3:
case 10:
case 16:
case 18:
case 20:
dt.Columns.Add(strArray[x].Trim(), typeof(DateTime));
break;
default:
dt.Columns.Add(strArray[x].Trim());
break;
}
}
//PROCESS the RECORDS/DATA itself / ADD ROWS TO TABLE
strLine = sr.ReadLine();
while (strLine != null)
{
strArray = strLine.Split(charArray);
DataRow dr = dt.NewRow();
for (int i = 0; i <= strArray.GetUpperBound(0) - 1; i++)
{
switch (i)
{
case 3:
case 10:
case 16:
case 18:
case 20:
if (!string.IsNullOrEmpty(strArray[i]))
{
LeKey = strArray[i].ToString();
dr[i] = Convert.ToDateTime(strArray[i]);
}
break;
default:
//need this to trace in case of error
if (i == 7)
{
LeKey = strArray[i].ToString();
}
dr[i] = strArray[i].Trim();
break;
}
//dr[i] = strArray[i].Trim();
}
dt.Rows.Add(dr);
strLine = sr.ReadLine();
}
sr.Close();
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
732017,INV09.020500,C1,30/11/2016,"XBegin - COMMITMENT FILE FOR FACILITIES FOR THE ORGANISATION""Footbal, robotics & agenda"" DURING THE DIFFERENCE DAY IN bazar - ILM - 03-05-2016, SI2.st017 Xend ", Test.Unit.z.1,Voodo,PLCDMSA,TIN100,2016
ps: XBegin till XEnd is 1 column....
UPDATE:
-------------
the excel file will be send 3 x p/week to end-user via email, I can't ask the user to manipulate data, s(he) should only do SAVE as and choose CSV file or other text format in EXcel app... and then my app should process this generated file.