转载:how to automate Microsoft Excel and return the values from a multi-cell range to an array

This article was previously published under Q186122

 

SUMMARY
This article demonstrates how to automate Microsoft Excel and return the values from a multi-cell range to an array.

 

MORE INFORMATION
To return the values from a multi-cell range without querying the cells one-by-one, you must use the GetValue member function of the Range object to return the values to a VARIANT. You then construct a SAFEARRAY based on the VARIANT you returned from GetValue. Once you have the SAFEARRAY, you can access the elements using GetElement. The following steps illustrate this process and demonstrate how you can access both string and numeric elements.


Notes for Automating Microsoft Excel 2000 and 2002
The sample code in this article uses class wrappers generated from the Excel 97 object library (Excel 8.olb). With slight modification, this code can be applied to an Automation client that uses class wrappers for Excel 2000 (Excel9.olb) or Excel 2002 (Excel.olb). For additional information about using the sample code described in this article with the Microsoft Excel 2000 or 2002 type library, click the article number below to view the article in the Microsoft Knowledge Base:
224925  (http://support.microsoft.com/kb/224925/EN-US/ ) INFO: Type Libraries for Office May Change with New Release


Steps to Create Project

   1. In Microsoft Excel, create a new workbook. Populate cells A1:C8 with a mixture of string and numeric data. Save the workbook as "c:/test.xls" and exit Microsoft Excel.
   2. Follow steps 1 through 12 in the following article in the Microsoft Knowledge Base to create a sample project that uses the IDispatch interfaces and member functions defined in the Excel8.olb type library:
      178749  (http://support.microsoft.com/kb/178749/EN-US/ ) How To Create Automation Project Using MFC and a Type Library
   3. At the top of the AutoProjectDlg.cpp file, add the following line:

   4. Add the following code to CAutoProjectDlg::OnRun() in the AutoProjectDlg.cpp file:
      Back to the top
      Sample Code

            

   5. Compile the project and run it.
   6. Click Run. The values in cells A1:C8 are displayed in the Output window.

 

(原文出处:http://support.microsoft.com/kb/186122/EN-US

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值