【Qt】QAxObject的简单使用,WPS

这个是电脑上安装了WPS的

//点击按键生成文件

void Widget::on_pushButton_clicked(bool checked)

cpp 复制代码
void Widget::merge(QAxObject *sheet, QString AB, QString name)
{
    // 合并 A2 和 B2 单元格
    QAxObject *range = sheet->querySubObject("Range(const QString&)", AB);  //"A2:B2"
    range->dynamicCall("Merge()");

    // 设置单元格格式
    QAxObject *interior = range->querySubObject("Interior");
    if (interior) {
        interior->dynamicCall("setColorIndex(int)", 1); // 设置颜色索引为 1,即黑色
    }
    QAxObject *font2 = range->querySubObject("Font");
    if (font2) {
        font2->dynamicCall("setColorIndex(int)", 2); // 设置颜色索引为 2,即白色
        font2->setProperty("Name", "SimSun"); // 设置字体为宋体
        font2->setProperty("Size", 12);        // 设置字体大小为20号
        font2->setProperty("Bold", true);      // 设置字体加粗
    }
    range->setProperty("HorizontalAlignment", -4108);
    range->setProperty("VerticalAlignment", -4108);
    range->dynamicCall("SetValue(const QVariant&)", name);   //tr("Level result")
}

void Widget::setBold(QAxObject *sheet, QString name, QString valueName, int col)
{
    QAxObject* cell = sheet->querySubObject("Cells(int, int)", col, 1);
    //cell->setProperty("ColumnWidth",30);
    cell->dynamicCall("SetValue(conts QVariant&)", name);   //   tr("Grade Standards")
    QAxObject* font3 = cell->querySubObject("Font");
    if (font3) {
        font3->setProperty("Name", "SimSun"); // 设置字体为宋体
        //font3->setProperty("Size", 20);        // 设置字体大小为20号
        font3->setProperty("Bold", true);      // 设置字体加粗
    }
    // 设置垂直居中对齐
    cell->setProperty("VerticalAlignment", -4108); // 使用常量值 -4107 表示 xlVAlignCenter

    cell = sheet->querySubObject("Cells(int, int)", col, 2);
    //cell->setProperty("ColumnWidth",30);
    cell->dynamicCall("SetValue(conts QVariant&)", valueName);   //tr("Overall Standards")
    QAxObject* fontScore = cell->querySubObject("Font");
    if (fontScore) {
        fontScore->setProperty("Name", "SimSun"); // 设置字体为宋体
        //font3->setProperty("Size", 20);        // 设置字体大小为20号
        fontScore->setProperty("Bold", true);      // 设置字体加粗
    }
    // 设置垂直居中对齐
    cell->setProperty("VerticalAlignment", -4108);
    cell->setProperty("HorizontalAlignment", -4131); // 设置左对齐
}

void Widget::setData(QAxObject *sheet, QVector<QString> nameVector, QVector<QString> valueVector, int col)
{
    for(int i = 0; i < nameVector.size(); i++)
    {
        QAxObject* cell = sheet->querySubObject("Cells(int, int)", col + i, 1);
        //cell->setProperty("ColumnWidth",30);
        cell->dynamicCall("SetValue(conts QVariant&)", nameVector.at(i));
        // 设置垂直居中对齐
        cell->setProperty("VerticalAlignment", -4108); // 使用常量值 -4107 表示 xlVAlignCenter

        cell = sheet->querySubObject("Cells(int, int)", col + i, 2);
        //cell->setProperty("ColumnWidth",30);
        cell->dynamicCall("SetValue(conts QVariant&)", valueVector.at(i));
        // 设置垂直居中对齐
        cell->setProperty("VerticalAlignment", -4108);
        cell->setProperty("HorizontalAlignment", -4131); // 设置左对齐
    }

}

void Widget::setDataCol(QAxObject *sheet, QString name, int col)
{
    QAxObject* cell = sheet->querySubObject("Cells(int, int)", col, 1);
    //cell->setProperty("ColumnWidth",30);
    cell->dynamicCall("SetValue(conts QVariant&)", name);
    // 设置垂直居中对齐
    cell->setProperty("VerticalAlignment", -4108); // 使用常量值 -4107 表示 xlVAlignCenter
    cell->setProperty("HorizontalAlignment", -4131); // 设置左对齐
}


//点击按键生成文件
void Widget::on_pushButton_clicked(bool checked)
{
    QString filePath = QFileDialog::getSaveFileName(this, tr("save path"),"Info", "Excel(*.xls)");
    if(filePath.isEmpty())
    {
        return ;
    }
    QAxObject *excel = new QAxObject(this);
    excel->setProperty("Visible", false);  //不显示窗体
    if (excel->setControl("Excel.Application"))
    {
    }
    else
    {
        excel->setControl("Ket.Application");  //连接Excel控件
    }

    excel->setProperty("DisplayAlerts", false);
    QAxObject *workbooks = excel->querySubObject("WorkBooks");
    workbooks->dynamicCall("Add");
    QAxObject *workbook = excel->querySubObject("ActiveWorkBook");

    QAxObject* sheet = workbook->querySubObject("WorkSheets(int)", 1);//获取工作表集合的工作表1,即sheet1

    QAxObject* cell = sheet->querySubObject("Cells(int, int)", 1, 1);//型号
    cell->setProperty("ColumnWidth", 20);
    cell->setProperty("RowHeight", 50); // 假设设置为400twips,相当于20磅
    cell->dynamicCall("SetValue(conts QVariant&)", tr("Title")); // 设置单元格的值
    cell->setProperty("HorizontalAlignment", -4108);
    cell->setProperty("VerticalAlignment", -4108);

    cell = sheet->querySubObject("Cells(int, int)", 1, 2);
    cell->setProperty("ColumnWidth", 50);
    cell->dynamicCall("SetValue(conts QVariant&)", tr("Report"));
    //cell->setProperty("HorizontalAlignment", -4131); // 设置左对齐
    //cell->setProperty("VerticalAlignment", -4108);   // 底端对齐
    cell->setProperty("HorizontalAlignment", -4131); // 设置左对齐
    cell->setProperty("VerticalAlignment", -4107);   // 设置底端对齐
    // 设置字体
    QAxObject* font = cell->querySubObject("Font");
    if (font) {
        font->setProperty("Name", "SimSun"); // 设置字体为宋体
        font->setProperty("Size", 20);        // 设置字体大小为20号
        font->setProperty("Bold", true);      // 设置字体加粗
    }

    merge(sheet, "A2:B2", "merge");
    setBold(sheet, "ONE", "TWO", 3);
    setData(sheet, m_levelResult_name, m_levelResult_value, 4);

    setDataCol(sheet, "Time: 2024/05/27 10:53:06:071", 8);

//    // 获取已使用的单元格范围
//    QAxObject *rangeUsed = sheet->querySubObject("UsedRange");
//    // 设置已使用的单元格范围以内的单元格为灰色
//    QAxObject *interiorUsed = rangeUsed->querySubObject("Interior");   //exterior    Interior
//    if (interiorUsed) {
//        interiorUsed->dynamicCall("setColorIndex(int)", 15); // 设置颜色索引为 15,即灰色
//    }

    // 获取已使用的单元格范围
    QAxObject *usedRange = sheet->querySubObject("UsedRange");
    if (!usedRange) {
        qWarning("Failed to get used range.");
        return;
    }
    // 设置边框样式
    QAxObject *borders = usedRange->querySubObject("Borders");
    if (!borders) {
        qWarning("Failed to get borders.");
        return;
    }
    // 应用边框
    usedRange->dynamicCall("BorderAround(const QVariant&)", QVariant(6)); // 6 代表边框颜色

    workbook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(filePath));//保存至fileName
    workbook->dynamicCall("Close()");//关闭工作簿
    excel->dynamicCall("Quit()");//关闭excel
    delete excel;

    // 设置单元格格式
//    QAxObject *interior = range->querySubObject("Interior");
//    if (interior) {
//        interior->dynamicCall("setColorIndex(int)", 1); // 设置颜色索引为 1,即黑色
//    }
//    QAxObject *font2 = range->querySubObject("Font");
//    if (font2) {
//        font2->dynamicCall("setColorIndex(int)", 2); // 设置颜色索引为 2,即白色
//        font2->setProperty("Name", "SimSun"); // 设置字体为宋体
//        font2->setProperty("Size", 12);        // 设置字体大小为20号
//        font2->setProperty("Bold", true);      // 设置字体加粗
//    }
//    range->setProperty("HorizontalAlignment", -4108);
//    range->setProperty("VerticalAlignment", -4108);
//    range->dynamicCall("SetValue(const QVariant&)", tr("Level result"));
}

参考文章:
https://blog.csdn.net/2401_84263282/article/details/137776485

https://blog.csdn.net/hitzsf/article/details/117174007

相关推荐
微学AI8 小时前
一根针指向所有方向:挂谷猜想对 LLM Agent 技能-记忆架构的启示
开发语言·人工智能·架构·挂谷猜想
豆瓣鸡8 小时前
算法日记 - Day3
java·开发语言·算法
hPw0eKIqD8 小时前
使用二次封装的Excel COM 组件操作Excel\WPS ET中的区域、行和列
excel·wps
韭菜炒鸡肝天9 小时前
VTK开发笔记(一):VTK介绍,Qt..+VSx+VTK.编译
开发语言·笔记·qt
Aaron - Wistron9 小时前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
刀法孜然10 小时前
QT5.12.8开发中使用触摸屏,配置的环境变量,imx6ullpro开发板qt开发
qt
Dxy123931021610 小时前
Python项目打包成EXE完整教程(PyInstaller实战避坑)
开发语言·python
05664611 小时前
Python康复训练——常用标准库
开发语言·python·学习
骊城英雄11 小时前
基于C#+avalonia ui实现的跨平台点胶机灌胶监控控制上位机软件
开发语言·ui·c#
吾儿良辰11 小时前
一个被BCL遗忘的高性能集合:C# CircularBuffer<T>深度解析
开发语言·windows·c#