Exemple #1
0
        public String ReadValue(out CtkCsvEnumReadType rt)
        {
            var rs = "";

            rt = ReadValue(out rs);
            return(rs);
        }
Exemple #2
0
        public CtkCsvEnumReadType ReadValue(out string value)
        {
            var c  = 0;
            var sb = new StringBuilder();

            while ((c = this.Read()) >= 0)
            {
                if (c == '"')
                {
                    while ((c = this.Read()) >= 0)
                    {
                        if (c == '"')
                        {
                            if ((c = this.Read()) == '"')
                            {
                                sb.Append((char)c);
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            sb.Append((char)c);
                        }
                    }
                }

                if (c == ',')
                {
                    value = sb.ToString();
                    return(this.LastReadType = CtkCsvEnumReadType.Cell);
                }
                else if (c == '\r')
                {
                }
                else if (c == '\n')
                {
                    value = sb.ToString();
                    return(this.LastReadType = CtkCsvEnumReadType.RowEnd);
                }
                else
                {
                    sb.Append((char)c);
                }
            }

            value = null;

            if (sb.Length == 0)
            {
                return(this.LastReadType = CtkCsvEnumReadType.NoData);
            }

            value = sb.ToString();
            return(this.LastReadType = CtkCsvEnumReadType.RowEnd);
        }
Exemple #3
0
        public CtkCsvEnumReadType MoveToRowEnd()
        {
            var val = "";
            CtkCsvEnumReadType rt = CtkCsvEnumReadType.Cell;

            while ((rt = ReadValue(out val)) == CtkCsvEnumReadType.Cell)
            {
                ;
            }
            return(rt);
        }
Exemple #4
0
        public static DataTable ReadCsvFile(String file, bool hasHeader = false)
        {
            var rs = new DataTable();

            using (var csv = new CtkCsvReader(file))
            {
                string             val      = null;
                CtkCsvEnumReadType readtype = CtkCsvEnumReadType.Cell;
                DataRow            row      = null;
                int idx = 0;

                if (hasHeader)
                {
                    while (readtype == CtkCsvEnumReadType.Cell)
                    {
                        readtype = csv.ReadValue(out val);
                        rs.Columns.Add(val, typeof(string));
                    }
                }
                else
                {
                    while (readtype != CtkCsvEnumReadType.RowEnd)
                    {
                        if (readtype == CtkCsvEnumReadType.RowEnd || row == null)
                        {
                            row = rs.NewRow();
                            idx = 0;
                        }

                        readtype = csv.ReadValue(out val);

                        //一次性的加入Header
                        //不在後面用if是減少效能消耗
                        rs.Columns.Add("Col_" + idx, typeof(string));

                        row[idx] = val;
                        idx++;
                    }
                }

                while (readtype != CtkCsvEnumReadType.NoData)
                {
                    if (readtype == CtkCsvEnumReadType.RowEnd || row == null)
                    {
                        if (row != null)
                        {
                            rs.Rows.Add(row);
                        }
                        row = rs.NewRow();
                        idx = 0;
                    }

                    readtype = csv.ReadValue(out val);

                    row[idx] = val;
                    idx++;
                }
            }


            return(rs);
        }