2021-04-29

How to calculate sum of first cell [a1] in multiple excel files

I am trying to create a method that retrieves the first value of each csv file within a folder. Each csv file within the main directory contains a number in position a1 of the excel file. I want to get this number from each file in the directory, convert it to an int (as they are stored as a string in excel), calculate the sum of all numbers, and store it as an int variable.

I have this method here that gets all of the files, but I'm not sure how I implement a statement that reads the first value, converts it to int, then gets the sum.

public void LoadCashFlow()
{
    string path = @"C:\temp\CashFlow\";
    string[] fileEntries = Directory.GetFiles(path);
foreach (string fileName in fileEntries)
{
    //Get first value [a1]
    //Convert to int
    //Get sum of all values
    //Store as variable
}
}

How might I achieve this?

Edit: This is what I have tried:

public void LoadCashFlow()
{
    string path = @"C:\temp\CashFlow\";
    string[] fileEntries = Directory.GetFiles(path);

    foreach (string fileName in fileEntries)
    {
        // do something with fileName
        string[] lines = File.ReadAllLines(fileName);
        string[] data;


        for (int i = 0; i < lines.Length; i++)
        {
            data = lines[i].ToString().Split(',');

            string[] row = new string[data.Length];

            for (int j = 0; j < data.Length; j++)
            {
                row[j] = data[j].Trim();
            }
            int value = System.Convert.ToInt32(data.ToString());
            int sum = "Something here";
        }
    }
}


from Recent Questions - Stack Overflow https://ift.tt/3xqrzHE
https://ift.tt/eA8V8J

No comments:

Post a Comment