简体   繁体   English

如果已经包含以下内容,如何在C#中处理CSV文件:逗号,撇号,分号

[英]how to process a CSV file in C# if it already contains: comma, aposthrope, semi-colon

how to process the CSV file if it already contain a contains: comma, aposthrope, semi-colonin in 1 of th column? 如果CSV文件已经包含一个包含内容:第1列中的逗号,撇号,半冒号,该如何处理?

The end-user will receive 3 xp/week an Excel file. 最终用户每周将收到3 xp的Excel文件。 I can't process the excel file in dotnet, some reason. 由于某些原因,我无法在dotnet中处理excel文件。 I have to use convert it to CSV file. 我必须使用将其转换为CSV文件。 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. 当用户接收到excel文件时,他需要另存为并选择CSV文件,然后DOTNET应用程序(我的自定义应用程序)应阅读并进行处理。

The problem is when the CSV already contains comma the applications break. 问题是当CSV已经包含逗号时,应用程序中断。 as you below can see the a column starts with XBegin and ends with Xend. 如下所示,您会看到一列以XBegin开头,以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.. 它们之间可能包含:逗号,撇号,分号等。因此,我认为当您执行SAVE时(如Microsoft将其放在双引号中)。

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 732017,INV09.020500,C1,30 / 11/2016,“ XBegin-组织机构的承诺文件”“ Footbal ,机器人技术和议程”“在巴扎尔差异日期间-ILM-2016年3月5日,SI2。 st017 Xend “,Test.Unit.z.1,Voodo,PLCDMSA,TIN100,2016

ps: XBegin till XEnd is 1 column.... ps:XBegin直到XEnd为1列...

UPDATE: 更新:
------------- -------------
the excel file will be send 3 xp/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. excel文件将通过电子邮件每周3 xp发送给最终用户,我不能要求用户操纵数据,他(他)只应另存为,并在EXcel应用程序中选择CSV文件或其他文本格式。 。,然后我的应用应处理此生成的文件。

Don't roll your own CSV generator or parser. 不要滚动自己的CSV生成器或解析器。 As you've found out, the format is not as easy as it sounds. 正如您所发现的,格式并不像听起来那样简单。 Use something like CsvHelper . 使用类似CsvHelper的东西。

As for your specific problem, the usual answer is to put your values in quotes. 对于您的特定问题,通常的答案是将您的值括在引号中。 So instead of a single cell being abc,def , it should be "abc,def" . 因此,应该是"abc,def"而不是单个单元格为abc,def Your code still can't handle this situation because you're spliting your string using a simple string.Split() . 您的代码仍然string.Split()这种情况,因为您正在使用简单的string.Split()字符串。 You could do something that enumerates through each character of the row and creates a new value only if you're not already inside a set of quotation marks, but you'd be reinventing the wheel. 只有在行引号中还没有包含引号的情况下,您才可以进行列举该行的每个字符并创建一个新值的操作,而您将需要重新设计轮子。 Use a software library for this. 为此使用软件库。

I think I fixed the problem by using the 我想我通过使用
1) adding reference to : Microsoft.VisualBasic 1)添加对:Microsoft.VisualBasic的引用
then next to lines 然后在行旁边
csvReader.SetDelimiters(new string[] { "," }); csvReader.SetDelimiters(new string [] {“,”});
csvReader.HasFieldsEnclosedInQuotes = true; csvReader.HasFieldsEnclosedInQuotes = true;

I can't void because of low points: @ MichaelMao point me the link thanks 我不能因为低分而无效:@ MichaelMao指向我的链接谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM