填充CSV文件中的数据集

时间:2013-05-17 10:24:53

标签: c# asp.net .net csv dataset

我想阅读CSV文件的内容并创建数据集。 我这样想:

var lines = File.ReadAllLines("test.csv").Select(a => a.Split(';'));
DataSet ds = new DataSet();
ds.load(lines);

但显然这是不正确的。

6 个答案:

答案 0 :(得分:20)

您需要添加引用 Microsoft.VisualBasic.dll 才能使用TextFieldParser类。

 private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
        {
            DataTable csvData = new DataTable();
            try
            {
              using(TextFieldParser csvReader = new TextFieldParser(csv_file_path))
                 {
                    csvReader.SetDelimiters(new string[] { "," });
                    csvReader.HasFieldsEnclosedInQuotes = true;
                    string[] colFields = csvReader.ReadFields();
                    foreach (string column in colFields)
                    {
                        DataColumn datecolumn = new DataColumn(column);
                        datecolumn.AllowDBNull = true;
                        csvData.Columns.Add(datecolumn);
                    }
                    while (!csvReader.EndOfData)
                    {
                        string[] fieldData = csvReader.ReadFields();
                        //Making empty value as null
                        for (int i = 0; i < fieldData.Length; i++)
                        {
                            if (fieldData[i] == "")
                            {
                                fieldData[i] = null;
                            }
                        }
                        csvData.Rows.Add(fieldData);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return csvData;
        }
      }

有关详细信息,请参阅此文章:http://www.morgantechspace.com/2013/08/how-to-read-data-from-csv-file-in-c.html

答案 1 :(得分:15)

您需要针对CSV文件运行SELECT语句以填充数据集:

编辑:这是http://carllbrown.blogspot.co.uk/2007/09/populate-dataset-from-csv-delimited_18.html

的一些示例代码
string FileName = ...
OleDbConnection conn = new OleDbConnection
       ("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + 
         Path.GetDirectoryName(FileName) + 
         "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");

conn.Open();

OleDbDataAdapter adapter = new OleDbDataAdapter
       ("SELECT * FROM " + Path.GetFileName(FileName), conn);

DataSet ds = new DataSet("Temp");
adapter.Fill(ds);

conn.Close();

答案 2 :(得分:4)

您可以使用像Fast CSV Reader那样的库

using System.IO;
using LumenWorks.Framework.IO.Csv;
void ReadCsv()
{
    // open the file "data.csv" which is a CSV file with headers
    using (CsvReader csv = new CsvReader(
                           new StreamReader("data.csv"), true))
    {
        myDataRepeater.DataSource = csv;
        myDataRepeater.DataBind();
    }
}

答案 3 :(得分:1)

此代码中解决的逗号(,)问题

即使您在单元格之间添加逗号(,)

也可以

阅读CSV文件代码:

public MainWindow()
                {
                    InitializeComponent();

                    DataTable dtDataSource = new DataTable();

                    string[] fileContent = File.ReadAllLines(@"..\\Book1.csv");

                    if (fileContent.Count() > 0)
                    {
                        //Create data table columns dynamically
                        string[] columns = fileContent[0].Split(',');

                        for (int i = 0; i < columns.Count(); i++)
                        {
                            dtDataSource.Columns.Add(columns[i]);
                        }

                        //Add row data dynamically
                        for (int i = 1; i < fileContent.Count(); i++)
                        {
                            string[] rowData = fileContent[i].Split(',');
                            string[] realRowData = new string[columns.Count()];
                            StringBuilder collaboration = new StringBuilder();
                            int v = 0;

                            //this region solves the problem of a cell containing ",".
                            #region CommaSepProblem
                            for (int j = 0, K = 0; j < rowData.Count(); j++, K++)
                            {
                                if ((rowData[j].Count(x => x == '"') % 2 == 0))//checks if the string contains even number of DoubleQuotes
                                {
                                    realRowData[K] = quotesLogic((rowData[j]));

                                }
                                else if ((rowData[j].Count(x => x == '"') % 2 != 0))//If Number of DoubleQuotes  are ODD
                                {
                                    int c = rowData[j].Count(x => x == '"');
                                    v = j;

                                    while (c % 2 != 0)//Go through all the next array cell till it makes EVEN Number of DoubleQuotes.
                                    {
                                        collaboration.Append(rowData[j] + ",");
                                        j++;
                                        c += rowData[j].Count(x => x == '"');

                                    }

                                    collaboration.Append(rowData[j]);
                                    realRowData[K] = quotesLogic(collaboration.ToString());
                                }
                                else { continue; }
                            }
                            #endregion
                            dtDataSource.Rows.Add(realRowData);
                        }
                        if (dtDataSource != null)
                        {
                            //dataGridView1 = new DataGridView();
                            dataGrid1.ItemsSource = dtDataSource.DefaultView;
                        }
                    }
                }

方法需要添加:

 string quotesLogic(string collaboration)
    {
        StringBuilder after = new StringBuilder(collaboration);

        if (after.ToString().StartsWith("\"") && after.ToString().EndsWith("\""))//removes 1st and last quotes as those are system generated
        {
            after.Remove(0, 1);
            after.Remove(after.Length - 1, 1);
            int count = after.Length - 1;
            //FACT: if you try to add DoubleQuote in a cell in excel. It'll save that quote as 2 times DoubleQuote(Like "")  which means first DoubleQuote is to give instruction to CPU that the next DoubleQuote  is not system generated.
            while (count > 0)//This loop find twice insertion of 2 DoubleQuotes and neutralise them to One DoubleQuote. 
            {
                if (after[count] == '"' && after[count - 1] == '"')
                {
                    after.Remove(count, 1);
                }
                count--;
            }
        }

        return after.ToString();
    }

答案 4 :(得分:0)

如果您只是想快速创建一个填充了CSV文件中的样本数据的DataTable(或直接从Excel粘贴)来玩游戏或原型,那么您可以使用我的Shan Carter先生的数据转换器 - 我最近添加了将逗号和制表符分隔的数据输出到C#DataTable的功能。

http://thdoan.github.io/mr-data-converter/

答案 5 :(得分:0)

下面我写了五个方法,将Csv文件转换为DataTable。

它们的设计考虑了可选的引号(例如“符号”),并且在不使用其他库的情况下尽可能多样化:

    public static DataTable GetDataTabletFromCSVFile(string filePath, bool isHeadings)
    {
        DataTable MethodResult = null;
        try
        {
            using (TextFieldParser TextFieldParser = new TextFieldParser(filePath))
            {
                if (isHeadings)
                {
                    MethodResult = GetDataTableFromTextFieldParser(TextFieldParser);

                }
                else
                {
                    MethodResult = GetDataTableFromTextFieldParserNoHeadings(TextFieldParser);

                }

            }

        }
        catch (Exception ex)
        {
            ex.HandleException();
        }
        return MethodResult;
    }

    public static DataTable GetDataTableFromCsvString(string csvBody, bool isHeadings)
    {
        DataTable MethodResult = null;
        try
        {
            MemoryStream MemoryStream = new MemoryStream();


            StreamWriter StreamWriter = new StreamWriter(MemoryStream);

            StreamWriter.Write(csvBody);

            StreamWriter.Flush();


            MemoryStream.Position = 0;


            using (TextFieldParser TextFieldParser = new TextFieldParser(MemoryStream))
            {
                if (isHeadings)
                {
                    MethodResult = GetDataTableFromTextFieldParser(TextFieldParser);

                }
                else
                {
                    MethodResult = GetDataTableFromTextFieldParserNoHeadings(TextFieldParser);

                }

            }

        }
        catch (Exception ex)
        {
            ex.HandleException();
        }
        return MethodResult;
    }

    public static DataTable GetDataTableFromRemoteCsv(string url, bool isHeadings)
    {
        DataTable MethodResult = null;
        try
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            StreamReader StreamReader = new StreamReader(httpWebResponse.GetResponseStream());

            using (TextFieldParser TextFieldParser = new TextFieldParser(StreamReader))
            {
                if (isHeadings)
                {
                    MethodResult = GetDataTableFromTextFieldParser(TextFieldParser);

                }
                else
                {
                    MethodResult = GetDataTableFromTextFieldParserNoHeadings(TextFieldParser);

                }

            }

        }
        catch (Exception ex)
        {
            ex.HandleException();
        }
        return MethodResult;
    }


    private static DataTable GetDataTableFromTextFieldParser(TextFieldParser textFieldParser)
    {
        DataTable MethodResult = null;
        try
        {
            textFieldParser.SetDelimiters(new string[] { "," });

            textFieldParser.HasFieldsEnclosedInQuotes = true;


            string[] ColumnFields = textFieldParser.ReadFields();

            DataTable dt = new DataTable();

            foreach (string ColumnField in ColumnFields)
            {
                DataColumn DataColumn = new DataColumn(ColumnField);

                DataColumn.AllowDBNull = true;

                dt.Columns.Add(DataColumn);

            }


            while (!textFieldParser.EndOfData)
            {
                string[] Fields = textFieldParser.ReadFields();


                for (int i = 0; i < Fields.Length; i++)
                {
                    if (Fields[i] == "")
                    {
                        Fields[i] = null;

                    }

                }

                dt.Rows.Add(Fields);

            }

            MethodResult = dt;

        }
        catch (Exception ex)
        {
            ex.HandleException();
        }
        return MethodResult;
    }

    private static DataTable GetDataTableFromTextFieldParserNoHeadings(TextFieldParser textFieldParser)
    {
        DataTable MethodResult = null;
        try
        {
            textFieldParser.SetDelimiters(new string[] { "," });

            textFieldParser.HasFieldsEnclosedInQuotes = true;

            bool FirstPass = true;

            DataTable dt = new DataTable();

            while (!textFieldParser.EndOfData)
            {
                string[] Fields = textFieldParser.ReadFields();

                if(FirstPass)
                {
                    for (int i = 0; i < Fields.Length; i++)
                    {
                        DataColumn DataColumn = new DataColumn("Column " + i);

                        DataColumn.AllowDBNull = true;

                        dt.Columns.Add(DataColumn);

                    }

                    FirstPass = false;

                }

                for (int i = 0; i < Fields.Length; i++)
                {
                    if (Fields[i] == "")
                    {
                        Fields[i] = null;

                    }

                }

                dt.Rows.Add(Fields);

            }

            MethodResult = dt;

        }
        catch (Exception ex)
        {
            ex.HandleException();
        }
        return MethodResult;
    }

如果像我一样,您正在保存报告服务,那么您应该像这样使用它:

    Warning[] warnings;
    string[] streamids;
    string mimeType;
    string encoding;
    string filenameExtension;

    byte[] bytes = rvMain.ServerReport.Render("csv", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);

    string CsvBody = System.Text.Encoding.UTF8.GetString(bytes);

    DataTable dt = GetDataTableFromCsvString(CsvBody,true);

否则,您只需要:

    bool IsHeadings = true; //Does the data include a heading row?

    DataTable dt = GetDataTableFromCsvString(CsvBody, IsHeadings);

或直接从csv文件中使用

    bool IsHeadings = true; //Does the data include a heading row?

    DataTable dt = GetDataTabletFromCsvFile(FilePath, IsHeadings)

或者使用远程存储的csv文件

    bool IsHeadings = true; //Does the data include a heading row?

    DataTable dt = GetDataTabletFromRemoteCsv(Url, IsHeadings)

数据集是DataTables的集合,因此请创建一个如下:

    DataSet ds = new DataSet();

    ds.Tables.Add(dt);