UTF-8 encoding on CSV Export

Hi, while using CSV Export on Grid widget it exports as UTF-8-BOM, but we need UTF-8.

This is how it shows using Notepad++:

Image_6957_1715726800177


6 Replies

RO Rodrigo May 19, 2024 06:05 AM UTC

Hi, any updates on this issue?


Thank you. Best regards.



JS Johnson Soundararajan S Syncfusion Team May 22, 2024 11:31 AM UTC

Hi Rodrigo,


We noticed that you would like to export data in UTF-8 format, but you are currently receiving it in UTF-8 with BOM (Byte Order Mark). To meet your requirement of exporting in plain UTF-8 format, we can use the saveAsBlob method instead of the save method.


Please refer to the below sample and code snippet for more information.


Code sample :

 

function toolbarClick(args) {

  if (args.item.id === 'Grid_csvexport') {

    var Gridexcelexport = grid.csvExport({}, true);

    Gridexcelexport.then(function (workbook) {

      const wbook = new ej.excelexport.Workbook(workbook'csv');

      wbook.saveAsBlob('text/csv''utf8').then(function (result) {

        var blob = result.blobData;

        var link = document.createElement('a');

        link.download = 'Workbook.csv';

        link.rel='nofollow' href = window.URL.createObjectURL(blob);

        document.body.appendChild(link);

        link.click();

        document.body.removeChild(link);

      });

    });

  }

};

 


Please get back to us, if you need further assistance.


Regards,

Johnson Soundararajan S



RO Rodrigo replied to Johnson Soundararajan S May 27, 2024 05:55 AM UTC

Hi Johnson, thanks for your response.


Works fine with the code provided, the code i'm using uses exportProperties wich now don't work any more.


Please check following code:

Image_3688_1716789232152


Still uses comma as separator instead of semi-colon.


Best regards,




JS Johnson Soundararajan S Syncfusion Team May 28, 2024 12:49 PM UTC

Hi Rodrigo,


We noticed that you would like to utilize exportProperties in the CSV export, but it's not functioning as expected. We attempted to meet your requirement for exporting using exportProperties. We can utilize the FileReader to customize the content.


Please refer to the below sample and code snippet for more information.


Code sample :

 
function toolbarClick(args) {

  if (args.item.id === 'Grid_csvexport') {

    var exportProperties = { separator: ';' };

    var Gridexcelexport = grid.csvExport(exportPropertiestrue);

    Gridexcelexport.then(function (workbook) {

      const wbook = new ej.excelexport.Workbook(workbook'csv');

      wbook.saveAsBlob('text/csv''utf8').then(function (result) {

        var blob = result.blobData;

        // Convert blob to string

        var reader = new FileReader();

        reader.readAsText(blob'utf-8');

        reader.onload = function () {

          // Modify CSV content to include custom separator

          var csvContent = reader.result.replace(/,/g';');

          // Create a new blob with modified CSV content

          var modifiedBlob = new Blob([csvContent], { type: 'text/csv' });

          // Create download link

          var link = document.createElement('a');

          link.download = 'Workbook.csv';

          link.rel='nofollow' href = window.URL.createObjectURL(modifiedBlob);

          // Trigger download

          document.body.appendChild(link);

          link.click();

          // Cleanup

          document.body.removeChild(link);

        };

      });

    });

  }

};
 


Please get back to us, if you need further assistance.


Regards,

Johnson Soundararajan S



RO Rodrigo replied to Johnson Soundararajan S May 29, 2024 03:18 AM UTC

Hi Johnson, thank you for your quick response.


I see you're using a Replace function to replace commas to semicolon, wich might works in most cases, in our case some of the columns have commas inside text fields wich will create extra columns on each hit.


Thank you.


Image_4973_1716952657566



JS Johnson Soundararajan S Syncfusion Team June 3, 2024 10:35 AM UTC

Hi Rodrigo,


We have observed that you are interested in using the exportProperties(separator) function in the CSV export, but it is not working as intended. In order to accommodate your request for exporting with a separator of ';', we can include the parameter in the workbook.


Please refer to the below code snippet for more information.


Code sample :

 

  if (args.item.id === 'Grid_csvexport') {

    var Gridexcelexport = grid.csvExport({}, true);

    Gridexcelexport.then(function (workbook) {

      const wbook = new ej.excelexport.Workbook(workbook'csv'undefinedundefined';' );

      wbook.saveAsBlob('text/csv''utf8').then(function (result) {

        var blob = result.blobData;

        var link = document.createElement('a');

        link.download = 'Workbook.csv';

        link.rel='nofollow' href = window.URL.createObjectURL(blob);

        document.body.appendChild(link);

        link.click();

        document.body.removeChild(link);

      });

    });

  }

 


Please get back to us, if you need further assistance.


Regards,

Johnson Soundararajan S


Loader.
Up arrow icon