QTableView內容保存到Excel

QTableView

QStringList displayItems;
displayItems << "學號" << "姓名" << "語文" << "數學" << "英語";

QStandardItemModel* model = new QStandardItemModel();
model -> setColumnCount(displayItems.count());
model -> setHorizontalHeaderLabels(displayItems);
model -> horizontalHeaderItem(displayItems.count() - 1) -> setTextAlignment(Qt::AlignCenter);

ui -> displaytableView -> setModel(model);
ui -> displaytableView -> setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
ui -> displaytableView -> horizontalScrollBar() -> setSliderPosition(0);

ui -> displaytableView -> setColumnWidth(0, 50);
ui -> displaytableView -> setColumnWidth(1, 100);
ui -> displaytableView -> setColumnWidth(2, 100);
ui -> displaytableView -> setColumnWidth(3, 100);
ui -> displaytableView -> setColumnWidth(4, 100);
ui -> displaytableView -> show();
void MainWindow::on_actionSave_triggered()
{
    QDir dir("files");
    if (!dir.exists())
    {
        dir.mkdir("files");
    }
    QString fileName = QFileDialog::getSaveFileName(
                    this,
                    "Save File",
                    "",
                    "Comma separated value(*.csv)");
    QFile file(fileName);
    bool ret = file.open(QIODevice::Truncate | QIODevice::ReadWrite);
    if(!ret)
    {
        return;
    }
    QTextStream stream(&file);
    QString contents;
    // 寫入頭
    QHeaderView * header = ui -> displaytableView -> horizontalHeader();
    if (NULL != header)
    {
        for (int i = 0; i < header -> count(); i++)
        {
            QStandardItem* item = model -> horizontalHeaderItem(i);
            if (NULL != item)
            {
                contents += item -> text() + ",";
            }
        }
        contents += "\n";
    }

    // 寫內容
    for (int row = 0; row < model -> rowCount(); row++)
    {
        for (int column = 0; column < model -> columnCount(); column++)
        {
            QStandardItem* item = model -> item(row, column);
            if (NULL != item )
            {
                QString str;
                str = item -> text();
                str.replace(",", " ");
                contents += str + ",";
            }
        }
        contents += "\n";
    }
    stream << contents;

    file.close();
    QMessageBox::information(this, "info", "File saved successfully!");
}
void MainWindow::on_actionOpen_file_triggered()
{
    model -> removeRows(0, model -> rowCount());

    QString fileName = QFileDialog::getOpenFileName(
                    this,
                    "Open files",
                    "",
                    "Comma Separated Value(*.csv)");
    QFile openDataFile(fileName);
    if (!openDataFile.open(QIODevice::ReadOnly))
    {
        return;
    }

    openDataFile.readLine();  // 去掉首行
    int row = 0;
    while(!openDataFile.atEnd())
    {
        QString tmp = openDataFile.readLine();
        QStringList tmpList = tmp.split(",");
        tmpList.removeLast();

        QStandardItem* items[5];
        for(int x = 0; x < 5; x++)
        {
            items[x] = new QStandardItem(tmpList.at(x));
            items[x] -> setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            model -> setItem(row, x, items[x]);
        }
        row++;
    }
    openDataFile.close();
}
相關文章
相關標籤/搜索