QSqlDatabase db = QSqlDatabase::database("ODBC");
QSqlQuery q(db);
QString i_string;
QAxObject* excel = new QAxObject("Excel.Application",NULL);
excel->dynamicCall("SetVisible(bool), FALSE");
QAxObject *workbooks = excel->querySubObject( "Workbooks" ); //得到Workbooks集合的指针
QAxObject *workbook = workbooks->querySubObject( "Open(const QString&)","D:\\BaseStudent.xls"); //打开硬盘上的一个Excel文档
QAxObject *sheets = workbook->querySubObject("sheets"); //得到Sheets对象的指针
//QAxObject *StatSheet = sheets->querySubObject( "Item(const QVariant&)", QVariant("stat") ); //得到名为stat的一个sheet的指针
QAxObject *StatSheet = sheets->querySubObject( "Item(int)", 1 ); //获取工作表集合的工作表1,即sheet1
QAxObject *range;
QString x = "A"+QString::number(2); //设置要操作的单元格,如A2
range = StatSheet->querySubObject("Range(QVariant, QVariant)",x); //选择A2这个range对象
i_string = range->property("Value").toString(); //将该range对象的取出
excel->dynamicCall( " Quit()");//退出
int r = q.exec(QString("insert into BaseStudent(姓名) VALUES('%1')").arg(i_string));
qDebug()<<r;
我想要获取excel中的数据,然后提取出来,接着再存入SQL的表中,可是总提示如下错误,大神帮忙看看啦(以上是核心代码)
Starting D:\qt\holy\debug\holy.exe...
CoCreateInstance failure (????????)
QAxBase::setControl: requested control Excel.Application could not be instantiated
QAxBase::dynamicCallHelper: Object is not initialized, or initialization failed
QAxBase::dynamicCallHelper: Object is not initialized, or initialization failed
C:\Program Files (x86)\SogouInput\6.7.0.0163\
D:\qt\holy\debug\holy.exe exited with code -1073741819