3
using System;
namespace jagged_array
{
class Program
{
static void Main(string[] args)
{
string[][] Members = new string[10][]{
new string[]{"amit","amit@gmail.com",         "9999999999"},
new string[]{"chandu","chandu@gmail.com","8888888888"},
new string[]{"naveen","naveen@gmail.com", "7777777777"},
new string[]{"ramu","ramu@gmail.com",       "6666666666"},
new string[]{"durga","durga@gmail.com",     "5555555555"},
new string[]{"sagar","sagar@gmail.com",      "4444444444"},
new string[]{"yadav","yadav@gmail.com",    "3333333333"},
new string[]{"suraj","suraj@gmail.com",        "2222222222"},
new string[]{"niharika","niharika@gmail.com","11111111111"},
new string[]{"anusha","anusha@gmail.com",  "0000000000"},
};

for (int i =0; i < Members.Length; i++)
{
System.Console.Write("Name List ({0}):", i + 1);
for (int j = 0; j < Members[i].Length; j++)
{
System.Console.Write(Members[i][j] + "\t");
}
System.Console.WriteLine();
}``
Console.ReadKey();
}
}
}

以上是我的 C# 控制台程序的代码,在该程序中我使用了锯齿状数组并手动分配了值,但现在我的要求是“无需手动分配到数组中,我希望将相同的详细信息从 csv 文件导入到我的程序中(位于我光盘中的某个位置)。那么如何做到这一点我应该使用哪些功能,请帮我举个例子。谢谢你。

4

3 回答 3

2
static void Main()
        {
            string csv_file_path=@"C:\Users\Administrator\Desktop\test.csv";

            DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);

            Console.WriteLine("Rows count:" + csvData.Rows.Count);

            Console.ReadLine();
        }


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;
        }
于 2013-08-28T04:04:26.007 回答
1

将 CSV 文件视为 Excel 工作簿,您会在网络上找到很多示例,说明您需要做什么。

ExcelFile ef = new ExcelFile();

// Loads file.
ef.LoadCsv("filename.csv");

// Selects first worksheet.
ExcelWorksheet ws = ef.Worksheets[0];
于 2012-09-25T11:48:58.500 回答
1

我不会详细介绍,但您可以使用File.ReadAllLines.
一旦你有了这些行,你可以使用将它们分成几部分String.Split(至少如果 CSV 文件包含非常简单的信息,例如你的示例,这将起作用)。

于 2012-09-25T11:49:11.200 回答