Ejemplo n.º 1
0
        public DataTable FromCsv(string strFilePath)
        {
            fileDelimiter = GetDelimiter(strFilePath);
            DataSet oDS = new DataSet();

            oDS.Tables.Add("Property");
            var oTable = oDS.Tables[0];

            using (var stream = new System.IO.FileStream(strFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                var    oSR = UtilFunctions.CreateStreamReader(stream);
                string line;
                int    cellCount   = 0;
                var    stringSplit = new StringSplit(fileDelimiter);
                var    lineValues  = new List <string>(10);

                while ((line = oSR.ReadLine()) != null)
                {
                    stringSplit.Split(line, lineValues);

                    bool skipRow = false;
                    if (!hasHeaderColumnGenerated)
                    {
                        oTable.Columns.AddRange(GenerateColumnHeader(lineValues, out skipRow));
                    }

                    if (!skipRow)
                    {
                        int intCounter = 0;
                        var oRows      = oTable.NewRow();
                        var colCount   = Math.Min(oTable.Columns.Count, 20);
                        foreach (string str in lineValues)
                        {
                            if (intCounter < colCount)
                            {
                                oRows[intCounter] = str;
                                intCounter++;
                            }
                        }

                        if (cellCount < intCounter)
                        {
                            cellCount = intCounter;
                        }
                        oTable.Rows.Add(oRows);
                    }
                }

                //Removing if Extra Columns were created
                int colIndex = 20;
                while (cellCount < oTable.Columns.Count)
                {
                    oTable.Columns.RemoveAt(--colIndex);
                }
            }
            return(oTable);
        }
        public static DataTable FromCsv(string strFilePath)
        {
            try
            {
                fileDelimiter = GetDelimiter(strFilePath);
                DataSet oDS = new DataSet();
                oDS.Tables.Add("Property");
                var oTable = oDS.Tables[0];
                hasHeaderColumnGenerated = false;
                using (var stream = new System.IO.FileStream(strFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    var    oSR = UtilFunctions.CreateStreamReader(stream);
                    string line;
                    int    cellCount   = 0;
                    var    stringSplit = new StringSplit(fileDelimiter);
                    var    lineValues  = new List <string>(10);

                    while ((line = oSR.ReadLine()) != null)
                    {
                        stringSplit.Split(line, lineValues);

                        bool skipRow = false;
                        if (!hasHeaderColumnGenerated)
                        {
                            oTable.Columns.AddRange(GenerateColumnHeader(lineValues, out skipRow));
                        }

                        if (!skipRow)
                        {
                            int intCounter = 0;
                            var oRows      = oTable.NewRow();
                            var colCount   = oTable.Columns.Count;
                            foreach (string str in lineValues)
                            {
                                if (intCounter < colCount)
                                {
                                    oRows[intCounter] = str;
                                    intCounter++;
                                }
                            }

                            if (cellCount < intCounter)
                            {
                                cellCount = intCounter;
                            }
                            oTable.Rows.Add(oRows);
                        }
                    }

                    //Removing Columns not required
                    int           colIndex       = 0;
                    List <string> coltoBeRemoved = new List <string>();;
                    while (colIndex < oTable.Columns.Count)
                    {
                        var col = oTable.Columns[colIndex].ColumnName;
                        if (col != "Action" && col != "Item" &&
                            col != "EAN" && col != "AlternativeItem" &&
                            col != "Name" && col != "SupplierItemId" &&
                            col != "Supplier" && col != "Unit" &&
                            col != "SalesPrice" && col != "SalesPrice" &&
                            col != "ItemGroup" && col != "DiscountGroup" &&
                            col != "WebArg")
                        {
                            coltoBeRemoved.Add(oTable.Columns[colIndex].ColumnName);
                        }
                        colIndex++;
                    }
                    foreach (var col in coltoBeRemoved)
                    {
                        oTable.Columns.Remove(col);
                    }
                }
                return(oTable);
            }
            catch
            {
                return(null);
            }
        }