1.Qt原身库支持打开XLSX文件
QFileInfo fileInfo(strTempFilePath);
QString filePath = fileInfo.filePath();
if (fileInfo.suffix().compare("xlsx") == 0)
{
/// 根据路径获取xlsx文件对象
QXlsx::Document xlsx(filePath);
QXlsx::CellRange range = xlsx.dimension();
int lastRow = range.lastRow();
int lastColumn = range.lastColumn();
QStringList code_nums; code_nums.clear();
QStringList code_name; code_name.clear();
if (lastRow > lastColumn)//行大于列多半是行为字段列为字段值
{
for (int row = range.firstRow(); row <= range.lastRow(); row += 1)
{
QString strKey = xlsx.read(row, m_tColumnField).toString();
QString strValue = xlsx.read(row, m_tColumnField + 1).toString();
}
}
else//列大于行多半是列为字段行为字段值
{
for (int column = range.firstColumn(); column <= range.lastColumn(); column += 1)
{
QString strKey = xlsx.read(m_tRowField, column).toString();
QString strValue = xlsx.read(m_tRowField + 1, column).toString();
}
}
}
2.对于XLS文件Qt源生的库不支持打开,可以通过QAxObject 基于COM系统组件通过本机安装的office或者WPS读取表格内容
if (fileInfo.suffix().compare("xls")