0

Using lightning component i want to download the excel file with some contacts. I am able to get the contacts. But file downloading if my contact name is in other language like Chinese/Japanese, the name appeared garbled.

console.log('****'+csv);
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_self'; 
hiddenElement.download = 'ExportData.csv'; 
document.body.appendChild(hiddenElement); 
hiddenElement.click(); 

In my console log for the csv is coming properly. After export i faced this problem

  • How do you determine that the file is corrupted? If you open and view it in a text editor with full Unicode support, such as Visual Studio Code, before opening it in a spreadsheet program, does it look correct? Some versions of Microsoft Excel are notorious for corrupting Unicode content. – David Reed Mar 16 '19 at 12:05
  • @DavidReed In Visual Studio code the value is coming properly . – jessy Mar 16 '19 at 14:04
0

The behavior you describe in your comment:

In Visual Studio code the value is coming properly

points to the location of the issue. Your code is correct, and the file downloaded is correct. The software you're using to view or manipulate your CSV, Microsoft Excel, is not correctly interpreting its UTF-8 contents.

I believe Excel does have an "Import" function that allows you to specify a text encoding upon load of CSV data into a spreadsheet, but my experience has been that directly opening a CSV with international text in many versions of Excel produces garbled results and can corrupt the file upon saving. I've had much more success with the free, open source LibreOffice Calc for working with these CSVs, which allows you to specify the desired text encoding each time you open a CSV file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.