How to iterate through a column in an Excel application through C# Console?

♀尐吖头ヾ 提交于 2020-01-11 10:58:11

问题


I have created a Console Application that reads from an Excel file. I want to take all of the document numbers and place it in an array for searching purposes. I am able to find the cell that contains the data "DocumentNumber" e.g. $F$5. How can I iterate through rows within column F? I have the following code:

public static void Main(string[] args)
    {
        string address;
        string next;
        try
        {
            Excel.ApplicationClass excel = new Excel.ApplicationClass();
            Excel.Workbook workbook = excel.Workbooks.Open(@"D:\sample.xls", Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value);
            Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets["Sheet1"];
            Excel.Range docNumber = worksheet.Cells.Find("DocumentNumber", worksheet.Cells[1, 1], Excel.XlFindLookIn.xlValues,
                Excel.XlLookAt.xlPart, Missing.Value, Excel.XlSearchDirection.xlNext,
                false, Missing.Value, Missing.Value);

            if(docNumber != null){
                address = docNumber.get_Address(true, true, Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value);
                Console.WriteLine(address);

            }
        }
        catch (Exception err)
        {
            Console.WriteLine(err.Message);
        }
    }

Thanks in advance.


回答1:


kindly look into this.It iterates through each and every rows and the columns.

string address;  
string next; 

try {  
    Excel.ApplicationClass excel = new Excel.ApplicationClass();
    object Missing = Type.Missing;
    FileInfo fInfo = new FileInfo(@"D:\sample.xls");

    if(fInfo.Exists) {
        Excel.Workbook workbook = excel.Workbooks.Open(@"D:\sample.xls", Missing, Missing, 
                    Missing, Missing, Missing, Missing, Missing,  
                    Missing, Missing, Missing, Missing, Missing,  
                    Missing, Missing);      
        Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets["Sheet1"];
        Excel.Range docNumber = worksheet.Cells.Find("DDEC", worksheet.Cells[1, 1], 
        Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Missing, Excel.XlSearchDirection.xlNext, false, Missing, Missing);     

        if(docNumber != null) {
            address = docNumber.get_Address(true, true, Excel.XlReferenceStyle.xlA1, Missing, Missing);
            docNumber = worksheet.UsedRange;                        

            for (int rCnt = 1; rCnt <= docNumber.Rows.Count; rCnt++) {
                for (int cCnt = 1; cCnt <= docNumber.Columns.Count; cCnt++) {
                    string str = (string)(docNumber.Cells[rCnt, cCnt] as Excel.Range).Value2;
                    MessageBox.Show(str);
                }
            }
            Console.WriteLine(address); 
        }     
    }
}



回答2:


Wouldn't it be easier to read the data using OLEDB? Cleaner syntax, then you can foreach a DataTable that is returned.




回答3:


please refer http://www.sharpprogrammer.com/dotnet/how-to-read-excel-file-in-c-net/



来源:https://stackoverflow.com/questions/4488403/how-to-iterate-through-a-column-in-an-excel-application-through-c-sharp-console

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