Job-hopping to the new company, began to work the site development, access to new applications. There are many applications that need to export tables to a table, and you can export the table to a file in CSV format. Share with the rest of the crowd;
[CPP]View PlainCopy
- Lass tabletoexcle: public qdialog
- {
- Q_object
- Public
- Tabletoexcle (Qwidget *parent = 0, Qt::wflags flags = 0);
- ~tabletoexcle ();
- Private
- Ui::tabletoexcleclass UI;
- Private Slots:
- void Addrowslot ();
- void Delrowslot ();
- void Exportslot ();
- };
[CPP]View PlainCopy
- Tabletoexcle::tabletoexcle (Qwidget *parent, qt::wflags flags)
- : Qdialog (parent, flags)
- {
- UI.SETUPUI (this);
- Ui.m_ptable->setcolumncount (4);
- Qtablewidgetitem * item = new Qtablewidgetitem ("0");
- Ui.m_ptable->sethorizontalheaderitem (0, item);
- item = New Qtablewidgetitem ("1");
- Ui.m_ptable->sethorizontalheaderitem (1, item);
- item = New Qtablewidgetitem ("2");
- Ui.m_ptable->sethorizontalheaderitem (2, item);
- item = New Qtablewidgetitem ("3");
- Ui.m_ptable->sethorizontalheaderitem (3, item);
- Ui.m_ptable->setselectionbehavior (qabstractitemview::selectrows);
- Connect (ui.m_paddbtn,signal (clicked ()),This,slot (Addrowslot ()));
- Connect (ui.m_pdelbtn,signal (clicked ()),This,slot (Delrowslot ()));
- Connect (ui.m_pexportbtn,signal (clicked ()),This,slot (Exportslot ()));
- }
- Tabletoexcle::~tabletoexcle ()
- {
- }
- void Tabletoexcle::addrowslot ()
- {
- Ui.m_ptable->insertrow (Ui.m_ptable->rowcount ());
- }
- void Tabletoexcle::d elrowslot ()
- {
- int index = Ui.m_ptable->currentrow ();
- if (Index >-1)
- {
- Ui.m_ptable->removerow (index);
- }
- }
- void Tabletoexcle::exportslot ()
- {
- QString FileName = Qfiledialog::getsavefilename (This, tr ("Save file"), "", tr ("file (*.csv)");
- if (!filename.isempty ())
- {
- QFile file (fileName);
- BOOL ret = File.Open (qiodevice::truncate | QIODEVICE::WRITEONLY);
- if (!ret)
- return;
- Qtextstream Stream (&file);
- QString conTents;
- Qheaderview * Header = Ui.m_ptable->horizontalheader ();
- if (header)
- {
- For ( int i = 0; i < Header->count (); i++)
- {
- Qtablewidgetitem *item = Ui.m_ptable->horizontalheaderitem (i);
- if (!item)
- {
- continue;
- }
- ConTents + = Item->text () + ",";
- }
- ConTents + = "\ n";
- }
- For ( int i = 0; i < Ui.m_ptable->rowcount (); i++)
- {
- For ( int j = 0; J < Ui.m_ptable->columncount (); j + +)
- {
- qtablewidgetitem* item = Ui.m_ptable->item (i, j);
- if (!item)
- continue;
- QString str = Item->text ();
- Str.replace (",", "");
- ConTents + = str + ",";
- }
- ConTents + = "\ n";
- }
- Stream << conTents;
- File.close ();
- }
- if (Qmessagebox::yes = = Qmessagebox::information (0,qobject::tr ("File Export"), QString ("file export succeeded, do you want to open the file?") "), Qmessagebox::yes,qmessagebox::no))
- {
- Qsettings settings ("Hkey_local_machine\\software\\microsoft\\office", Qsettings::nativeformat);
- QString Szdefault, szpath;
- BOOL bsuccess;
- Szpath = Settings.value ("12.0/excel/installroot/path"). ToString ();
- if (Szpath.isempty ())
- Szpath = Settings.value ("11.0/excel/installroot/path"). ToString ();
- if (Szpath.isempty ())
- Szpath = Settings.value ("10.0/excel/installroot/path"). ToString ();
- if (Szpath.isempty ())
- Szpath = Settings.value ("9.0/excel/installroot/path"). ToString ();
- if (Szpath.isempty ())
- Szpath = Settings.value ("14.0/excel/installroot/path"). ToString ();
- if (Szpath.isempty ())
- {
- Qmessagebox::information (0, "prompt", "the system does not have Office installed, cannot view the failure report, please install Microsoft Office first.");
- return;
- }
- qprocess * proce = new qprocess;
- QString Szexcelexe = szpath + "Excel.exe";
- QString Szopen = "/safe";
- QString Szdoc = fileName;
- Qstringlist list;
- list<<szdoc;
- if (proce)
- {
- Proce->start (szexcelexe,list);
- proce->waitforstarted (5000); //need to wait for the boot to complete
- }<pre name="code" class="cpp" > delete proce;
http://blog.csdn.net/hai200501019/article/details/37538591
Qtablewidget Export to CSV table