简体   繁体   English

如何从C#的文件夹中读取多个.CSV文件?

[英]How to read multiple .CSV files from a folder in c#?

I am writing a console application that reads multiple CSV files from the specified folder using SmartXLS library. 我正在编写一个控制台应用程序,该应用程序使用SmartXLS库从指定的文件夹中读取多个CSV文件。 I am able to read from a single file but unable to figure out how to read multiple files. 我能够从单个文件读取,但无法弄清楚如何读取多个文件。 Kindly, help me with this. 请帮助我。

public void GetData()
        {

            int count = 0;

            DeskTokens = new List<Token>();

            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string path = Path.Combine(directory, @"C:\projects\Product_Usage_Year.csv");

            SmartXLS.WorkBook WB = new WorkBook();
            WB.readCSV(path);

            DataTable dt = WB.ExportDataTable();

            string CurrentType = string.Empty;
            string CurrentCategory = string.Empty;

            DataRow dr;
            for (int i = 1; i < dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                var tkn = new Token();

                tkn.Product_name = dr[0].ToString();
                tkn.Product_Version = dr[1].ToString();
                tkn.Userid = dr[2].ToString();
                tkn.User_name = dr[3].ToString();

                DeskTokens.Add(tkn);
                count++;
                Console.WriteLine("Read : " + count);

                Console.WriteLine("    Reading : " + tkn.Product_name + "," + tkn.Product_Version + "," + tkn.Userid + "," + tkn.User_name);

            }
        }

Below, "path" is the directory in which all your CSV files reside. 下面的"path"是所有CSV文件所在的目录。

var files = Directory.EnumerateFiles("path", "*.csv");
foreach (string file in files)
{
    using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
    {
        // Use the file stream to read data.
    }
}

Use Directory.GetFiles : 使用Directory.GetFiles

// Gets only .csv files 
string[] csvFiles = Directory.GetFiles(directoryPath, "*.csv");

and write a loop like this: 并编写一个像这样的循环:

foreach(file in csvFiles)
{
   getData(file);
   // ...
}

And also you should take a path parameter in getData method: 另外,您还应该在getData方法中采用path参数:

public void GetData(string path)
        {

            int count = 0;

            DeskTokens = new List<Token>();

            SmartXLS.WorkBook WB = new WorkBook();
            WB.readCSV(path);

            DataTable dt = WB.ExportDataTable();

            string CurrentType = string.Empty;
            string CurrentCategory = string.Empty;

            DataRow dr;
            for (int i = 1; i < dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                var tkn = new Token();

                tkn.Product_name = dr[0].ToString();
                tkn.Product_Version = dr[1].ToString();
                tkn.Userid = dr[2].ToString();
                tkn.User_name = dr[3].ToString();

                DeskTokens.Add(tkn);
                count++;
                Console.WriteLine("Read : " + count);

                Console.WriteLine("    Reading : " + tkn.Product_name + "," + tkn.Product_Version + "," + tkn.Userid + "," + tkn.User_name);

            }
        }

I hope to be helpful for you:) 希望对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM