Пример #1
0
    public static CSVFile Convert(CSVFile inCsv)
    {
        CSVFile outCsv = new CSVFile {
            new CSVRow()
        };

        // Insert headline of outfile
        foreach (KeyValuePair <string, string> kvp in clConfig)
        {
            outCsv[0].Add(kvp.Key);
        }

        // Dictionary<Spaltenindex Eingabedatei, Spaltenname Ausgabedatei>
        Dictionary <int, String> clPhoneColumnMap = new Dictionary <int, String>();

        for (int i = 0; i < inCsv[0].Count; ++i)
        {
            foreach (KeyValuePair <Regex, string> kvp in clPhoneNumberMatching)
            {
                if (kvp.Key.IsMatch(inCsv[0][i]))
                {
                    clPhoneColumnMap.Add(i, kvp.Value);
                }
            }
        }

        for (int r = 1; r < inCsv.Count; ++r)
        {
            outCsv.Add(new CSVRow());

            foreach (KeyValuePair <string, string> kvp in clConfig)
            {
                // leave column empty || fill it with function
                if (kvp.Value == String.Empty)
                {
                    if (kvp.Key == "Displayname")
                    {
                        outCsv[r].Add(inCsv.GetField(r, "Vorname") + " " + inCsv.GetField(r, "Nachname"));
                    }
                    else
                    {
                        outCsv[r].Add("");
                    }
                }
                // Simple copy of column
                else
                {
                    outCsv[r].Add(inCsv.GetField(r, kvp.Value));
                }
            }

            // Phone-Number-Import
            int    iPhoneNumber   = 1;
            int    maxPhoneNumber = 4;
            string phoneNumber;

            foreach (KeyValuePair <int, string> kvp in clPhoneColumnMap)
            {
                if ((phoneNumber = inCsv[r][kvp.Key]) == String.Empty)
                {
                    continue;
                }

                outCsv.SetField(r, "" + iPhoneNumber + ". Typ Telefonnummer", kvp.Value);
                outCsv.SetField(r, "" + iPhoneNumber + ". Telefonnummer", Regex.Replace(phoneNumber, "\\D", ""));

                if (++iPhoneNumber > maxPhoneNumber)
                {
                    break;
                }
            }
        }

        return(outCsv);
    }