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

后端 未结 3 361
傲寒
傲寒 2021-01-22 18:02

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

相关标签:
3条回答
  • 2021-01-22 18:16

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

    0 讨论(0)
  • 2021-01-22 18:21

    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); 
            }     
        }
    }
    
    0 讨论(0)
  • 2021-01-22 18:33

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

    0 讨论(0)
提交回复
热议问题