qt read excel
阅读原文时间:2023年07月11日阅读:1

void exceladapter::readfile(QString filename, QString sheetname, int colNo)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "excelexport");
if(!db.isValid())
{
return;
}
QString dsn = QString("DRIVER={Microsoft Excel Driver (*.xls)};DSN=''; FIRSTROWHASNAMES=1; READONLY=FALSE;CREATE_DB=\"%1\";DBQ=%2").
arg(filename).arg(filename);
db.setDatabaseName(dsn);

if(!db.open())
{
return;
}

QSqlQuery query(db);
query.exec("select * from"+sheetname);
while (query.next())
{
valuelist.append(query.value(colNo));
}
db.close();
}