【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

相关推荐
Felix_One18 小时前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
blasit4 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
郑州光合科技余经理9 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1239 天前
matlab画图工具
开发语言·matlab
dustcell.9 天前
haproxy七层代理
java·开发语言·前端
norlan_jame9 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone9 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054969 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月9 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237179 天前
C语言-数组练习进阶
c语言·开发语言·算法