Qt进行CSV文件操作
https://blog.csdn.net/weixin_39935783/article/details/111685444?ops_request_misc=&request_id=&biz_id=102&utm_term=qt csv文件&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-0-111685444.142v68pc_rank_34_queryrelevant25,201v4add_ask,213v2t3_control1&spm=1018.2226.3001.4187
**
Qt操作csv文件
**
csv文件简述
逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。
示例代码
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void onBtn();
void onOpenBtn();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
信号绑定
connect(ui->pushButton, &QPushButton::clicked, this, &Widget::onBtn);
connect(ui->pushButton_open, &QPushButton::clicked, this, &Widget::onOpenBtn);
- 1
- 2
此信号与槽函数的关联在构造函数中实现即可。
槽函数:onBtn
QString csvFileName = QFileDialog::getSaveFileName(this, QStringLiteral("保存文件设置"), ".", "csv files(*.csv)");
QFile file(csvFileName);
if(!file.exists())
{
file.open(QIODevice::WriteOnly);
QTextStream csvOutPut(&file);
QString str ="姓名,班级,年龄\n";
csvOutPut << str.toUtf8();
file.close();
}
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>file<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span>QIODevice<span class="token operator">::</span>WriteOnly <span class="token operator">|</span> QIODevice<span class="token operator">::</span>Append<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token function">qDebug</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><<</span> <span class="token string">"0000000000000000000000"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span>
<span class="token punctuation">{<!-- --></span>
QTextStream <span class="token function">csvOutPut</span><span class="token punctuation">(</span><span class="token operator">&</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
QString str1 <span class="token operator">=</span> <span class="token string">"张三,3年级1班,28\n"</span><span class="token punctuation">;</span>
QString str2 <span class="token operator">=</span> <span class="token string">"李四,3年级2班,36\n"</span><span class="token punctuation">;</span>
QString str3 <span class="token operator">=</span> <span class="token string">"王五,3年级2班,36\n"</span><span class="token punctuation">;</span>
csvOutPut <span class="token operator"><<</span> str1<span class="token punctuation">.</span><span class="token function">toUtf8</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
csvOutPut <span class="token operator"><<</span> str2<span class="token punctuation">.</span><span class="token function">toUtf8</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
csvOutPut <span class="token operator"><<</span> str3<span class="token punctuation">.</span><span class="token function">toUtf8</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
file<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
file<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
运行结果:
槽函数:onOpenBtn
QString fileName = QFileDialog::getOpenFileName(this,QStringLiteral("打开文件"),".",tr("csv files(*.csv)"));
if(fileName == "")
{
return;
}
QFile <span class="token function">file</span><span class="token punctuation">(</span>fileName<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>file<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span>QIODevice<span class="token operator">::</span>ReadOnly<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token function">qDebug</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator"><<</span><span class="token function">QStringLiteral</span><span class="token punctuation">(</span><span class="token string">"请正确选择csv文件"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span>
<span class="token punctuation">{<!-- --></span>
QTextStream <span class="token operator">*</span> read <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">QTextStream</span><span class="token punctuation">(</span><span class="token operator">&</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
QStringList Data <span class="token operator">=</span> read<span class="token operator">-</span><span class="token operator">></span><span class="token function">readAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">"\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//每行以\n区分</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span> i <span class="token operator"><</span> Data<span class="token punctuation">.</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span> <span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token comment">//去掉最后一行空白行</span>
<span class="token punctuation">{<!-- --></span>
QStringList strLine <span class="token operator">=</span> Data<span class="token punctuation">.</span><span class="token function">at</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">","</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//一行中的单元格以,区分</span>
<span class="token function">qDebug</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><<</span> strLine<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
运行结果:
</article>