Prevent Javascript Excel Conversion from removing Leading 0

After Converting an Object to Excel Format, The Final Output removes the leading 0 from the Column "Application ID".

enter image description here

Image Showing an Application ID where those values start with leading 0 (i.e. 0799, 0791)

Here is the sample code (trimmed to minimal)

exportCSV() {
  let csvContent = "data:text/csv;charset=utf-8,";
  let universalBOM = "\uFEFF";
    let dataArray = this.MyFacilitiesData.map(item => {
      const { applicationId } = item
      const extract = { "Application ID": applicationId}
      return extract;
    });
    const array = [Object.keys(dataArray[0])].concat(dataArray);
    let converted = universalBOM +
                    array.map(content => Object.values(content).toString()).join("\n");
    csvContent += converted + "\r\n";
    var encodedUri = encodeURI(csvContent);
 }

where I use encodeUri to make it download as excel. How can i prevent the removal of leading 0?

5 thoughts on “Prevent Javascript Excel Conversion from removing Leading 0”

  1. Excel loves to remove leading zeros from numbers. You can prevent this by having an apostrophe at the start of the number. Example:

    '0799
    

    I think you could do this:

    const extract = { "Application ID": `'${applicationId}`}
    
    Reply

Leave a Comment