简体   繁体   English

在csv导出中需要帮助

[英]Need help in csv export

I've a strange problem. 我有一个奇怪的问题。 Actually I am using export csv in yii with ECSVExport. 实际上,我在ECSV中使用ECSVExport使用export csv。 And its working fine. 而且工作正常。 When I export csv then its headers looks something like this. 当我导出csv时,其标题看起来像这样。

organisation_path, client_key and so on

Now I need to add one row before header (I mean above titles). 现在,我需要在标题之前添加一行(我的意思是标题上方)。 So I used below lines after export csv but before save file to specified location. 所以我在导出csv之后但将文件保存到指定位置之前使用了以下几行。

$handle = fopen($path, 'r+');
fputcsv($handle, array('version', '1'));
fclose($handle);

And after adding this in my csv, added a new row like 在我的csv中添加此内容后,添加新行,例如

version, 1

but my header title became like 但是我的标题变成了

on_path, client_key and so on

So now my problem is when I've added new row at beginning then why my csv title gets disturb (I mean before it was organisation_path but after adding row it became on_path). 所以现在我的问题是,当我在开始处添加新行时,为什么我的csv标题会受到干扰(我的意思是在它成为Organisation_path之前,但在添加行之后变成on_path)。 Is there any better way to add row at beginning of my csv file? 有什么更好的方法可以在csv文件的开头添加行? Actually I don't want to run any loop here as I just want to add one row before header title. 实际上,我不想在这里运行任何循环,因为我只想在标题标题之前添加一行。

Any help is really appreciated. 任何帮助都非常感谢。 Thanks. 谢谢。

Opening file in r+ mode means that r+模式下打开文件意味着

Open for reading and writing; 开放供阅读和写作; place the file pointer at the beginning of the file. 将文件指针放在文件的开头。

So, when you write your version string, current content is overwritten. 因此,当您编写版本字符串时,当前内容将被覆盖。 And you can see that lengths of version, 1 and organizati are the same. 您会看到version, 1长度和organizati相同。

So, you can write your version before exporting to file. 因此,您可以导出到文件之前编写版本。

Or file_get_contents() of your file, prepend this string with your version string and then with file_put_contents() write new data to a file. 或文件的file_get_contents() ,在此字符串前加上版本字符串,然后在file_put_contents()新数据写入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM