0

I am getting strange Issue that whenever I am exporting the data in csv which have a currency symbol, It has added junk extra character in the data beside the currency symbol.

For example if My data = France - Admin Fee 1 x £100 I am getting the result like = France - Admin Fee 1 x £100 when i open this in Excel. My code is :

<html>
<head>

<script type="text/javascript">
function CreateCSV()
{

var buffer = "France - Admin Fee 1 x £100";
buffer = "\"" + buffer + "\"";
// buffer = "" + euro; //"\u2034";

var uri = "data:text/csv;charset=UTF," + encodeURIComponent(buffer);
var fileName = "InvoiceData.csv";

var link = document.createElement("a");
if (link.download !== undefined) { // feature detection
    // Browsers that support HTML5 download attribute
    link.setAttribute("href", uri);
    link.setAttribute("download", fileName);
}
else if (navigator.msSaveBlob) { // IE 10+
    link.addEventListener("click", function (event) {
        var blob = new Blob([buffer], {
            "type": "data:text/csv;charset=UTF;"
        });
        navigator.msSaveBlob(blob, fileName);
    }, false);
}
else {
    // it needs to implement server side export

}
link.innerHTML = "Export to CSV";
link.click();
}
</script>
</head>
<body>
<input type="button" value="Download CSV" onclick="CreateCSV()" />
</body>
</html>

When i open the same in notepad. I cannot see the junk character. I am very thankful if you can get me a work around.

Kartik Patel
  • 9,303
  • 12
  • 33
  • 53

1 Answers1

0

The character set should probably be UTF-8. Also check the unicode for the £, I do believe it is u2034. You can find a chart here, and it lists it as U+00A3. If you have something more advanced than just Notepad, like Notepad++ for example, check the encoding type when you open the time. Excel can be finicky.

dc317
  • 26
  • 1
  • yes it is UTF-8. but can you please tell what should i need to edit to resolve this issue in above code. – Kartik Patel Nov 07 '14 at 14:27
  • So I ran the snippet and opened it in Notepad++, which immediately showed the issue. The encoding is UTF-8 without Byte Order Mark (BOM), there's another post about it [here](http://stackoverflow.com/questions/17879198/adding-utf-8-bom-to-string-blob). You need to prepend the buffer string with \ufeff so it would look like this: `buffer = "\ufeff\"" + buffer + "\"";` – dc317 Nov 07 '14 at 15:28
  • I have tried with above way but still have a same issue. – Kartik Patel Nov 10 '14 at 06:19