QTableWidget 导出到表格
跳槽到了新的公司。開始苦逼的出差现场开发。接触到了新的应用。有非常多应用须要将Table导出成表格,能够把table导出成csv格式的文件。
跟大伙分享一下;
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();
};
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::delRowSlot()
{
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("文件导出"),QString("文件导出成功,是否打开该文件?"),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, "提示", "系统没有安装Office, 不能查看故障报告,请您先安装Microsoft Office.");
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); //须要等待完毕启动
}<pre name="code" class="cpp"> delete proce;
浙公网安备 33010602011771号