如何用C获取Excel单元格中值+ [英] How to get Excel cell value in C++

查看:252
本文介绍了如何用C获取Excel单元格中值+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人知道如何从Excel中去::窗口指针在打开的Excel单元格中的真正价值,请让我知道。
这里是任务的条件:
- Excel中,目前在一个窗口在一张纸上运行带有一个工作簿
- 有些细胞具有数据(简化假设只有一格[1,1]的数据,这是A)
现在的问题是如何找出只有一个细胞中有数据,并且单元是[1,1],数据为a。
与这里开始是code片断:

  INT主(INT ARGC,CHAR *的argv [])
{
   CoInitialize的(NULL);
   HWND excelWindow = FindWindow函数(LXLMAIN,NULL);
   EnumChildWindows(excelWindow,(WNDENUMPROC)EnumChildProc,(LPARAM)1);
   返回0;
}
BOOL回调EnumChildProc(HWND HWND,LPARAM)
{
   WCHAR szClassName [64];
   如果(GetClassNameW(HWND,szClassName,64))
   {
      如果(_wcsicmp(szClassName,LEXCEL7)== 0)
      {
         Excel中::窗口* pWindow = NULL;
         HRESULT HR = AccessibleObjectFromWindow(HWND,OBJID_NATIVEOM,__uuidof(EXCEL ::窗口),(无效**)及pWindow);
         如果(HR = = S_OK)
         {
            //这里我们需要回答使用pWindow问题
            pWindow->发行();
         }
         返回false;
      }
   }
   返回true;
}


解决方案

下面是code终于用:

 的#include<&WINDOWS.H GT;
#包括LT&;&oleacc.h GT;
#进口C:\\ Program Files文件(x86)的\\ Common Files文件\\微软共享\\ OFFICE14 \\ MSO.DLLno_implementation重命名(RGB,ExclRGB)重命名(DocumentProperties,ExclDocumentProperties)重命名(SearchPath中的 ExclSearchPath)
#进口C:\\ Program Files文件(x86)的\\ Common Files文件\\微软共享\\ VBA \\ VBA6 \\ VBE6EXT.OLBno_implementation
#进口C:\\ Program Files文件(x86)的\\的Microsoft Office \\ OFFICE14 \\ EXCEL.EXE重命名(DialogBox的,ExclDialogBox)重命名(RGB,ExclRGB)重命名(CopyFile,ExclCopyFile )重命名(ReplaceText,ExclReplaceText)
#包括LT&;串GT;
使用std :: string的;BOOL回调EnumChildProc(HWND HWND,LPARAM)
{
    WCHAR szClassName [64];
    如果(GetClassNameW(HWND,szClassName,64))
    {
        如果(_wcsicmp(szClassName,LEXCEL7)== 0)
        {
            //获取的AccessibleObject
            Excel中::窗口* pWindow = NULL;
            IDispatch的* pXlSheet = NULL;
            HRESULT HR = AccessibleObjectFromWindow(HWND,OBJID_NATIVEOM,__uuidof(EXCEL ::窗口),(无效**)及pWindow);
            如果(HR = = S_OK)
            {
                尝试{
                    VARIANT的结果;
                    VariantInit(安培;结果);
                    AutoWrap(DISPATCH_PROPERTYGET,&放大器;结果,pWindow,LActiveSheet,0);
                    pXlSheet = result.pdispVal;
                    _variant_t SHEETNAME;
                    VariantInit(安培; SHEETNAME);
                    如果((pXlSheet = NULL)及!&安培;!(pXlSheet =(IDispatch的*)0xCCCCCCCC))
                    AutoWrap(DISPATCH_PROPERTYGET,&放大器; SHEETNAME,pXlSheet,L姓名,0);                    //获取你需要的细胞
                    字符串位置=C5;
                    位置+ =+位置; //细胞是具有相同的开始/结束单元格的范围内
                    OLECHAR * sOleText =新OLECHAR [location.length()+ 1];
                    mbstowcs(sOleText,location.c_str(),location.length()+ 1);                    IDispatch的* pXlRange; //从表获取范围
                    VARIANT PARM;
                    parm.vt = VT_BSTR;
                    parm.bstrVal = :: SysAllocString(sOleText);
                    VARIANT的结果;
                    VariantInit(安培;结果);
                    AutoWrap(DISPATCH_PROPERTYGET,&安培;结果,pXlSheet,L范围,1,PARM);
                    VariantClear(安培; PARM);
                    pXlRange = result.pdispVal;
                    Excel中:: RangePtr电池= pXlRange;
                    Excel中:: CharactersPtr PTR =小区>字符;
                    _bstr_t文本= ptr->文字; //这里需要的文字
                }赶上(...){}
            }
            返回false; //停止通过枚举儿童
        }
    }
    返回true;
}无效的主要(INT ARGC,CHAR *的argv [])
{
    CoInitialize的(NULL);
    HWND excelWindow = FindWindow函数(LXLMAIN,NULL);
    EnumChildWindows(excelWindow,(WNDENUMPROC)EnumChildProc,(LPARAM)1);
    CoUninitialize();
}

该AutoWrap()函数已从采取的http://支持.microsoft.com / KB / 216686 / EN-US?FR = 1 ,而来自<一个初始样品href=\"http://www.northatlantawebdesign.com/index.php/2009/07/15/access-microsoft-excel-2007-com-api-through-microsoft-active-accessibility/\" rel=\"nofollow\">http://www.northatlantawebdesign.com/index.php/2009/07/15/access-microsoft-excel-2007-com-api-through-microsoft-active-accessibility/
感谢所有谁帮助。

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 single window with one workbook on one sheet - some cells have data (to simplify let's say only one cell[1,1] has data, which is "a") The question is how to find out that only one cell has data, and cell is [1,1] and the data is "a". To start with here is a code snippet:

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;
}  

解决方案

Here is code finally used:

#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();
}

The AutoWrap() function has been taken from http://support.microsoft.com/kb/216686/en-us?fr=1, while the initial sample from http://www.northatlantawebdesign.com/index.php/2009/07/15/access-microsoft-excel-2007-com-api-through-microsoft-active-accessibility/ Thanks to all who helped.

这篇关于如何用C获取Excel单元格中值+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆