How to iterate through Excel Worksheets only extracting data from specific columns

感情迁移 提交于 2019-11-27 07:03:59

问题


How do you iterate through an excel workbook with multiple worksheets only extracting data from say columns "C", "E" & "F"?

Here is the code I have thus far:

public static string ExtractData(string filePath)
    {
        Excel.Application excelApp = new Excel.Application();
        Excel.Workbook workBook = excelApp.Workbooks.Open(filePath);

        string data = string.Empty;

        int i = 0;
        foreach (Excel.Worksheet sheet in workBook.Worksheets)
        {
            data += "*******   Sheet " + i++.ToString() + "   ********\n";

            //foreach (Excel.Range row in sheet.UsedRange.Rows)
            //{
            //    data += row.Range["C"].Value.ToString();
            //}

            foreach (Excel.Range row in sheet.UsedRange.Rows)
            {
                foreach (Excel.Range cell in row.Columns)
                {
                    data += cell.Value + "   ";
                }
                data += "\n";
            }
        }

        excelApp.Quit();

        return data;
    }

Thank you very much for your time, any help is appreciated.


回答1:


Editing your method, here's something should do what you're looking for:

public static string ExtractData(string filePath)
{
    Excel.Application excelApp = new Excel.Application();
    Excel.Workbook workBook = excelApp.Workbooks.Open(filePath);
    int[] Cols = { 3, 5, 6 }; //Columns to loop
                 //C, E, F
    string data = string.Empty;

    int i = 0;
    foreach (Excel.Worksheet sheet in workBook.Worksheets)
    {
        data += "*******   Sheet " + i++.ToString() + "   ********\n";

        foreach (Excel.Range row in sheet.UsedRange.Rows)
        {
            foreach (int c in Cols) //changed here to loop through columns
            {
                data += sheet.Cells[row.Row, c].Value2.ToString() + "   ";
            }
            data += "\n";
        }
    }

    excelApp.Quit();

    return data;
}

I've created a int array to indicate which columns you'd like to read from, and then on each row we just loop through the array.

HTH, Z




回答2:


You could use something like this to get Column C for example:

var numberOfRows = sheet.UsedRange.Columns[3, Type.Missing].Rows.Count;
var values = sheet.Range["C1:C" + numberOfRows].Value2;

numberOfRows holds the number of rows in the worksheet (I think it doesn't skip the blank rows at the top tho, not sure). After that you select a range from C1 to CN and get the Value2 which contains the values. Mind that the values array is actually a two dimensional array. You can now easily do a for loop to get the items:

for (int i = 1; i <= values.Length; i++){
   sb.Append(values[i, 1] + " ");
}

This could be optimized in case columns are next to each other and such, but the above code should get you started.



来源:https://stackoverflow.com/questions/16156951/how-to-iterate-through-excel-worksheets-only-extracting-data-from-specific-colum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!