如何将 QTable 导出到 .csv?
How to export QTable to .csv?
如何将值从 QTableWidget
导出到 .csv 文件或 Excel 文件?
程序应该从 PLC 收集三个变量的值。这个PLC是我公司的,有采集功能。
正在将项目插入 QTableWidget
.cpp:
void MainWindow::on_pushInsert_clicked()
{
/*
the program already is connected to plc,
so I just have to choose the address inside of the plc.
*/
WORD wAddress = 50001;
WORD value1 = 0;
WORD value2 = 0;
WORD value3 = 0;
CollectVariables(wAddress,wValue);
int ID = 0;
string Name = "";
int Age = 0;
QTableWidget *table = ui->tableWidget;
for(i=0;i<10; i++) {
//id
CollectVariables(wAddress+i, value1);
table->setItem(i, 0, new QTableWidgetItem(value1));
//name
CollectVariables(wAddress+10+i, value2);
table->setItem(i, 1, new QTableWidgetItem(value2));
//age
CollectVariables(wAddress+20+i, value3);
table->setItem(i, 2, new QTableWidgetItem(value3));
tabela->selectRow(i);
}
}
我以前将 QTableWidget
“导出”到 .csv 文件(我无法导出 excel)的方法是将每个 space 的值存储在 table 到数组,同时将项目插入到 table.
QFile
用于创建 .csv 文件,QTextStream
用于在其上插入值。
为了改进代码,我创建了一个按钮,指向 select 保存此 .csv 文件的目录。
Select 目录.cpp:
void MainWindow::on_pushSelectDir_clicked()
{
QFileDialog directory;
ui->lineEditDir->setText(directory.getSaveFileName(this,"Choose an directory"));
dir_name = ui->lineEditDir->text();
}
创建 .csv 文件 .cpp:
void MainWindow::on_pushExportCSV_clicked()
{
QFile data(dir_name+".csv");
if (data.open(QFile::WriteOnly | QIODevice::Append)) {
}
QTextStream output(&data);
output <<"ID"<<","<<"Name"<<","<<"Age"<< '\n';
for(i=0;i<10; i++) {
output << ID[i] <<","<< Name[i] <<","<< Age[i] << '\n';
}
data.close();
}
如何将值从 QTableWidget
导出到 .csv 文件或 Excel 文件?
程序应该从 PLC 收集三个变量的值。这个PLC是我公司的,有采集功能。
正在将项目插入 QTableWidget
.cpp:
void MainWindow::on_pushInsert_clicked()
{
/*
the program already is connected to plc,
so I just have to choose the address inside of the plc.
*/
WORD wAddress = 50001;
WORD value1 = 0;
WORD value2 = 0;
WORD value3 = 0;
CollectVariables(wAddress,wValue);
int ID = 0;
string Name = "";
int Age = 0;
QTableWidget *table = ui->tableWidget;
for(i=0;i<10; i++) {
//id
CollectVariables(wAddress+i, value1);
table->setItem(i, 0, new QTableWidgetItem(value1));
//name
CollectVariables(wAddress+10+i, value2);
table->setItem(i, 1, new QTableWidgetItem(value2));
//age
CollectVariables(wAddress+20+i, value3);
table->setItem(i, 2, new QTableWidgetItem(value3));
tabela->selectRow(i);
}
}
我以前将 QTableWidget
“导出”到 .csv 文件(我无法导出 excel)的方法是将每个 space 的值存储在 table 到数组,同时将项目插入到 table.
QFile
用于创建 .csv 文件,QTextStream
用于在其上插入值。
为了改进代码,我创建了一个按钮,指向 select 保存此 .csv 文件的目录。
Select 目录.cpp:
void MainWindow::on_pushSelectDir_clicked()
{
QFileDialog directory;
ui->lineEditDir->setText(directory.getSaveFileName(this,"Choose an directory"));
dir_name = ui->lineEditDir->text();
}
创建 .csv 文件 .cpp:
void MainWindow::on_pushExportCSV_clicked()
{
QFile data(dir_name+".csv");
if (data.open(QFile::WriteOnly | QIODevice::Append)) {
}
QTextStream output(&data);
output <<"ID"<<","<<"Name"<<","<<"Age"<< '\n';
for(i=0;i<10; i++) {
output << ID[i] <<","<< Name[i] <<","<< Age[i] << '\n';
}
data.close();
}