开发者

Reading an Excel 2007 cell formula value using C#

I've got a fairly large and complex Excel 2007 file and a cell with the a formula which references a cell in another worksheet. I've tried so many things to try to get the value (as in the one I can see when loaded in Excel itself) but mostly related to trying to get child objects of OpenXmlPart's. The value of the cell is 40178, but there is no list in the file with that many indices. The formula in the cell is "'Input Control Sheet'!$开发者_运维问答F$8". I'm (perhaps stupidly) assuming that this string can be used directly on the OpenXML API to read the value from the cell referenced by that string, but am I wrong?

If someone could tell me how to get the value from the correct cell out based on the formula I would be very happy - I've been basing my work so far on the code provided by Microsoft (terrible) help page: http://msdn.microsoft.com/en-us/library/cc850837.aspx

Thanks,

Matt.


Probably u can use this code to read each cell value. U can modify the code accordingly, for now this code is reading the values from excel file and returning the list of values read:

First put this: using Excel = Microsoft.Office.Interop.Excel;

private List<string> GetKeywordsList(string xlsFilePath)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        Excel.Range range;
        string str;
        int rCnt = 0;
        int cCnt = 0;

        List<string> keywords = new List<string>();
        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Open(xlsFilePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        range = xlWorkSheet.UsedRange;
        for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
        {
            for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
            {

                if (!(((range.Cells[rCnt, cCnt] as Excel.Range).Value2) == null))
                {
                    if ((range.Cells[rCnt, cCnt] as Excel.Range).Value2.GetType().ToString() == "System.Double")
                    {
                        double d1 = (Double)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                        str = Convert.ToString(d1);
                        keywords.Add(str);
                    }
                    else
                    {
                        str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                        keywords.Add(str);
                    }
                }
            }

        }

        xlWorkBook.Close(true, null, null);
        xlApp.Quit();

        ReleaseObject(xlWorkSheet);
        ReleaseObject(xlWorkBook);
        ReleaseObject(xlApp);
        return keywords;

    }

private void ReleaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
        }
        finally
        {
            GC.Collect();
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜