中升乙源建设工程有限公司网站,南京网站建设小程序,php调用网站,昆明专业做网站多少钱本文转自#xff1a;http://www.vcfans.com/2010/08/vs2010-mfc-excel-file-in-the-data-analysis.html 前两天折腾一个小功能#xff0c;需求是解析Excel中的数据出来。网上一般使用的方案#xff1a;1. ODBC当数据库来操作。2. 使用第三方的类库3. 使用COM调用Excel.exe中…本文转自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中加入如下代码 ?[Copy to clipboard]View Code CPP 1
2
3
4
5
if(!AfxOleInit()){AfxMessageBox(_T( Cannot initialize COM dll ));return FALSE;} 第二步向工程添加Excel的C操作类。具体步骤见下图1.CtrlShiftX 打开类向导。 2.添加“类型库中MFC类”。此处操作与VC6.0有很大的不一样让我找了半天如下图 3.添加需要的类文件。如下图 4.将刚才导入的类头文件加到模块中。 ?[Copy to clipboard]View Code CPP 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将给你如下惊喜 1d:\xls\xls\debug\excel.tlh(74974): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(74974): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(74974): warning C4183: “GetMailEnvelope”: 缺少返回类型假定为返回“int”的成员函数1d:\xls\xls\debug\excel.tlh(75683): error C2146: 语法错误: 缺少“;”(在标识符“HTMLProject”的前面)1d:\xls\xls\debug\excel.tlh(75683): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75683): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75693): error C2146: 语法错误: 缺少“;”(在标识符“CommandBars”的前面)1d:\xls\xls\debug\excel.tlh(75693): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75693): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75737): error C2146: 语法错误: 缺少“;”(在标识符“VBProject”的前面)1d:\xls\xls\debug\excel.tlh(75737): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75737): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75739): error C2146: 语法错误: 缺少“;”(在标识符“Permission”的前面)1d:\xls\xls\debug\excel.tlh(75739): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75739): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75741): error C2146: 语法错误: 缺少“;”(在标识符“SharedWorkspace”的前面)1d:\xls\xls\debug\excel.tlh(75741): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75741): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75743): error C2146: 语法错误: 缺少“;”(在标识符“Sync”的前面)1d:\xls\xls\debug\excel.tlh(75743): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75743): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75757): error C2146: 语法错误: 缺少“;”(在标识符“SmartDocument”的前面)1d:\xls\xls\debug\excel.tlh(75757): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75757): error C4430: 缺少类型说明符 – 假定为 int。注意: C 不支持默认 int1d:\xls\xls\debug\excel.tlh(75759): error C2146: 语法错误: 缺少“;”(在标识符“DocumentLibraryVersions”的前面)1d:\xls\xls\debug\excel.tlh(75759): fatal error C1003: 错误计数超过 100正在停止编译 生成: 成功 0 个失败 1 个最新 0 个跳过 0 个 我也不知道为什么造成这种结果可能是由于这类接口是MS在N年前就弄好后来VS升级了相关接口也就年久失修了。造成我们这帮使用老技术的人的尴尬后来求助于万能的Google大侠得到如下方案 将刚才导入的几个头文件中的 ?[Copy to clipboard]Download download.txt 1
#import D:\\Program Files\\Microsoft Office\\Office12\\EXCEL.EXE no_namespace 替换成 ?[Copy to clipboard]Download download.txt 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.OLBusing 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进行操作吧。 有图有真像 转载于:https://www.cnblogs.com/freeliver54/archive/2012/10/12/2720785.html