Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
As reported here:
http://stackoverflow.com/questions/6588068/which-encoding-opens-csv-file...
There is no way to open a UTF-8 csv file correctly with Excel on UNIX and Windows.
Is there any way to provide an option in the display? I found a workaround that I implemented in several projects already, but an option itself would be great!
Workaround code - in views_data_export_plugin_display_export.inc:
protected function outputfile_create() {
$dir = 'temporary://views_plugin_display';
// Make sure the directory exists first.
if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
$this->abort_export(t('Could not create temporary directory for result export (@dir). Check permissions.', array ('@dir' => $dir)));
}
$path = drupal_tempnam($dir, 'views_data_export');
//----hack - add UTF-8 BOM for MS Excel use----
file_put_contents($path, chr(0xEF).chr(0xBB).chr(0xBF));
//--------------------------------------------------------------
// Save the file into the DB.
$file = $this->file_save_file($path);
return $file->fid;
}
Comment | File | Size | Author |
---|---|---|---|
#46 | views_data_export-junk-chars-1701018-45.patch | 355 bytes | sangeetharaog |
#30 | views_data_export-add_optional_BOM_for_CSV-1701018-30.patch | 3.73 KB | dasha_v |
Comments
Comment #1
dropert CreditAttribution: dropert commentedI had the exact same problem. Unfortunately, this hack didn't work out for me because of code changes in the version i recently downloaded (7.x-3.0-beta6). I tried to adjust the code, adding the BOM in front of the CSV string in the file_put_contents method. I tried several other solutions adding the BOM in front of the header in the template file but again with no success. My solution was (i had german characters in my csv file) selecting the "ASCII "encoding" in the display and changing the charset of the iconv function (views_data_export.theme.inc) to "ISO-8859-1". It does the job but why doesn't this module use php's own fputcsv function instead?
Comment #2
somersoft CreditAttribution: somersoft commentedHere is a patch file for 7.x-3.0-beta6 with the adding of the BOM conditional and also in a different place as I found this to work better.
Comment #3
somersoft CreditAttribution: somersoft commentedHere is the version of the patch file with the correct line endings.
Comment #5
somersoft CreditAttribution: somersoft commentedChanging the Version from 7.x-3.x-dev to 7.x-3.0-beta6 so that the automatic test will pass as the test are not a part of that release and the patch file does not patch the test files.
Comment #6
somersoft CreditAttribution: somersoft commented#3: views_data_export-set_utf8_bom-1701018-3.patch queued for re-testing.
Comment #8
bkonetzny CreditAttribution: bkonetzny commentedApplied the patch from #3 to my local 7.x-3.0-beta6 installation and export works now as expected on windows.
Comment #9
killua99 CreditAttribution: killua99 commentedNeeds reroll ... some code change.
Comment #10
jjcarrionApplied the patch from #3 and works ok on windows.
Comment #11
jide CreditAttribution: jide commentedRTBCing.
Comment #12
bkonetzny CreditAttribution: bkonetzny commentedRe-rolled patch against latest dev.
Comment #14
stockliasteroid CreditAttribution: stockliasteroid commentedThis fix works, but this patch won't work if it's a batched export, which is going to be common with any large files. That's because views_data_export_plugin_display_export calls the render_header, render_body, and render_footer methods directly in a batch run, and skips the render() call where this change resides. My fix was to move this code into render_header() instead, since that gets called in both cases.
Comment #15
GrimreaperHello,
Patch in comment #14 solved the problem for me.
Thank you very much.
Comment #16
Alan D. CreditAttribution: Alan D. commentedComment #18
Nicolas Bouteille CreditAttribution: Nicolas Bouteille commentedMy client reported encrypted characters on Windows with a CSV file. Patch fixes the issue thanks!
Comment #19
Steven Jones CreditAttribution: Steven Jones at ComputerMinds commentedTests are failing with this patch, sorry!
Comment #20
lazzyvn CreditAttribution: lazzyvn commentedi confirme patch #14 works fine
But i think we have to add option UTF8 with BOM in config
Comment #21
AhmadZain CreditAttribution: AhmadZain as a volunteer and commentedAfter searching I managed to solve this issue inside the template it self.
Comment #22
colepacak CreditAttribution: colepacak commented#21 worked for me. Thanks, AhmadZain.
Comment #23
circuscowboy CreditAttribution: circuscowboy commentedThe template solution is great because I don't have to even wait for a commit or deal with a patch. It works as advertise and I would recommend that this gets added to the module. I would also argue that this is more of a bug then a feature. Thanks for your solution AhmadZain
Comment #26
dasha_v CreditAttribution: dasha_v commentedRe-formatting patch #21 for testing.
Comment #27
davidhernandezComment #30
dasha_v CreditAttribution: dasha_v at FFW commentedUpdated patch.
Made BOM optional and configurable per view.
By default it is disabled, this should resolve issue with existing tests.
Added new test for this BOM parameter.
Comment #31
weri CreditAttribution: weri at Previon Plus AG commentedI applied and tested the patch #30. It works as expected but I've not done a code review (yet).
Comment #32
amme CreditAttribution: amme commentedConfirm patch #30 works
Comment #33
NWOM CreditAttribution: NWOM commented#30 applied cleanly and worked as expected. Thank you. I think we can mark this as RTBC.
Comment #34
AhmadZain CreditAttribution: AhmadZain as a volunteer and commentedHi sorry for being away
thanks @colepacak and @circuscowboy actually I am still confirming like you guys but I can not get the main reason that makes the patch fail to pass the test, so I reformated the patch and hope that can be confirmed from all sides.
#1701018: CSV export UTF-8 & windows - BOM Ref. About BOM fix in CSV views export.
@dasha_v I removed your patches from this one as you mentioned it in a separate comment and thanks for your support.
Best
Comment #37
NWOM CreditAttribution: NWOM commentedComment #38
AhmadZain CreditAttribution: AhmadZain as a volunteer and commentedHI @NWOM
can you just add a comment to this to make it clear ? is it the testing is not covered or what ?
Thanks in advance.
Zain
Comment #39
NWOM CreditAttribution: NWOM commented@AhmadZain Hello, sorry about not commenting. I had meant to set it to "Needs Review", since a new patch was added that had not been reviewed yet. I was just trying to help with the normal patch submission workflow. Normally every patch that gets added needs to be tested by others before being set to "Reviewed & tested by the community", however I instead chose the wrong status as well :P
Comment #40
AhmadZain CreditAttribution: AhmadZain as a volunteer and commented@NWOM no worries. ;)
thanks for your replay
Comment #41
AhmadZain CreditAttribution: AhmadZain as a volunteer and commentedHi All,
Is there any person is looking after this project ?
Best
Comment #42
RoSk0Patch from #30 applies cleanly and allows to fix an issue in proper way.
I would not recommend to go with approaches form other patches because they are not configurable and have some strange logic.
Comment #43
burshyn CreditAttribution: burshyn commentedPatch from #30 work for me! Thanks dasha_v!
Comment #44
hgoto CreditAttribution: hgoto at Studio Umi commentedI also tested the patch #30 and it works well.
I'm confused. I don't understand why the patch #34 was proposed after the patch #30. The patch #30 has configuration and tests and looks better for me...
Anyway, thank you. +1 for RTBC :)
Comment #45
sangeetharaog CreditAttribution: sangeetharaog as a volunteer commentedI tried patch #30 but it did not work for me. As analyzing the patch I found another way to print
print "\xEF\xBB\xBF";
.This works for me.
Comment #46
sangeetharaog CreditAttribution: sangeetharaog as a volunteer commentedComment #47
aschiwi CreditAttribution: aschiwi at undpaul commentedAdding my experience: the patch in #30 still works for me, thank you!