ole问题,

hailin10 2004-08-16 05:42:21

Variant ExcelApp = Variant::CreateObject("Excel.Application");

ExcelApp.OlePropertySet("Visible", true);


ExcelApp.OlePropertyGet("Workbooks").OleFunction("Open","d:\\Sheet");

ExcelApp.OlePropertyGet("Columns",3).OleProcedure("Select");


Variant Sheet1;
Sheet1=ExcelApp.OlePropertyGet("WorkSheet").OlePropertyGet("ActiveSheet");
//这里出错,这句话有问题,为什么?

ExcelApp.OlePropertyGet("ActiveSheet").OlePropertyGet("Cells",1,3).OleProcedure("Select");

Variant Range=ExcelApp.OlePropertyGet("Selection");//这句有问题,

AnsiString asd="asd";
ExcelApp.OlePropertyGet("ActiveSheet").OlePropertyGet("Cells",1,3).OlePropertySet("Value",asd.c_str());

ShowMessage("Excel has been launched and now is visible");

ExcelApp.OleProcedure("Save"); //修改后保存出错,为什么???
ExcelApp.OleProcedure("Close");// 这句也有问题,为什么???
ExcelApp.OleFunction("Quit");
ExcelApp = Unassigned;
...全文
148 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ynotgo 2004-08-29
  • 打赏
  • 举报
回复
//打开指定的Excel报表文件。报表文件中最好设定只有一个Sheet。
Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",ExcelFileName.c_str());
Wb = Ex.OlePropertyGet("ActiveWorkBook");
Sheet = Wb.OlePropertyGet("ActiveSheet");//获得当前默认的Sheet


Wb.OleProcedure("Save");//保存表格
Wb.OleProcedure("Close");关闭表格
Ex.OleFunction("Quit");退出Excel
constantine 2004-08-29
  • 打赏
  • 举报
回复
look
commars 2004-08-29
  • 打赏
  • 举报
回复
顶!
cchao 2004-08-25
  • 打赏
  • 举报
回复
学习
sunliwen780502 2004-08-17
  • 打赏
  • 举报
回复
我把自己写的代码帖出来,仅供参考.
/*******************************************************************************
* 函数名: ExportDataToExcel
* 输入参数: ado //pointer of TADOQuery
* dia //pointer of TSaveDialog
* grid //pointer of TDBGrid
* k //y coordiate of cells
* Str //title of Excel table
* 输出参数: None
* 返回值: void
* 描述:
*******************************************************************************/
void ExportDataToExcel(TADOQuery *ado, TSaveDialog *dia, TDBGrid *grid, int k, char* Str)
{
Variant ex, newxls, cellms;
AnsiString sFileName;
int row, i, j = 1;

if (dia->Execute())
{
try
{
ex = CreateOleObject("Excel.Application");//启动Excel
ex.OlePropertySet("Visible", (Variant)false);//使Excel不可见
newxls = (ex.OlePropertyGet("Workbooks")).OleFunction("Add");//添加一个工作薄
cellms = newxls.OlePropertyGet("ActiveSheet"); //创建工作区
}

catch (...)
{
ShowMessage("无法启动Excel");
return;
}

ado->Active = true;
ado->First();

row = 1;//在第一行显示标题
//ex.OlePropertyGet("Cells", 1, 6).OlePropertySet("Value", );
ex.OlePropertyGet("Cells", 1, k).OlePropertySet("Value", Str);
cellms = ex.Exec(PropertyGet("Range") << ("A" + IntToStr(row) + ":A" + IntToStr(row)));

for (i = 0; i < ado->FieldCount; i++)
{
//将字段名写到工作薄的第二行
cellms.OlePropertyGet("Cells", 2, i + 1).OlePropertySet("Value", (WideString)grid->Columns->Items[i]->Title->Caption.c_str());
//cellms.OlePropertyGet("Cells", 2, i + 1).OlePropertySet("Value", (WideString)ado->Fields->Fields[i]->FieldName);
}

while (!ado->Eof)//将数据库中的记录依次写到Excel中
{
j = j + 1;

for (i = 0; i < ado->FieldCount; i++)
{
cellms.OlePropertyGet("Cells", j + 1, i + 1).OlePropertySet("Value",(WideString)ado->Fields->Fields[i]->AsString);
}
ado->Next();
}

sFileName = dia->FileName;
newxls.OleProcedure("SaveAs", sFileName.c_str()); //保存Excel文件

ex.OleFunction("Quit");//退出Excel
ex = Unassigned;
newxls = Unassigned;
cellms = Unassigned;
}
}
3996906 2004-08-16
  • 打赏
  • 举报
回复
自己上妖哥站上看吧~~~~~

http://www.ccrun.com/doc/go.asp?id=529
http://www.ccrun.com/doc/go.asp?id=543

13,824

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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