c++ - 如何在C++中获取Excel单元格值

标签 c++ excel automation cell

如果有人知道如何从 Excel::Window 指针转到打开的 Excel 单元格中的实际值,请告诉我。 任务条件如下: - Excel 当前在一个窗口中运行,一张工作表上有一个工作簿 - 一些单元格有数据(为了简化,我们假设只有一个单元格[1,1]有数据,即“a”) 问题是如何找出只有一个cell有数据,cell是[1,1],数据是“a”。 首先是一个代码片段:

int main( int argc, CHAR* argv[])
{
   CoInitialize( NULL );
   HWND excelWindow = FindWindow(L"XLMAIN", NULL);
   EnumChildWindows(excelWindow, (WNDENUMPROC) EnumChildProc, (LPARAM)1);
   return 0;
}
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM)
{
   WCHAR szClassName[64];
   if(GetClassNameW(hwnd, szClassName, 64))
   {
      if(_wcsicmp(szClassName, L"EXCEL7") == 0)
      {
         Excel::Window* pWindow = NULL;
         HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Excel::Window), (void**)&pWindow);
         if(hr == S_OK)
         {
            // Here we need to answer the question using pWindow                
            pWindow->Release();
         }
         return false;     
      }     
   }
   return true;
}  

最佳答案

这是最终使用的代码:

#include <windows.h>
#include <oleacc.h>
#import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\MSO.DLL" no_implementation rename("RGB", "ExclRGB") rename("DocumentProperties", "ExclDocumentProperties") rename("SearchPath", "ExclSearchPath")
#import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\VBE6EXT.OLB" no_implementation
#import "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" rename("DialogBox", "ExclDialogBox") rename("RGB", "ExclRGB") rename("CopyFile", "ExclCopyFile") rename("ReplaceText", "ExclReplaceText")
#include <string>
using std::string;

BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM)
{
    WCHAR szClassName[64];
    if(GetClassNameW(hwnd, szClassName, 64))
    {
        if(_wcsicmp(szClassName, L"EXCEL7") == 0)
        {
            //Get AccessibleObject
            Excel::Window* pWindow = NULL;
            IDispatch* pXlSheet = NULL;
            HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Excel::Window), (void**)&pWindow);
            if(hr == S_OK)
            {               
                try {
                    VARIANT result;
                    VariantInit(&result);
                    AutoWrap(DISPATCH_PROPERTYGET, &result, pWindow, L"ActiveSheet", 0);
                    pXlSheet = result.pdispVal;
                    _variant_t sheetName;
                    VariantInit(&sheetName);
                    if ((pXlSheet != NULL) && (pXlSheet != (IDispatch*)0xCCCCCCCC))
                    AutoWrap(DISPATCH_PROPERTYGET, &sheetName, pXlSheet, L"Name", 0); 

                    // get cell which you need
                    string location = "C5"; 
                    location += ":" + location; // cell is a range with the same start/end cells
                    OLECHAR *sOleText=new OLECHAR[location.length()+1];
                    mbstowcs(sOleText,location.c_str(),location.length()+1);

                    IDispatch *pXlRange; // Get Range from Sheet
                    VARIANT parm;
                    parm.vt = VT_BSTR;
                    parm.bstrVal = ::SysAllocString(sOleText); 
                    VARIANT result;
                    VariantInit(&result);
                    AutoWrap(DISPATCH_PROPERTYGET, &result, pXlSheet, L"Range", 1, parm);
                    VariantClear(&parm);
                    pXlRange = result.pdispVal;
                    Excel::RangePtr cell = pXlRange;
                    Excel::CharactersPtr ptr = cell->Characters;
                    _bstr_t text = ptr->Text; // here is needed text
                } catch (...) { }
            }
            return false;     // Stops enumerating through children
        }       
    }
    return true;
}

void main( int argc, CHAR* argv[])
{
    CoInitialize(NULL);
    HWND excelWindow = FindWindow(L"XLMAIN", NULL);
    EnumChildWindows(excelWindow, (WNDENUMPROC) EnumChildProc, (LPARAM)1);
    CoUninitialize();
}

AutoWrap() 函数取自 http://support.microsoft.com/kb/216686/en-us?fr=1 ,而初始样本来自 http://www.northatlantawebdesign.com/index.php/2009/07/15/access-microsoft-excel-2007-com-api-through-microsoft-active-accessibility/ 感谢所有提供帮助的人。

关于c++ - 如何在C++中获取Excel单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8619530/

相关文章:

c++ - 使用 SDL 加载 OpenGL 纹理

c++ - 启动第二个 Linux 程序并从 C/C++ 退出当前程序?

python - 使用 Python 更新 Excel 电子表格中的链接

mysql - Excel VBA : recordset joining and performance

vba - 从格式为 (A1 :A15)

testing - 避免集成测试中的依赖

c++ - 内联函数作为模板参数,不使用函数指针

java - 当从 Selenium 触发时,Google Chrome 无法导航到指定的 URL

api - 如何通过命令行在GitHub上发布版本?

c++ - 使用 Matlab Coder 将 Matlab m 文件转换为 C/C++ 代码,包括 mex 文件 (mxArray)