void ImportChooseDialog::on_imdorButton_clicked()[pre]{[/pre][pre] QString path3 = ui->dorpathlineEdit->text();[/pre][pre] QAxWidget excel("Excel.Application");[/pre][pre] excel.setProperty("Visible", true);[/pre][pre] //显示当前窗口[/pre][pre] QAxObject * workbooks = excel.querySubObject("WorkBooks");[/pre][pre] workbooks->dynamicCall("Open (const QString&)",QString(path3));[/pre][pre] //打开已存在的工作簿C:/data/book1.xls[/pre][pre] QAxObject * workbook = excel.querySubObject("ActiveWorkBook");[/pre][pre] //获取活动工作簿[/pre][pre] QAxObject * worksheets = workbook->querySubObject("WorkSheets");[/pre][pre] //获取所有工作表[/pre][pre] int intCount = worksheets->property("Count").toInt();[/pre][pre] //获取工作表数量[/pre][pre] if(createConnection3())[/pre][pre] {[/pre][pre] for (int i = 1; i <= intCount; i++)[/pre][pre] {[/pre][pre] QString sheetname;[/pre][pre] QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", i);[/pre][pre] //获取第i个工作表[/pre][pre] sheetname = worksheet->property("Name").toString();[/pre][pre] //获取工作表名[/pre]
[pre] QAxObject * usedrange = worksheet->querySubObject("UsedRange");[/pre][pre] //工作表中的已用区域[/pre][pre] QAxObject * rows = usedrange->querySubObject("Rows");[/pre][pre] //行[/pre][pre] int intRowStart = usedrange->property("Row").toInt();//开始行[/pre][pre] int intRows = rows->property("Count").toInt();//行数[/pre][pre] QSqlQuery query;[/pre][pre] query.exec("create table "+sheetname+" (num varchar(10) not null,"[/pre][pre] "student_num varchar(30) not null primary key,"[/pre][pre] "name varchar(30) not null,"[/pre][pre] "dorm varchar(10) not null)");[/pre]
[pre] for (int j = intRowStart+1; j < intRowStart + intRows; j++)[/pre][pre] {[/pre][pre] QAxObject * range1 = worksheet->querySubObject("Cells (int,int)",j,1);[/pre][pre] QAxObject * range2 = worksheet->querySubObject("Cells (int,int)",j,2);[/pre][pre] QAxObject * range3 = worksheet->querySubObject("Cells (int,int)",j,3);[/pre][pre] QAxObject * range4 = worksheet->querySubObject("Cells (int,int)",j,4);[/pre][pre] QString cell1 = range1->property("Value").toString();[/pre][pre] QString cell2 = range2->property("Value").toString();[/pre][pre] QString cell3 = range3->property("Value").toString();[/pre][pre] QString cell4 = range4->property("Value").toString();[/pre]
[pre] qDebug() << cell1 << cell2 << cell3 << cell4 ;[/pre][pre] QString sheetname2 = worksheet->property("Name").toString();[/pre][pre] QSqlQuery q;[/pre][pre] q.exec("insert into "+sheetname2+" (num,student_num,name,dorm)"[/pre][pre] "values ('"+cell1+"','"+cell2+"','"+cell3+"','"+cell4+"')");[/pre]
[pre] q.exec();[/pre][pre] }[/pre][pre] }[/pre]
[pre] }[/pre]
[pre] workbook->dynamicCall("Close (Boolean)", false);[/pre][pre] excel.dynamicCall("Quit (void)");[/pre]
[pre] delete workbook;[/pre][pre] delete workbooks;[/pre][pre] delete worksheets;[/pre]
[pre]}[/pre][pre]
[/pre][pre]点击按钮,数据读取正常,插入数据可正常,但是非正常退出,excel进程不结束,各位帮我看看最后应该怎么写,关闭excel进程啊[/pre]