QTableWidget 导出到表格

跳槽到了新的公司,开始苦逼的出差现场开发,接触到了新的应用。有很多应用需要将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 += "\n";  
  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 += "\n";  
  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;  

 
 
 



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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值