使用 QAxObject 计算合并单元格的行数
Count rows for merged cells using QAxObject
我需要读取包含许多合并单元格的 Excel 文件。假设第一列包含 'Category' 值,第二列包含 'sub_category' 值。问题是每个类别单元格可能是第二列中有多个 sub_categories 的合并单元格。我需要知道此合并单元格占用的行数 。
我的图表是这样的:
已经有人问过类似的问题,但语言不同:
- Count rows for merged cells
- Count rows inside merged cells using Python
QString category = "";
QString sub_category = "";
auto excel = new QAxObject("Excel.Application");
auto workbooks = excel->querySubObject("Workbooks");
auto workbook = workbooks->querySubObject("Open(const QString&)" ,"C:\Users\Orkideh\Documents\build-MyApp-Desktop_Qt_5_12_9_MSVC2017_64bit-Debug\Book1.xlsx");
auto sheets = workbook->querySubObject("Worksheets");
auto sheet = sheets->querySubObject("Item(int)", 1);
// Read the rows 2..150
for (int r = 2; (r <= 150); ++r)
{
auto cCell = sheet->querySubObject("Cells(int,int)",r,1);
category = cCell->dynamicCall("Value()").toString().simplified();
// if each row in the excel file had only 1 category and 1 sub-category,
// I could use the below code to get sub_category:
// cCell = sheet->querySubObject("Cells(int,int)",r, 2);
// sub_category = cCell->dynamicCall("Value()").toString().simplified();
// But in fact, the cell with category value might be a merged cell with
// unknown number of rows.
// I need to know the number of rows that this merged cell occupies to connect
// each sub_category to its own category.
}
我终于从这里得到了答案:
https://blog.csdn.net/u18004660201/article/details/80350164
可以获取目标单元格的首行行号和下一个单元格的首行行号。通过获取单元格的 'range' 和 'MergeArea'.
是可行的
for (int row = 2; row <= 150; ++row)
{
QAxObject *range = sheet->querySubObject("Cells(int,int)", row, 1);
range = range->querySubObject("MergeArea");
const int nRowStart = range->property("Row").toInt();
const int nRowEnd = range->property("Column").toInt();
range = sheet->querySubObject("Cells(int,int)", nRowStart, nRowEnd);
cout << "Value: " << range->property("Value").toString() << endl
<< "The first line of this cell: " << nRowStart << endl << endl;
// Do some other tasks...
}
此代码读取所有行但不再打印 null 作为值
第一列中的合并单元格。
nRowStart 保存第一行的编号。现在,我可以设置一个计数器并增加它,直到 nRowStart 的值变新,这意味着我已经到达下一个单元格。那个计数器的值就是我需要的。
我需要读取包含许多合并单元格的 Excel 文件。假设第一列包含 'Category' 值,第二列包含 'sub_category' 值。问题是每个类别单元格可能是第二列中有多个 sub_categories 的合并单元格。我需要知道此合并单元格占用的行数 。 我的图表是这样的:
已经有人问过类似的问题,但语言不同:
- Count rows for merged cells
- Count rows inside merged cells using Python
QString category = "";
QString sub_category = "";
auto excel = new QAxObject("Excel.Application");
auto workbooks = excel->querySubObject("Workbooks");
auto workbook = workbooks->querySubObject("Open(const QString&)" ,"C:\Users\Orkideh\Documents\build-MyApp-Desktop_Qt_5_12_9_MSVC2017_64bit-Debug\Book1.xlsx");
auto sheets = workbook->querySubObject("Worksheets");
auto sheet = sheets->querySubObject("Item(int)", 1);
// Read the rows 2..150
for (int r = 2; (r <= 150); ++r)
{
auto cCell = sheet->querySubObject("Cells(int,int)",r,1);
category = cCell->dynamicCall("Value()").toString().simplified();
// if each row in the excel file had only 1 category and 1 sub-category,
// I could use the below code to get sub_category:
// cCell = sheet->querySubObject("Cells(int,int)",r, 2);
// sub_category = cCell->dynamicCall("Value()").toString().simplified();
// But in fact, the cell with category value might be a merged cell with
// unknown number of rows.
// I need to know the number of rows that this merged cell occupies to connect
// each sub_category to its own category.
}
我终于从这里得到了答案: https://blog.csdn.net/u18004660201/article/details/80350164
可以获取目标单元格的首行行号和下一个单元格的首行行号。通过获取单元格的 'range' 和 'MergeArea'.
是可行的 for (int row = 2; row <= 150; ++row)
{
QAxObject *range = sheet->querySubObject("Cells(int,int)", row, 1);
range = range->querySubObject("MergeArea");
const int nRowStart = range->property("Row").toInt();
const int nRowEnd = range->property("Column").toInt();
range = sheet->querySubObject("Cells(int,int)", nRowStart, nRowEnd);
cout << "Value: " << range->property("Value").toString() << endl
<< "The first line of this cell: " << nRowStart << endl << endl;
// Do some other tasks...
}
此代码读取所有行但不再打印 null 作为值 第一列中的合并单元格。 nRowStart 保存第一行的编号。现在,我可以设置一个计数器并增加它,直到 nRowStart 的值变新,这意味着我已经到达下一个单元格。那个计数器的值就是我需要的。