关于OleExcel的问题

u012522824 2017-03-08 11:08:37
在网上抄来的代码,发现有两个问题。
第一个问题如下代码:
void __fastcall TForm1::Button10Click(TObject *Sender) {
Excel = GetActiveOleObject("Excel.Application"); // 本方法极易出错,重启计算机又能够运行
Excel.OlePropertySet("Caption", WideString("sss"));
}
使用GetActiveOleObject("Excel.Application")语句有时能运行,有时不能运行,请问是什么原因?有什么方法改进。

第二个问题,使用此方法时如何给Excel的枚举赋值,如下代码:
Cell.OlePropertySet("Underline",xlUnderlineStyleSingle); //"xlUnderlineStyleSingle"
其中xlUnderlineStyleSingle为VBA的枚举值,如何将此单词赋给上面的代码而不是用Int代替。
感谢大家,请多多关注!
...全文
185 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
u012522824 2017-03-10
  • 打赏
  • 举报
回复
妖哥出错了 [bcc32 Error] EXCEL操作大全Code.cpp(257): E2090 Qualifier 'XlUnderlineStyle' is not a class or namespace name Full parser context EXCEL操作大全Code.cpp(190): parsing: void _fastcall TForm1::Button12Click(TObject *) [bcc32 Error] EXCEL操作大全Code.cpp(257): E2090 Qualifier 'XlUnderlineStyle' is not a class or namespace name Full parser context EXCEL操作大全Code.cpp(190): parsing: void _fastcall TForm1::Button12Click(TObject *)
ccrun.com 2017-03-10
  • 打赏
  • 举报
回复
#include <Excel_xp.h>
...
sheet.OlePropertyGet("Cells", 1, 1).OlePropertyGet("Font")
	.OlePropertySet("Underline", XlUnderlineStyle::xlUnderlineStyleSingle);
...
u012522824 2017-03-10
  • 打赏
  • 举报
回复
明白了
zcw1967 2017-03-10
  • 打赏
  • 举报
回复
应该: #include "Excel_2K.h" 或者 // #include "Excel_XP.h"
ccrun.com 2017-03-09
  • 打赏
  • 举报
回复
建议 GetActiveOleObject 配合 CreateOleObject 一起使用。 如果你不想在工程中包含Excel_2k/xp的头文件,直接写数值也可以,xlUnderlineStyleSingle 的值是2:
Cell.OlePropertySet("Underline", 2); // xlUnderlineStyleSingle
u012522824 2017-03-09
  • 打赏
  • 举报
回复
感谢妖哥,今天发现是别处执行了这行代码后出现问题: Worksheets = Excel.OlePropertyGet("Worksheets"); 把此行代码删除就没有问题了。 另,很惭愧,xlUnderlineStyleSingle似乎是常量不是枚举。 想在工程中包含Excel_2k/xp的头文件,请问如何做? 想结识妖哥,这是我的QQ:3196472129,愿意的话请妖哥加我!

703

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder ActiveX/COM/DCOM
社区管理员
  • ActiveX/COM/DCOM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧