QAxObject excel("Excel.Application");
excel.setProperty("Visible", true);
QAxObject *work_books = excel.querySubObject("WorkBooks");
work_books->dynamicCall("Open(const QString&)",
"E:\\test.xlsx");
excel.setProperty("Caption", "Qt Excel");
QAxObject *work_book = excel.querySubObject("ActiveWorkBook");
QAxObject *work_sheets =
work_book->querySubObject("Sheets");
//Sheets也可換用WorkSheets
//刪除工作表(刪除第一個(gè))
QAxObject *first_sheet =
work_sheets->querySubObject("Item(int)", 1);
first_sheet->dynamicCall("delete");
//插入工作表(插入至最后一行)
int sheet_count = work_sheets->property("Count").toInt();
//獲取工作表數(shù)目
QAxObject *last_sheet =
work_sheets->querySubObject("Item(int)", sheet_count);
QAxObject *work_sheet =
work_sheets->querySubObject("Add(QVariant)",
last_sheet->asVariant());
last_sheet->dynamicCall("Move(QVariant)",
work_sheet->asVariant());
work_sheet->setProperty("Name", "Qt Sheet");
//設(shè)置工作表名稱
//操作單元格(第2行第2列)
QAxObject *cell =
work_sheet->querySubObject("Cells(int,int)", 2, 2);
cell->setProperty("Value", "Java C++ C# PHP Perl Python
Delphi Ruby"); //設(shè)置單元格值
cell->setProperty("RowHeight", 50);
//設(shè)置單元格行高
cell->setProperty("ColumnWidth", 30);
//設(shè)置單元格列寬
cell->setProperty("HorizontalAlignment", -4108);
//左對(duì)齊(xlLeft):-4131 居中(xlCenter):-4108
右對(duì)齊(xlRight):-4152
cell->setProperty("VerticalAlignment", -4108);
//上對(duì)齊(xlTop)-4160 居中(xlCenter):-4108
下對(duì)齊(xlBottom):-4107
cell->setProperty("WrapText", true);
//內(nèi)容過(guò)多,自動(dòng)換行
//cell->dynamicCall("ClearContents()");
//清空單元格內(nèi)容
QAxObject* interior =
cell->querySubObject("Interior");
interior->setProperty("Color", QColor(0, 255, 0));
//設(shè)置單元格背景色(綠色)
QAxObject* border =
cell->querySubObject("Borders");
border->setProperty("Color", QColor(0, 0, 255));
//設(shè)置單元格邊框色(藍(lán)色)
QAxObject *font = cell->querySubObject("Font");
//獲取單元格字體
font->setProperty("Name", QStringLiteral("華文彩云"));
//設(shè)置單元格字體
font->setProperty("Bold", true);
//設(shè)置單元格字體加粗
font->setProperty("Size", 20);
//設(shè)置單元格字體大小
font->setProperty("Italic", true);
//設(shè)置單元格字體斜體
font->setProperty("Underline", 2);
//設(shè)置單元格下劃線
font->setProperty("Color", QColor(255, 0, 0));
//設(shè)置單元格字體顏色(紅色)
//設(shè)置單元格內(nèi)容,并合并單元格(第5行第3列-第8行第5列)
QAxObject *cell_5_6 =
work_sheet->querySubObject("Cells(int,int)", 5, 3);
cell_5_6->setProperty("Value", "Java");
//設(shè)置單元格值
QAxObject *cell_8_5 =
work_sheet->querySubObject("Cells(int,int)", 8, 5);
cell_8_5->setProperty("Value", "C++");
QString merge_cell;
merge_cell.append(QChar(3 - 1 + 'A'));
//初始列
merge_cell.append(QString::number(5));
//初始行
merge_cell.append(":");
merge_cell.append(QChar(5 - 1 + 'A'));
//終止列
merge_cell.append(QString::number(8));
//終止行
QAxObject *merge_range =
work_sheet->querySubObject("Range(const QString&)",
merge_cell);
merge_range->setProperty("HorizontalAlignment",
-4108);
merge_range->setProperty("VerticalAlignment", -4108);
merge_range->setProperty("WrapText", true);
merge_range->setProperty("MergeCells", true);
//合并單元格
//merge_range->setProperty("MergeCells", false);
//拆分單元格
//work_book->dynamicCall("Save()");
//保存文件(為了對(duì)比test與下面的test2文件,這里不做保存操作)
work_book->dynamicCall("SaveAs(const QString&)",
"E:\\test2.xlsx");
//另存為另一個(gè)文件
work_book->dynamicCall("Close(Boolean)", false);
//關(guān)閉文件
excel.dynamicCall("Quit(void)"); //退出