1 ///
2 void ExportToCsv(QAbstractItemModel* view, QString fileName)
3 {
4 if (fileName.isEmpty())
5 return;
6
7 QFile file(fileName);
8
9 if (file.open(QIODevice::WriteOnly))
10 {
11 QTextStream stream(&file);
12 int cc=view->columnCount();
13
14 QStringList list;
15 for (int i=0;i<cc;i++)
16 {
17 list<< view->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString();
18 }
19 stream<< list.join(",")<<"\r\n";
20
21
22 for (int i=0;i<view->rowCount();i++)
23 {
24 list.clear();
25 for (int j=0;j<cc;j++)
26 {
27 list<<view->index(i,j).data().toString();
28 }
29 stream<< list.join(",")<<"\r\n";
30 }
31 file.close();
32 }
33 }
34 其中:
35 view=ui.tableView->model();