Answer the question
In order to leave comments, you need to log in
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
I solved it this way:
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;
}
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;
QAxObject *excel = new QAxObject("Excel.Application",this);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question