Exemplo n.º 1
0
        // not work with large file
        public DataTable LoadExcel(string csvFilePathNam, StdLib.Constanct.Csv.SPLIT split)
        {
            string[] Lines = File.ReadAllLines(csvFilePathNam);
            string[] Fields;
            Fields = Lines[0].Split(new char[] { CsvUtil.GetSplitChar(split) });
            int       Cols = Fields.GetLength(0);
            DataTable dt   = new DataTable();

            //1st row must be column names; force lower case to ensure matching later on.
            for (int i = 0; i < Cols; i++)
            {
                dt.Columns.Add(Fields[i].ToLower(), typeof(string));
            }
            DataRow Row;

            for (int i = 1; i < Lines.GetLength(0); i++)
            {
                Fields = Lines[i].Split(new char[] { CsvUtil.GetSplitChar(split) });
                Row    = dt.NewRow();
                if (Fields.Length >= Cols)
                {
                    for (int f = 0; f < Cols; f++)
                    {
                        Row[f] = Fields[f];
                    }
                    dt.Rows.Add(Row);
                }
            }

            return(dt);
        }
Exemplo n.º 2
0
        public void WriteDataTable2Csv(string dirPath, string csvName, DataTable tb, StdLib.Constanct.Csv.SPLIT split)
        {
            StringBuilder sb = new StringBuilder();

            foreach (DataRow row in tb.Rows)
            {
                IEnumerable <string> fields = row.ItemArray.Select(field =>
                                                                   string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\""));
                sb.AppendLine(string.Join(CsvUtil.GetSplitChar(split).ToString(), fields));
            }

            File.WriteAllText(dirPath + csvName, sb.ToString());
        }