QTableView与Excel之间的文件打开与保存

2020-05-26 16:00:32来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

QTableView与Excel之间的文件打开与保存

设置与制作TableView表

 1 QStringList displayItems;
 2 displayItems << "Init Status"  \
 3                  << "Recv Byte 1 / Bit" << "Recv Byte 2 / Bit" << "Recv Byte 3 / Bit" << "Recv Byte 4 / Bit" \
 4                  << "Recv Byte 5 / Bit" << "Recv Byte 6 / Bit" << "Recv Byte 7 / Bit" << "Recv Byte 8 / Bit";  
 5 
 6 QStandardItemModel* model = new QStandardItemModel;
 7 model -> setColumnCount(displayItems.count());
 8 model -> setHorizontalHeaderLabels(displayItems);
 9 ui -> displaytableView -> setModel(model);
10 ui -> displaytableView -> show();
11 
12 QStandardItem* item0 = new QStandardItem("Success");
13 item0 -> setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
14 model -> setItem(0, 0, item0);

保存到Excel

 1 void MainWindow::on_actionSave_triggered()
 2 {
 3     if (readTemperatureTimer -> isActive())
 4     {
 5 #if LANG_EN
 6         QMessageBox::information(this, "info", "Please stop reading the temperature first!");
 7 #else
 8         QMessageBox::information(this, "提示", "请先停止自动读取温度!");
 9 #endif
10     }
11     else
12     {
13         QDir dir("files");
14         if (!dir.exists())
15         {
16             dir.mkdir("files");
17         }
18         QString fileName = QFileDialog::getSaveFileName(
19                     this,
20             #if LANG_EN
21                     tr("Save File"),
22             #else
23                     tr("保存文件"),
24             #endif
25                     "",
26                     "Comma separated value(*.csv)");
27         QFile file(fileName);
28         bool ret = file.open(QIODevice::Truncate | QIODevice::ReadWrite);
29         if (!ret)
30         {
31             qDebug() << "open failure";
32             return;
33         }
34         QTextStream stream(&file);
35         QString conTents;
36         // 写入头
37         QHeaderView * header = ui -> displaytableView -> horizontalHeader();
38         if (NULL != header)
39         {
40             for (int i = 0; i < header->count(); i++)
41             {
42                 QStandardItem* item = model -> horizontalHeaderItem(i);
43 
44                 if (NULL != item)
45                 {
46                     conTents += item -> text() + ",";
47                 }
48             }
49             conTents += "\n";
50         }
51 
52         // 写内容
53         for (int row = 0; row < model -> rowCount(); row++)
54         {
55             for (int column = 0; column < model -> columnCount(); column++)
56             {
57                 QStandardItem* item = model -> item(row, column);
58                 if (NULL != item )
59                 {
60                     QString str;
61                     str = item -> text();
             conTents += str + ","; 65 } 66 } 67 conTents += "\n"; 68 } 69 stream << conTents; 70 71 file.close(); 72 #if LANG_EN 73 QMessageBox::information(this, "info", "File saved successfully!"); 74 #else 75 QMessageBox::information(this, "提示", "文件保存成功!"); 76 #endif 77 } 78 }

从Excel打开

 1 void MainWindow::on_actionOpen_file_triggered()
 2 {
 3     model -> removeRows(0, model -> rowCount());
 4 
 5     QString fileName;
 6     fileName=QFileDialog::getOpenFileName(this,
 7                                       #if LANG_EN
 8                                           tr("Open files"),
 9                                       #else
10                                           tr("打开文件"),
11                                       #endif
12                                           "",
13                                           "Comma Separated Value(*.csv)");
14     if(fileName.isNull())
15         return;
16 
17     QFile openDataFile(fileName);
18     if (!openDataFile.open(QIODevice::ReadOnly))
19     {
20         qDebug() << "Open file error";
21         return;
22     }
23     qDebug() << "open successful...";
24     openDataFile.readLine();
25     int row = 0;
26     while(!openDataFile.atEnd())
27     {
28         QString tmp = openDataFile.readLine();
29         QStringList tmpList = tmp.split(",");
30         tmpList.removeLast();
31 
32         QStandardItem* item0 = new QStandardItem(tmpList.at(0));
33         item0 -> setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
34         model -> setItem(row, 0, item0);
35         QStandardItem* itemByte2[8];
36         for(int x = 0; x < 8; x++)
37         {
38             itemByte2[x] = new QStandardItem(tmpList.at(x + 1));
39             itemByte2[x] -> setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
40             model->setItem(row, x + 1, itemByte2[x]);
41         }
43         row++;
44     }
45     openDataFile.close();
47 }

 


原文链接:https://www.cnblogs.com/fabric-summoner/p/12956166.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:bzoj3569 DZY Loves Chinese II

下一篇:Qt做Tcp数据传输