Parsing an excel file and reading a cell

后端 未结 5 735
故里飘歌
故里飘歌 2021-01-16 16:22

I got an excel file. I have uploaded the screenshot. I need to write a .NET application (console application) to parse the excel file. You can see a cell titled \"Function N

相关标签:
5条回答
  • 2021-01-16 16:46

    Based on the Description on what you are trying to do you should really use LINQ to Ecxel plugin I think it simple way to solve your problem

    http://code.google.com/p/linqtoexcel/

    0 讨论(0)
  • 2021-01-16 16:46

    There are a handful of tutorials here that should get you started:

    http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm

    0 讨论(0)
  • 2021-01-16 16:52

    Extra notes for future reference. Just wanted to point out that, working with interops are messy. Your best bet would be to use libraries.

    0 讨论(0)
  • 2021-01-16 16:56

    I strongly suggest using a library like Excel Data Reader and work with the data in managed code. The CodePlex site has a good example of doing what you need.

    0 讨论(0)
  • 2021-01-16 17:00

    Using Excel interop you can do it this way. I supposed that the function names are pointing to functions that are defined in your excel workbook, otherwise you have to change that part to use reflection (but in this case you'd need a receiver for the methods, and I don't see one in the question).

            object hmissing = System.Reflection.Missing.Value; 
    
            Xls.Application App = new Xls.ApplicationClass();
            App.Visible = true;
            Xls.Workbook wb = App.Workbooks.Open(@"c:\tmp\cartel1.xls", hmissing, hmissing, hmissing, hmissing, hmissing, hmissing,
                hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing);
            Xls.Worksheet ws = (Xls.Worksheet)wb.ActiveSheet;
            Xls.Range rng = ws.UsedRange;
            Xls.Range hdr = rng.Find("Function Name", hmissing, hmissing, hmissing, hmissing, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, hmissing, hmissing, hmissing);
            string hdrAdd = hdr.get_Address(hmissing, hmissing, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, hmissing, hmissing);
            string[] pcs = hdrAdd.Split('$');
            string col = pcs[1];
            int row;
            int.TryParse(pcs[2], out row);
            string methName;
            row++;
            while ((methName = App.get_Range(col + row.ToString(), hmissing).get_Value(hmissing) as String) != null)
            {
                App.Run(methName.Split('.')[1], hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing,
                    hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing,
                    hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing,
                    hmissing, hmissing, hmissing);
                row++;
            }
    
    0 讨论(0)
提交回复
热议问题