我怎么不能创建Excel对象

swgfqq 2005-09-20 09:46:38
我上次提问有关Excel表的操作
后来我按照上面的给的方法试着用以下的语句去创建Variant类型的ExcelApp,WorkBook,WorkSheet三个对象
ExcelApp=CreateOleObject("Excel.Application");
WorkBook=ExcelApp.OlePropertyGet("ActiveWorkBook");
WorkSheet=WorkBook.OlePropertyGet("ActiveSheet");
可是执行的时候总是出错,不知道是怎么回事
...全文
379 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xjq2003 2005-09-20
  • 打赏
  • 举报
回复
定义几个私有变量啊
private: // User declarations
Variant Ex,Wb,Sheet,ERange,EBorders;
xjq2003 2005-09-20
  • 打赏
  • 举报
回复
//  并在文件头中包含如下语句:

//#include "Excel_2K_SRVR.h"
//#include

//  在Form1.cpp的文件头中加入

//#pragma link "Excel_2K_SRVR"

//  主要代码如下:
try
{


//取报表文件CardSend.xls的完整目录名
AnsiString ExcelFileName = GetCurrentDir()+"\\导出数据.xls";

if(!FileExists(ExcelFileName))
{
Application->MessageBox("报表模板文件不存在,无法打开!",
"错误",MB_ICONSTOP|MB_OK);
return;
}
//建立Excel的Ole对象Ex
try
{
Ex = Variant::CreateObject("Excel.Application");
}
catch(...)
{
Application->MessageBox("无法启动Excel","错误",MB_ICONSTOP|MB_OK);
return;
}
//设置Excel为不可见
Ex.OlePropertySet("Visible",false);
//打开指定的Excel报表文件。报表文件中最好设定只有一个Sheet。
Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",ExcelFileName.c_str());
Wb = Ex.OlePropertyGet("ActiveWorkBook");
Sheet = Wb.OlePropertyGet("ActiveSheet");//获得当前默认的Sheet

//清空Excel表,这里是用循环清空到第300行。对于一般的表格已经足够了。
AnsiString strRowTemp;
AnsiString strRange;
int iCols,iRows;//记录列数和行数

/*从第三行开始,到第300行止。一般第一行是表标题,第二行是副标题或者制表日期。*/
for(iRows=3;iRows<800;iRows++)
{ //假设只有6列。
for (iCols = 1;iCols < 8; iCols++)
{
//清空行
Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertySet("Value","");
}
//去掉表格边框
strRange = "A"+IntToStr(iRows)+":H"+IntToStr(iRows);//获取操作范围
ERange = Sheet.OlePropertyGet("Range",strRange.c_str());
EBorders = ERange.OlePropertyGet("Borders");//获取边框对象
EBorders.OlePropertySet("linestyle",xlNone);
}

AnsiString strPtrDate; //存放当前日期,作为制表日期
DateSeparator = '-';
ShortDateFormat = "yyyy/m/d";//设置为年/月/日格式

strPtrDate = DateToStr(Date());//取当前日期

AnsiString strYear = strPtrDate.SubString(1,4);
strPtrDate = strPtrDate.SubString(6,strPtrDate.Length()-5);
AnsiString strMonth = strPtrDate.SubString(1,strPtrDate.Pos("-")-1);
AnsiString strDay =
strPtrDate.SubString(strPtrDate.Pos("-")+1,
strPtrDate.Length()-strPtrDate.Pos("-"));
strPtrDate = strYear+"年"+strMonth+"月"+strDay+"日";

AnsiString strData = "daw50AD数据";//报表标题
//将报表标题置于第一行第一列。在此之前,应将报表文件的标题格式设定好。
Sheet.OlePropertyGet("Cells",1,1).OlePropertySet("Value",strData.c_str());
//将制表日期置于表格第二行的右侧。
Sheet.OlePropertyGet("Cells",2,5).OlePropertySet("Value",strPtrDate.c_str());

iRows = 3;//在第三行放置表格的列名
Sheet.OlePropertyGet("Cells",iRows,1).OlePropertySet("Value","测试时间");
Sheet.OlePropertyGet("Cells",iRows,2).OlePropertySet("Value","车辆速度");
Sheet.OlePropertyGet("Cells",iRows,3).OlePropertySet("Value","测试速度");
Sheet.OlePropertyGet("Cells",iRows,4).OlePropertySet("Value","轴数");
Sheet.OlePropertyGet("Cells",iRows,5).OlePropertySet("Value","车辆总重");
Sheet.OlePropertyGet("Cells",iRows,6).OlePropertySet("Value","实际重量");
Sheet.OlePropertyGet("Cells",iRows,7).OlePropertySet("Value","误差(%)");
//Sheet.OlePropertyGet("Cells",iRows,8).OlePropertySet("Value","轴数");
//画表格边框,在A3:F3之间取范围
strRange = "A"+IntToStr(iRows)+":H"+IntToStr(iRows);
ERange = Sheet.OlePropertyGet("Range",strRange.c_str());
EBorders = ERange.OlePropertyGet("Borders");
EBorders.OlePropertySet("linestyle",xlContinuous);
EBorders.OlePropertySet("weight",xlThin);
EBorders.OlePropertySet("colorindex",xlAutomatic);
iRows++;
//从数据库中取数据(略),假设数据集放入Query1中。
Table1->Open();//打开数据集
Table1->First();
//循环取数
while(!Table1->Eof)
{
//循环取字段的数据放到Excel表对应的行列中
iCols=1;

strRowTemp = Table1->FieldByName("Dtime") ->AsString;
Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertySet("Value",strRowTemp.c_str());
iCols++;
strRowTemp = Table1->FieldByName("speed") ->AsString;
Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertySet("Value",strRowTemp.c_str());
iCols++;
strRowTemp = Table1->FieldByName("vehspeed") ->AsString;
Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertySet("Value",strRowTemp.c_str());
iCols++;
strRowTemp = Table1->FieldByName("axlenum") ->AsString;
Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertySet("Value",strRowTemp.c_str());
iCols++;
strRowTemp = Table1->FieldByName("totalweigh") ->AsString;
Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertySet("Value",strRowTemp.c_str());
iCols++;

/* strRowTemp = Table1->FieldByName("") ->AsString;
Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertySet("Value",strRowTemp.c_str());
iCols++;
*/

//画该行的表格边框
strRange = "A"+IntToStr(iRows)+":H"+IntToStr(iRows);
ERange = Sheet.OlePropertyGet("Range",strRange.c_str());
EBorders = ERange.OlePropertyGet("Borders");
EBorders.OlePropertySet("linestyle",xlContinuous);
EBorders.OlePropertySet("weight",xlThin);
EBorders.OlePropertySet("colorindex",xlAutomatic);
iRows++;
Table1->Next();
}//while结束

Wb.OleProcedure("Save");//保存表格
Wb.OleProcedure("Close");//关闭表格
Ex.OleFunction("Quit");//退出Excel
//定义目标文件名
AnsiString DestinationFile =GetCurrentDir()+"\\table.xls";
//将刚刚修改的Excel表格文件table.xls拷贝到report目录下
if(!CopyFile(ExcelFileName.c_str(),DestinationFile.c_str(),false))
{
Application->MessageBox("复制文件操作失败,Excel文件可能正在使用中!",
"错误",MB_ICONSTOP|MB_OK);
return;
}
Application->MessageBox("成功完成报表保存!\n可以按\'打开Excel文件\'按钮进行报表工作","提示",MB_ICONINFORMATION|MB_OK);


}//try结束
catch(...)
{
Application->MessageBox("操作Excel表格失败!",
"错误",MB_ICONSTOP|MB_OK);
Wb.OleProcedure("Close");
Ex.OleFunction("Quit");

}
wf2091139 2005-09-20
  • 打赏
  • 举报
回复
实在不行改用ODBC吧
ccrun.com 2005-09-20
  • 打赏
  • 举报
回复
晕。具体出什么错,能不能多打几个字一下说明白?
xibingwuqing 2005-09-20
  • 打赏
  • 举报
回复
也可能WorkBook=ExcelApp.OlePropertyGet("ActiveWorkBook");时,activeworkbook为空,所以WorkSheet=WorkBook.OlePropertyGet("ActiveSheet");找不到activesheet了
swgfqq 2005-09-20
  • 打赏
  • 举报
回复
在创建ExcelApp和WorkBook时都没有问题,但是当我用上面的方法在创建WorkSheet时就会出现报出“未知错误”的信息。也不知道是怎么回事
swgfqq 2005-09-20
  • 打赏
  • 举报
回复
我在运行程序时,系统就只有报出是“未知错误”其他的也没有说
xibingwuqing 2005-09-20
  • 打赏
  • 举报
回复
WorkBook=ExcelApp.OlePropertyGet("ActiveWorkBook");
可能WorkBook还没有创建,所以不能用Active,可以改为WorkBook=(ExcelApp.OlePropertyGet("WorkBooks")).OleFunction("Add");
另外,可以加上ex.OlePropertySet("Visible",true); 使excel显示出来

13,825

社区成员

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

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