ODBC获取excel内容

//获取配置文件地址
#include 
#include 
#include 

QString strFile = QFileDialog::getOpenFileName(NULL,tr("选择Excel配置文件"),".",tr("Exel file(*.xls *.xlsx)"));
if (strFile.isEmpty())
{
    QMessageBox::about(this, "提示", "获取配置文件失败,请重试");
    return;
}    
//使用ODBC获取
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "excelexport");
if(!db.isValid())
{
     QMessageBox::about(this, "提示", "获取数据库失败,请重试\nerror:"+ db.lastError().text());
     return;
}
QString dsn="Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};Readonly=TRUE;DBQ="+strFile+ ";";
db.setDatabaseName(dsn);
if(!db.open())
{
    QMessageBox::about(this, "提示", "数据库打开失败,请重试\nerror:"+ db.lastError().text());
    return;
}

QSqlQuery query(db);
query.exec("select * from [Sheet1$]");
while (query.next())
{
    int index = query.value(0).toInt();//这里的0指的是在表格中的第一列
}

db.close();    

 

你可能感兴趣的:(ODBC获取excel内容)