1

我是 CSharp 编程的新手。我需要从一个文件夹中读取多个 excel 文件。我不知道该文件夹中的 excel 文件数。我想一一阅读所有文件。为了读取一个文件,我编写了一些代码。我想应用此代码来一一读取文件夹中的所有文件。请让我知道路。这是我的代码。

class RatWalk
{
    public List<RatStep> steps = new List<RatStep>();
    string[] Individal_Runs = Directory.GetFiles(@"C:\Users\AG_Winter\Desktop\Individual_Runs");
    public void LoadFromFile(String fileName) // reads data from excel file
    {
        steps.Clear();
        XlsFile file = new XlsFile(fileName);
        try
        {
           // Everything I wanna do
        }
        catch (NullReferenceException ex)
        {
            Console.Out.WriteLine("No run");
        }
    }
}

谢谢你们。我不知道如何回复帖子,因为评论应该是有限的字符数。所以我在这里输入。

在我的程序中,我想从一个文件夹中一个一个地读取 xlsx 文件。到目前为止,我正在使用一个按钮来浏览单个文件。但后来我想使用这个按钮来浏览我有文件的文件夹。这样当我选择这个文件夹时,程序应该会自动一个一个地运行文件夹中的所有文件。这是我在此之前所做的。

[\code = c#] class RatWalk { public List steps = new List();

    public void LoadFromFile(String fileName)                       // reads data from excel file
    {

            steps.Clear();

            XlsFile file = new XlsFile(fileName);
try{
//everything I wanna do

}抓住{} }

 private void InitializeComponent()                             
        {
            EventHandler handler = new EventHandler(OnClick);
            button.Text = "Browse for the XLS file";                    
            // button properties                                       
            this.Controls.Add(button);
}
private void OnClick(object sender, EventArgs e)            // Browses for the file and loads the selected Excel file
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            if (fileDialog.ShowDialog() != DialogResult.OK)
                return;
            ratWalk.LoadFromFile(fileDialog.FileName);

           // Whatever I wanna do   
        }

在这里,我想改变它,当我单击按钮并选择文件夹时,它应该一个一个地运行文件夹中的所有文件。

请让我知道如何做到这一点。

谢谢你。

4

3 回答 3

3

我会给你举个例子,剩下的就交给你了。

string [] fileEntries = Directory.GetFiles(sourceDir);
foreach(string fileName in fileEntries)
{
   // do something with fileName
   Console.WriteLine(fileName);
}

顺便说一句,这不会遍历子文件夹。

于 2012-12-17T11:48:16.877 回答
1
        string[] Individal_Runs = Directory.GetFiles(@"D:\testfiles");
        foreach (string s in Individal_Runs)
        {
            try
            {
String theConnString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + s + ";Extended Properties=Excel 8.0;";

    OleDbConnection objConn = new OleDbConnection(theConnString);
    objConn.Open();

    OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [sheet1$]", objConn);
    OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

    objAdapter1.SelectCommand = objCmdSelect;
    DataSet objDataset1 = new DataSet();
    objAdapter1.Fill(objDataset1, "XLData");

     //Your code here

    objConn.Close();


            }
            catch (NullReferenceException ex)
            {
                Console.Out.WriteLine("No run");
            }
        }
于 2012-12-17T11:56:02.537 回答
0
  1. 获取目标文件夹的路径。
  2. 获得文件夹后,以编程方式获取其下的所有 excel 文件并将它们放入列表中。
  3. 您的代码读取一个 excel 文件,遍历 excel 文件列表并在循环中使用相同的函数。
using System;
using System.IO;

namespace FileOperationsSample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Change this path to the directory you want to read
            string path = "C:\\Junk";             
            DirectoryInfo dir = new DirectoryInfo(path);
            Console.WriteLine("File Name                       Size        Creation Date and Time");
            Console.WriteLine("=================================================================");
            foreach (FileInfo flInfo in dir.GetFiles())
            {
                String name = flInfo.Name;
                long size = flInfo.Length;
                DateTime creationTime = flInfo.CreationTime;
                Console.WriteLine("{0, -30:g} {1,-12:N0} {2} ", name, size, creationTime);
            }
            Console.ReadLine();
        }
    }
}
于 2012-12-17T11:49:34.150 回答