O
O
Oleg Seledets2018-06-25 23:29:51
Qt
Oleg Seledets, 2018-06-25 23:29:51

How to draw output in excel from qtableview?

Hello, how can I save in excel only the data that is displayed in the table that is shown to us through the TableView. (sorting and screening of data will be used)
used qvarriant to connect

QString nameExc = ui->name_excel_file->text();
                                                            //QAxObject *excel = new QAxObject("Excel.Application",this);
                                                            //QAxObject *wbooks = excel->querySubObject("Workbooks");
                                                            //QAxObject *wbook = wbooks->querySubObject("Open(const QString&)","C:\\q\\"+nameExc+".xls");
    QAxObject *excel = new QAxObject("Excel.Application",this);
    excel->setProperty("Visible", true);                                            //видимость
    QAxObject *wbooks = excel->querySubObject( "Workbooks" );                       //книга
    QAxObject *shab = wbooks->querySubObject("Add()");                              //, "C:\\q\\q1\\q32.xls");    //файл
    QAxObject *mSheets = shab->querySubObject("Sheets");                            //листы
    QAxObject *StatSheet = mSheets->querySubObject("Item(1)");                      //текущий лист
    StatSheet->dynamicCall("Select()");

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Seledets, 2018-06-29
@oleja1ee7

I solved it this way:

header
int rows = ui->tableView->horizontalHeader()->count();
    int columns = ui->tableView->verticalHeader()->count();
    for(int k=1; k<rows;k++)
    {
        QVariant header = ui->tableView->model()->headerData(k, Qt::Horizontal);
        QAxObject *cell = StatSheet->querySubObject("Cells(QVariant,QVariant)", 1, k);
        cell->setProperty("Value", QVariant(header));
        /* 
       тут  65 строчек внешнего оформления (Переносы, цвета, фильтры, границы)
        */
        delete cell;
    }
the cells themselves
for(int i=2; i<=columns+1;i++)//строки
    {
        for(int j=1;j<rows;j++)//столбцы
        {
            QVariant value1 = ui->tableView->model()->data(ui->tableView->model()->index(i-2,j)); //i  - начать с 2 ячейки j - упустить первый столбец
            QAxObject* cell = StatSheet->querySubObject("Cells(QVariant,QVariant)", i, j);
            // вставка значения переменной data (любой тип, приводимый к QVariant) в полученную ячейку
            cell->setProperty("Value", QVariant(value1));
            value1="";
        }

    }
 // освобождение памяти
        delete StatSheet;
        mSheets->dynamicCall("Save()");
        delete mSheets;
        delete shab;//wbook;
        delete wbooks;//wbooks;
        excel->dynamicCall("Quit()");
        ui->name_excel_file->setText("");
        delete excel;

A
Alexander Taratin, 2018-06-26
@Taraflex

QAxObject *excel = new QAxObject("Excel.Application",this);

I'd rather take
https://github.com/tfussell/xlnt
Doesn't require office to be installed on a PC, but it can only xlsx. It won't work with xls.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question