How to get Excel cell value in C++

前端 未结 2 1986
夕颜
夕颜 2021-02-06 18:42

If someone knows how to go from Excel::Window pointer to real value in opened Excel cell, please let me know. Here is the task conditions: - Excel is currently running in one s

2条回答
  •  余生分开走
    2021-02-06 19:19

    Daniil,

    I believe you are using the Excel COM to access it. I've never done this using C++, but I guess it shouldn't be much different than C#. Your toughest task should be importing the necessary libraries to do so. But I believe you have done that already.

    You should use something like this:

    int full_cells = 0;
    Excel::_Worksheet worksheet = pWindow->ActiveSheet;
    for(int j = 1; j < worksheet.Columns.Count; j++)
    {
        for (int i = 1; i < worksheet.Rows.Count; i++)
        {
            if(worksheet.Cells[i][j].Value != "")
            {
                full_cells++;
                cout << worksheet.Cells[i][j].Value;
            }
         }
    }
    

    Please note I haven't tested this code. I'm just showing you what it is supposed to look like. You should read the documentation here: http://msdn.microsoft.com/en-us/library/ms262200.aspx and use it appropriately.

    Hope it helps.

提交回复
热议问题