zoukankan      html  css  js  c++  java
  • [转]VS2010+MFC解析Excel文件中数据

    本文转自:http://www.vcfans.com/2010/08/vs2010-mfc-excel-file-in-the-data-analysis.html

    前两天折腾一个小功能,需求是解析Excel中的数据出来。网上一般使用的方案:
    1. ODBC当数据库来操作。
    2. 使用第三方的类库
    3. 使用COM调用Excel.exe中的接口。

    第三方类库我怕MS升级版本后,是否仍然使用是个问号。使用ODBC的方案感觉很怪怪的,但据说不能修改删除什么的,不过我没有试验,还是放弃此种方案。我最终还是使用COM来做的,本来是没什么实现代码,都是COM实现好的,没什么可写的。难点在于VS2010的版本与MSDN上的说法严重不一致,折腾死人。MSDN上说的方案是VC6.0的实现方法。而且加入COM后与VS的SDK库冲突,无法编译。下面说说我的使用方法。

    第一步:初化COM接口。在InitInstance中加入如下代码:

    1
    2
    3
    4
    5
    
    if(!AfxOleInit())
    	{
    		AfxMessageBox(_T( "Cannot initialize COM dll" ));
    		return FALSE;
    	}

    第二步:向工程添加Excel的C++操作类。具体步骤见下图:
    1.Ctrl+Shift+X 打开类向导。

    2.添加“类型库中MFC类”。此处操作与VC6.0有很大的不一样,让我找了半天,如下图:

    3.添加需要的类文件。如下图:

    4.将刚才导入的类头文件,加到模块中。

    1
    2
    3
    4
    5
    6
    
    #include "CApplication.h"
    #include "CWorkbooks.h"
    #include "CWorksheets.h"
    #include "CWorkbook.h"
    #include "CWorksheet.h"
    #include "CRange.h"

    到此好像一切都结束了,下面就是写调用上面载入的代码了。But……,当你编译时。MS将给你如下惊喜:

    1>d:\xls\xls\debug\excel.tlh(74974): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(74974): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(74974): warning C4183: “GetMailEnvelope”: 缺少返回类型;假定为返回“int”的成员函数
    1>d:\xls\xls\debug\excel.tlh(75683): error C2146: 语法错误: 缺少“;”(在标识符“HTMLProject”的前面)
    1>d:\xls\xls\debug\excel.tlh(75683): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75683): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75693): error C2146: 语法错误: 缺少“;”(在标识符“CommandBars”的前面)
    1>d:\xls\xls\debug\excel.tlh(75693): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75693): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75737): error C2146: 语法错误: 缺少“;”(在标识符“VBProject”的前面)
    1>d:\xls\xls\debug\excel.tlh(75737): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75737): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75739): error C2146: 语法错误: 缺少“;”(在标识符“Permission”的前面)
    1>d:\xls\xls\debug\excel.tlh(75739): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75739): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75741): error C2146: 语法错误: 缺少“;”(在标识符“SharedWorkspace”的前面)
    1>d:\xls\xls\debug\excel.tlh(75741): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75741): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75743): error C2146: 语法错误: 缺少“;”(在标识符“Sync”的前面)
    1>d:\xls\xls\debug\excel.tlh(75743): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75743): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75757): error C2146: 语法错误: 缺少“;”(在标识符“SmartDocument”的前面)
    1>d:\xls\xls\debug\excel.tlh(75757): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75757): error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
    1>d:\xls\xls\debug\excel.tlh(75759): error C2146: 语法错误: 缺少“;”(在标识符“DocumentLibraryVersions”的前面)
    1>d:\xls\xls\debug\excel.tlh(75759): fatal error C1003: 错误计数超过 100;正在停止编译
    ========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

    我也不知道为什么造成这种结果,可能是由于这类接口是MS在N年前就弄好,后来VS升级了,相关接口也就年久失修了。造成我们这帮使用老技术的人的尴尬,后来求助于万能的Google大侠,得到如下方案:

    将刚才导入的几个头文件中的:

    1
    
    #import "D:\\Program Files\\Microsoft Office\\Office12\\EXCEL.EXE" no_namespace

    替换成:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    
    #pragma region Import the type libraries
     
    #import "libid:2DF8D04C-5BFA-101B-BDE5-00AA0044DE52" \
    	rename("RGB", "MSORGB") \
    	rename("DocumentProperties", "MSODocumentProperties")
    // [-or-]
    //#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\MSO.DLL" \
    //	rename("RGB", "MSORGB") \
    //	rename("DocumentProperties", "MSODocumentProperties")
     
    using namespace Office;
     
    #import "libid:0002E157-0000-0000-C000-000000000046"
    // [-or-]
    //#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB"
     
    using namespace VBIDE;
     
    #import "libid:00020813-0000-0000-C000-000000000046" \
    	rename("DialogBox", "ExcelDialogBox") \
    	rename("RGB", "ExcelRGB") \
    	rename("CopyFile", "ExcelCopyFile") \
    	rename("ReplaceText", "ExcelReplaceText") \
    	no_auto_exclude
    // [-or-]
    //#import "C:\\Program Files\\Microsoft Office\\Office12\\EXCEL.EXE" \
    //	rename("DialogBox", "ExcelDialogBox") \
    //	rename("RGB", "ExcelRGB") \
    //	rename("CopyFile", "ExcelCopyFile") \
    //	rename("ReplaceText", "ExcelReplaceText") \
    //	no_auto_exclude
     
    #pragma endregion

    到此搞定环境问题,下面就根据需求对Excel进行操作吧。

    有图有真像:

  • 相关阅读:
    LeetCode 40. Combination Sum II
    LeetCode 39. Combination Sum
    #Python学习#python虚拟环境——virtualenv
    #阿里云#云服务器部署Django(基础篇)
    驱动调试之打印
    Linux内核源码阅读记录一之分析存储在不同段中的函数调用过程
    uevent机制
    字符设备的另一种写法
    DM9000C网卡驱动程序编写与测试
    网卡驱动程序框架以及虚拟网卡驱动程序的实现
  • 原文地址:https://www.cnblogs.com/freeliver54/p/2720785.html
Copyright © 2011-2022 走看看