bcb6操作word

来源:互联网 发布:ps淘宝修图兼职 编辑:程序博客网 时间:2024/06/05 04:27

  Variant ex,wb,sheet,range;
 try
 {
  ex=CreateOleObject("Excel.Application");//创建应用对象
 }
 catch(...)
 {
  MessageBox(0, "启动Excel出错,可能是没有安装Excel","DBGrid2Excel",MB_OK | MB_ICONERROR);
  return;
 }
 ex.OlePropertySet("Visible",true);//显示excel
 ex.OlePropertyGet("Workbooks").OleProcedure("Add"); // 工作表
 wb=ex.OlePropertyGet("ActiveWorkBook");//创建工作簿对象
 sheet=wb.OlePropertyGet("ActiveSheet");

问题1:现在已经有sheet1,sheet2,sheet3,怎样添加sheet4,5....
问题2:wb.OleFunction("SaveAs","优化前的电压");与sheet.OleFunction("SaveAs","优化前的电压");
      怎么效果一样啊都是给excel文档命名,怎么把sheet1,2,3重命名啊
问题3:ex.OlePropertyGet("workbooks").OleFunction("Add","E:\\result.xls");替换上面的
ex.OlePropertyGet("Workbooks").OleProcedure("Add");运行到此处时发生错误"发生意外"


 
1、增加 sheet:

wb.OlePropertyGet("Sheets").OleFunction("Add", "Sheet4");


2、sheet 重新命名:

由于:sheet 的名称获取采用 sheet.OlePropertyGet("Name");
因此:sheet 的名称设置采用 sheet.OlePropertySet("Name", "优化前的电压");

3、方法不对。


关于OLE操作Excel属性、方法、事件等详细说明,可以看 Office 安装目录内 VBAXL9.CHM 文档。
 百度云技术论坛有奖讨论第一期
对我有用[0] 丢个板砖[0] 引用 | 举报 |
编辑 删除
管理

 
2、sheet重命名没问题。
但是1、
ex.OlePropertySet("Visible",true);
ex.OlePropertyGet("Workbooks").OleProcedure("Add");
wb=ex.OlePropertyGet("ActiveWorkBook");
sheet=wb.OlePropertyGet("ActiveSheet");
wb.OlePropertyGet("Sheets").OleFunction("Add", "Sheet4");//运行到此处提示"发生意外";

 
Variant   ex,wb,sh1; 
  AnsiString   p_name; 
  p_name="d:\\h.xls"; 
  ex=CreateOleObject("Excel.Application"); 
  ex.OlePropertyGet("WorkBooks").OleProcedure("open",p_name.c_str()); 
  ex.OlePropertySet("Visible",(Variant)true); 
  wb=ex.OlePropertyGet("ActiveWorkBook"); 
  wb.OlePropertyGet("worksheets").OleFunction("Add");       //增加工作表 
 
 
不过打开已存在excel或在指定路径下创建excel仍有问题
1、假如E:\优化结果.xls已存在
Variant  ex,wb,sh1; 
  AnsiString  p_name; 
  p_name="d:\\优化结果.xls"; 
  ex=CreateOleObject("Excel.Application"); 
  ex.OlePropertyGet("WorkBooks").OleProcedure("Open",p_name.c_str());//在这仍然"发生意外"
2、假如"优化结果.xls"不存,在E盘新建优化结果.xls
AnsiString  p_name; 
  p_name="d:\\优化结果.xls"; 
  ex=CreateOleObject("Excel.Application"); 
  ex.OlePropertyGet("WorkBooks").OleProcedure("Add",p_name.c_str());//在这仍然"发生意外"

AnsiString p_name="d:\\\\优化结果.xls";

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

 

原创粉丝点击