【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

相关推荐
fdc20173 小时前
Avalonia 基础导航实现:从页面切换到响应式交互全指南
开发语言·javascript·ecmascript
wangwangblog4 小时前
LLVM 数据结构简介
开发语言·数据结构·c++
大可门耳4 小时前
Qt的数据库模块介绍,Qt访问SQLite详细示例
数据库·qt·sqlite
Yeats_Liao4 小时前
Java 软件测试(三):Mockito打桩与静态方法模拟解析
java·开发语言
JAVA学习通4 小时前
RabbitMQ---面试题
java·开发语言
艾菜籽4 小时前
UDP套接字的使用
java·开发语言·网络
云天徽上5 小时前
【数据可视化-111】93大阅兵后的军费开支情况———2024年全球军费开支分析:用Python和Pyecharts打造炫酷可视化大屏
开发语言·python·信息可视化·pyecharts
zhangfeng11335 小时前
错误于make.names(vnames, unique = TRUE): invalid multibyte string 9 使用 R 语言进行数据处理时
开发语言·r语言·生物信息
七夜zippoe5 小时前
缓存三大劫攻防战:穿透、击穿、雪崩的Java实战防御体系(三)
java·开发语言·缓存
郝学胜-神的一滴5 小时前
Linux命令行的核心理念与实用指南
linux·运维·服务器·开发语言·程序人生