麻烦,用c++Builder读取excel数据出错,请大家帮帮忙
麻烦各位高手,用c++Builder读取excel数据出错,请大家帮帮忙
程序很简单,我想把excel的数据读进c++builder的数组或者数据库中,但是遇到一些问题,我还在上学,才刚上手BCB,希望大家帮帮忙。
运行后显示Isbound()@c:\bcb\emuvcl\utilcl.h/4249;继续运行时报错,错误出现处已注释标出
以下是程序:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "ComObj.hpp"
#include"Utilcls.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------
#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
//---------------------------------------
void __fastcall TForm1::N15Click(TObject *Sender)
{
Variant ExcelApp;
Variant Workbook1;
Variant Sheet1;
ExcelApp.PG("workbooks").FN("open", "G:\project\dungou\data\操作参数.xls"); //提示本行出现错误
Workbook1.PG("Sheets", 1).PR("Select");
Sheet1 = Workbook1.PG("ActiveSheet");
for(int i=2;i<=20;i++){
HS[i-2]=StrToFloat(Sheet1.PG("Cells", i, 1).PG("Value")); //HS dt pm为定义的数组
dt[i-2]=StrToFloat(Sheet1.PG("Cells", i, 2).PG("Value"));
pm[i-2]=StrToFloat(Sheet1.PG("Cells", i, 3).PG("Value"));
};
ExcelApp.Exec(Procedure("Quit"));
ExcelApp=Unassigned;
Workbook1=Unassigned;
Sheet1=Unassigned;
}
能不能帮忙修改一下呢,感激不尽!
------解决方案--------------------
你都没有创建Excel对象的。
程序很简单,我想把excel的数据读进c++builder的数组或者数据库中,但是遇到一些问题,我还在上学,才刚上手BCB,希望大家帮帮忙。
运行后显示Isbound()@c:\bcb\emuvcl\utilcl.h/4249;继续运行时报错,错误出现处已注释标出
以下是程序:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "ComObj.hpp"
#include"Utilcls.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------
#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
//---------------------------------------
void __fastcall TForm1::N15Click(TObject *Sender)
{
Variant ExcelApp;
Variant Workbook1;
Variant Sheet1;
ExcelApp.PG("workbooks").FN("open", "G:\project\dungou\data\操作参数.xls"); //提示本行出现错误
Workbook1.PG("Sheets", 1).PR("Select");
Sheet1 = Workbook1.PG("ActiveSheet");
for(int i=2;i<=20;i++){
HS[i-2]=StrToFloat(Sheet1.PG("Cells", i, 1).PG("Value")); //HS dt pm为定义的数组
dt[i-2]=StrToFloat(Sheet1.PG("Cells", i, 2).PG("Value"));
pm[i-2]=StrToFloat(Sheet1.PG("Cells", i, 3).PG("Value"));
};
ExcelApp.Exec(Procedure("Quit"));
ExcelApp=Unassigned;
Workbook1=Unassigned;
Sheet1=Unassigned;
}
能不能帮忙修改一下呢,感激不尽!
------解决方案--------------------
你都没有创建Excel对象的。
- C/C++ code
try { ExcelApp = Variant::CreateObject("Excel.Application"); } catch(...) { MessageBox(0, "启动 Excel 出错.", "", MB_OK | MB_ICONERROR); return; } // 新建一个工作表 ExcelApp.PG("workbooks").FN("open", WideString("G:\\project\\dungou\\data\\操作参数.xls")); ...
------解决方案--------------------
你的Workbook1没有初始化啊老弟。
- C/C++ code
try { ExcelApp = Variant::CreateObject("Excel.Application"); } catch(...) { MessageBox(0, "启动 Excel 出错.", "", MB_OK | MB_ICONERROR); return; } // 新建一个工作表 ExcelApp.PG("workbooks").FN("open", WideString("G:\\project\\dungou\\data\\操作参数.xls")); Workbook1 = ExcelApp.OlePropertyGet("ActiveWorkbook"); Sheet1 = Workbook1.PG("ActiveSheet"); for (int i = 2; i <= 20; i++) ...
------解决方案--------------------
问题一:前面缺语句。加上下面的代码:
try
{
ExcelApp = Variant::CreateObject(WideString (L"Excel.Application"));
}
catch(...)
{
MessageBox( NULL, L"无法启动 Excel,请检查 Excel程序 是否安装。 "
, L"错误", MB_OK|MB_ICONSTOP );
return;
}
ExcelApp.OlePropertySet( WideString(L"Visible"),false);//这一句可要可不要。
问题二:字符串尽量用WideString,可确保兼容性。
ExcelApp.PG(WideString(L"Workbooks")).FN(WideString(L"open"), WideString(L"d:\\新建 Microsoft Excel 工作表.xls"));
问题三:不要忘了Close
ExcelApp.PG(WideString(L"Workbooks")).FN(WideString(L"close"));
------解决方案--------------------
仔细看了一下,还差一句关键代码:
ExcelApp.PG(WideString(L"Workbooks")).FN(WideString(L"open"), WideString(L"d:\\新建 Microsoft Excel 工作表.xls"));