windows vs cpp qt写excel cell,限制字符串长度,禁止输入,下拉菜单

开发环境:windows vs cpp
用qt导出的excel接口进行excel读写访问

限制excel cell的输入代码:

QString GetExcelCellIndex(const int row_index, const int col_index)
{
    if (row_index < 1 || col_index < 1)//从1开始
    {
        return QString("");
    }
    const int col_ary = 26;//A--Z 26进制
    int remainder = (col_index - 1) % col_ary;//余数
    int quotient = (col_index - 1) / col_ary;//商
    QString col_str = QChar('A' + remainder);
    while (quotient)
    {
        remainder = (quotient - 1) % col_ary;
        quotient = (quotient - 1) / col_ary;
        col_str = QChar('A' + remainder) + col_str;
    }
    return col_str + QString::number(row_index);
}
            Excel::Range *range_cell = sheet->Range(GetExcelCellIndex(
                i + 1,
                j + 1));
            if (range_cell == NULL)
            {
                delete sheet;
                sheet = NULL;
                EventTimeEnd(std::string(log_buf));
                return false;
            }
            Excel::Validation *validation = range_cell->Validation();
            if (validation)
            {
                QString str_list_temp = ",";
                validation->Delete();
                validation->Add(Excel::xlValidateList, Excel::xlValidAlertStop, Excel::xlBetween, QVariant(str_list_temp));
                validation->SetInputMessage(tr("禁止修改表头"));
                validation->SetShowInput(true);
                validation->SetErrorMessage(tr("输入错误"));
                validation->SetShowError(true);
                validation->SetIgnoreBlank(true);
                validation->SetInCellDropdown(false);
                validation->SetIMEMode(Excel::xlIMEModeNoControl);
            }

cell为下拉菜单


                    Excel::Validation *validation = range_cell->Validation();
                    if (validation)//限制输出文件中excel cell的输入值,添加excel的cell下拉菜单。
                    {
                        QStringList str_list = pView->GetCellStringList(index);
                        QString str_list_temp;
                        if (str_list.size() == 0)
                        {
                        }
                        else if (str_list.size() == 1)
                        {
                            if (str_list.at(0).trimmed().length() == 0)
                            {
                            }
                            else
                            {
                                str_list_temp = str_list.at(0).trimmed();
                            }
                        }
                        else
                        {
                            str_list_temp = str_list.join(',').trimmed();
                        }
                        validation->Delete();
                        if (str_list_temp.length() == 0)
                        {
                            str_list_temp = ",";//下拉菜单必须非空,空字符串就是逗号
                        }
                        validation->Add(Excel::xlValidateList, Excel::xlValidAlertStop, Excel::xlBetween, QVariant(str_list_temp));
                        if (str_list_temp == ",")
                        {
                            validation->SetInputMessage(tr("禁止修改"));
                            validation->SetShowInput(true);
                        }
                        validation->SetErrorMessage(tr("输入错误"));
                        validation->SetShowError(true);
                        validation->SetIgnoreBlank(true);
                        validation->SetInCellDropdown(true);
                        validation->SetIMEMode(Excel::xlIMEModeNoControl);
                    }

限制cell的输入字符串的长度:

    if (limit_len > 0)
    {
        Excel::Validation *validation = range_cell->Validation();
        if (validation)
        {
            QString str_list_temp = ",";
            validation->Delete();
            validation->Add(Excel::xlValidateTextLength, Excel::xlValidAlertStop, Excel::xlLess, QVariant(limit_len));
            validation->SetIgnoreBlank(true);
            validation->SetInCellDropdown(true);
            QString str_mess = tr("限制长度%1").arg(limit_len);
            validation->SetInputMessage(str_mess);
            validation->SetShowInput(true);
            validation->SetErrorMessage(str_mess);
            validation->SetShowError(true);
            validation->SetIMEMode(Excel::xlIMEModeNoControl);
        }
        if (str.length() < limit_len)
        {
            range_cell->SetFormulaR1C1(str);//20191126 超长字符串写入excel cell。
        }
    }
    else
    {
        if (limit_len == 0)//无限制
        {
            range_cell->SetFormulaR1C1(str);//20191126 超长字符串写入excel cell。
        }
    }

通用方法:记录宏的方式获得office excel、word…的操作代码,然后根据关键字找到对应接口。
例如限制长度的vb宏代码如下

    With Selection.Validation
    .Delete
    .Add Type:=xlValidateTextLength, AlertStyle:=xlValidAlertStop, _
    Operator:=xlLess, Formula1:="32"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .IMEMode = xlIMEModeNoControl
    .ShowInput = True
    .ShowError = True
    End With

猜你喜欢

转载自blog.csdn.net/weixin_43172531/article/details/105532909
cpp