在具体操作之前,我们先了解一下excel的层次结构,如图所示,Application对象–>Workbook对象–>Worksheet对象–>Range对象。
1个excel有一个Application对象,1个Application对象有多个workbook对象组成,这些workbook对象由workbooks对象统一管理,workbook对象下包含若干个worksheet,这些worksheet对象由worksheets对象统一管理,worksheet下面的range对象,对应这worksheet里面的表格单元了。
2.具体的操作流程如下:
QAxWidget excel(“Excel.Application”);
1) 显示当前窗口:
excel.setProperty(“Visible”, true);
2) 更改 Excel 标题栏:
excel.setProperty(“Caption”, “Invoke Microsoft Excel”);
3) 添加新工作簿:
QAxObject * workbooks = excel.querySubObject(“WorkBooks”);
workbooks->dynamicCall(“Add”);
4) 打开已存在的工作簿:
workbooks->dynamicCall(“Open (const QString&)”, QString(“c:/test.xls”));
5) 获取活动工作簿:
QAxObject * workbook = excel.querySubObject(“ActiveWorkBook”);
6) 获取所有的工作表:
QAxObject * worksheets = workbook->querySubObject(“WorkSheets”);
7) 获取工作表数量:
int intCount = worksheets->property(“Count”).toInt();
8) 获取第一个工作表:
QAxObject * worksheet = workbook->querySubObject(“Worksheets(int)”, 1);
9) 获取cell的值:
QAxObject * range = worksheet->querySubObject(“Cells(int,int)”, 1, 1 );
3.新建
void ExcelHelper::NewExcel(const QString &fileName)
{
QFile file(fileName);
if (file.exists()) {
m_pWorkBook = m_pWorkBooks->querySubObject("Open(const QString &)", fileName);
}
else {
m_pWorkBooks->dynamicCall("Add");
m_pWorkBook = m_pApplication->querySubObject("ActiveWorkBook");
m_pWorkBook->dynamicCall("SaveAs (const QString&)", fileName);
}
m_pWorkBook->dynamicCall("Close (Boolean)", false);
m_pApplication->dynamicCall("Quit (void)");
}
4.打开
void ExcelHelper::OpenExcel(const QString &fileName)
{
m_pWorkBooks->dynamicCall("Open (const QString&)", fileName);
QAxObject * workbook = m_pApplication->querySubObject("ActiveWorkBook");
QAxObject * worksheets = workbook->querySubObject("WorkSheets");
int intCount = worksheets->property("Count").toInt();
QAxObject * worksheet = workbook->querySubObject("Worksheets(int)", 1);
QAxObject * usedrange = worksheet->querySubObject("UsedRange");
QAxObject * rows = usedrange->querySubObject("Rows");
QAxObject * columns = usedrange->querySubObject("Columns");
int intRowStart = usedrange->property("Row").toInt();
int intColStart = usedrange->property("Column").toInt();
int intCols = columns->property("Count").toInt();
int intRows = rows->property("Count").toInt();
for (int i = intRowStart; i < intRowStart + intRows; i++)
{
for (int j = intColStart; j <= intColStart + intCols; j++)
{
QAxObject * range = worksheet->querySubObject("Cells(int,int)", i, j );
qDebug() << i << j << range->property("Value");
}
}
m_pApplication->setProperty("DisplayAlerts", 0);
workbook->dynamicCall("SaveAs (const QString&)", QDir::toNativeSeparators("c:/xlsbyqt.xls"));
m_pApplication->setProperty("DisplayAlerts", 1);
workbook->dynamicCall("Close (Boolean)", false);
m_pApplication->dynamicCall("Quit (void)");
}
5.使用文章来源:https://www.toymoban.com/news/detail-506202.html
pExcelHelper->OpenExcel(QString("D:/test.xlsx"));
pExcelHelper->NewExcel(QString("D:\\test1.xlsx"));
6.注意新建和打开时的路径写法文章来源地址https://www.toymoban.com/news/detail-506202.html
到了这里,关于QT操作Excel的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!