• QTableWidget 导出到csv表格


      跳槽到了新的公司,开始苦逼的出差现场开发,接触到了新的应用。有很多应用需要将Table导出成表格,可以把table导出成csv格式的文件。跟大伙分享一下;

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. lass TableToExcle : public QDialog  
    2. {  
    3.     Q_OBJECT  
    4.   
    5. public:  
    6.     TableToExcle(QWidget *parent = 0, Qt::WFlags flags = 0);  
    7.     ~TableToExcle();  
    8.   
    9. private:  
    10.     Ui::TableToExcleClass ui;  
    11.     private slots:  
    12.         void addRowSlot();  
    13.         void delRowSlot();  
    14.         void exportSlot();  
    15. };  
    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. TableToExcle::TableToExcle(QWidget *parent, Qt::WFlags flags)  
    2.     : QDialog(parent, flags)  
    3. {  
    4.     ui.setupUi(this);  
    5.     ui.m_pTable->setColumnCount(4);  
    6.      QTableWidgetItem * item = new QTableWidgetItem("0");   
    7.     ui.m_pTable->setHorizontalHeaderItem ( 0, item );  
    8.     item = new QTableWidgetItem("1");   
    9.     ui.m_pTable->setHorizontalHeaderItem ( 1, item );  
    10.     item = new QTableWidgetItem("2");   
    11.     ui.m_pTable->setHorizontalHeaderItem ( 2, item );  
    12.     item = new QTableWidgetItem("3");   
    13.      ui.m_pTable->setHorizontalHeaderItem ( 3, item );  
    14.     ui.m_pTable->setSelectionBehavior(QAbstractItemView::SelectRows);  
    15.     connect(ui.m_pAddBtn,SIGNAL(clicked()),this,SLOT(addRowSlot()));  
    16.     connect(ui.m_pDelBtn,SIGNAL(clicked()),this,SLOT(delRowSlot()));  
    17.     connect(ui.m_pExportBtn,SIGNAL(clicked()),this,SLOT(exportSlot()));  
    18. }  
    19.   
    20. TableToExcle::~TableToExcle()  
    21. {  
    22.   
    23. }  
    24.   
    25. void TableToExcle::addRowSlot()  
    26. {  
    27.    ui.m_pTable->insertRow(ui.m_pTable->rowCount());  
    28.   
    29. }  
    30.   
    31. void TableToExcle::delRowSlot()  
    32. {  
    33.    int index = ui.m_pTable->currentRow ();  
    34.    if (index > -1)  
    35.    {  
    36.        ui.m_pTable->removeRow(index);  
    37.    }  
    38. }  
    39.   
    40. void TableToExcle::exportSlot()  
    41. {  
    42.     QString fileName = QFileDialog::getSaveFileName(this, tr("Save File")," ",tr("file (*.csv)"));  
    43.     if (!fileName.isEmpty())  
    44.     {  
    45.         QFile file(fileName);  
    46.         bool ret = file.open( QIODevice::Truncate | QIODevice::WriteOnly);  
    47.         if(!ret)  
    48.             return;  
    49.   
    50.         QTextStream stream(&file);  
    51.         QString conTents;                  
    52.         QHeaderView * header = ui.m_pTable->horizontalHeader() ;  
    53.         if (header)  
    54.         {  
    55.             for ( int i = 0; i < header->count(); i++ )  
    56.             {  
    57.                 QTableWidgetItem *item = ui.m_pTable->horizontalHeaderItem(i);  
    58.                 if (!item)  
    59.                 {  
    60.                     continue;  
    61.                 }  
    62.                 conTents += item->text() + ",";  
    63.             }  
    64.             conTents += " ";  
    65.         }  
    66.   
    67.         for ( int i = 0 ; i < ui.m_pTable->rowCount(); i++ )  
    68.         {  
    69.             for ( int j = 0; j < ui.m_pTable->columnCount(); j++ )  
    70.             {  
    71.   
    72.                 QTableWidgetItem* item = ui.m_pTable->item(i, j);  
    73.                 if ( !item )  
    74.                     continue;  
    75.                 QString str = item->text();  
    76.                 str.replace(","," ");  
    77.                 conTents += str + ",";  
    78.             }  
    79.             conTents += " ";  
    80.         }  
    81.         stream << conTents;  
    82.         file.close();  
    83.     }  
    84.     if( QMessageBox::Yes == QMessageBox::information(0,QObject::tr("文件导出"),QString("文件导出成功,是否打开该文件?"),QMessageBox::Yes,QMessageBox::No) )  
    85.     {  
    86.    
    87.         QSettings settings("HKEY_LOCAL_MACHINE\Software\Microsoft\Office",QSettings::NativeFormat);   
    88.         QString szDefault, szPath;  
    89.         bool bSuccess;  
    90.         szPath = settings.value("12.0/Excel/InstallRoot/Path").toString();  
    91.         if (szPath.isEmpty())  
    92.             szPath = settings.value("11.0/Excel/InstallRoot/Path").toString();  
    93.         if (szPath.isEmpty())  
    94.             szPath = settings.value("10.0/Excel/InstallRoot/Path").toString();  
    95.         if (szPath.isEmpty())  
    96.             szPath = settings.value("9.0/Excel/InstallRoot/Path").toString();  
    97.         if (szPath.isEmpty())  
    98.             szPath = settings.value("14.0/Excel/InstallRoot/Path").toString();  
    99.   
    100.         if (szPath.isEmpty())  
    101.         {  
    102.             QMessageBox::information(0, "提示", "系统没有安装Office, 不能查看故障报告,请您先安装Microsoft Office.");  
    103.             return;  
    104.         }  
    105.   
    106.         QProcess * proce = new QProcess;  
    107.         QString szExcelexe = szPath + "excel.exe";  
    108.         QString szopen = "/safe";  
    109.         QString szDoc = fileName;  
    110.         QStringList list;  
    111.         list<<szDoc;  
    112.         if( proce )  
    113.         {  
    114.             proce->start(szExcelexe,list);  
    115.              proce->waitForStarted(5000);        //需要等待完成启动  
    116.         }<pre name="code" class="cpp">               delete proce;  
    
    

    http://blog.csdn.net/hai200501019/article/details/37538591

  • 相关阅读:
    2017 Multi-University Training Contest 2.Balala Power!(贪心)
    2017ICPCECIC C.A math problem(高次剩余)
    Atcoder 068E
    51nod 1385 凑数字(贪心+构造)
    cf round #418 div2 D. An overnight dance in discotheque(贪心)
    cf round #418 div2 C. An impassioned circulation of affection(暴力)
    cf round #424 div2 E. Cards Sorting(线段树)
    Atcoder 077E
    hdu 6162 Ch’s gift(树链剖分+主席树)
    Educational Codeforces Round 26 D. Round Subset(dp)
  • 原文地址:https://www.cnblogs.com/findumars/p/5176084.html
Copyright © 2020-2023  润新知