QT 导出.csv文件

QString file_name = QFileDialog::getSaveFileName(m_wgt, tr("Choice file"), "./", "*.csv");

QFile out_file(file_name);
if (!out_file.open(QIODevice::WriteOnly | QIODevice::Text)) return;

for (int i = 0; i < tbl->columnCount(); i++) {
    out_file.write(tbl->horizontalHeaderItem(i)->text().toLocal8Bit());
    if (i != tbl->columnCount() - 1) out_file.write(",");
}
out_file.write("\n");

for (int r = 0; r < tbl->rowCount(); r++) {
     for (int c = 0; c < tbl->columnCount(); c++) {
         QString item_text = tbl->item(r, c)->text();
         out_file.write(item_text.toLocal8Bit());
         if (c != tbl->columnCount() - 1) out_file.write(",");
     }
     out_file.write("\n");
}

out_file.close();
QMessageBox::information(0, tr("Export"), tr("Save sessuced!"));

 

你可能感兴趣的:(QT,C++)