function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ani MathurAni Mathur 

How to parse an Excel File using Apex?

Ajay K DubediAjay K Dubedi
Hi Ani,

Its better to first read contents of file in JavaScript and then send it in form of JSON string in Apex. Therefore if possible try reading file contents in JavaScript itself, here's the code:
 
var fileName = 'No File Selected..';
var fileOutput = {};
var file = e.getSource().get("v.files")[0];
console.log('file: '+ file +' type:'+typeof file);
var self = this;
console.log('self-->'+JSON.stringify(self));
var objFileReader = new FileReader();
var dec;
objFileReader.onload = $A.getCallback(function() {
   console.log('Inside function');
   var fileContents = objFileReader.result;
   var base64 = 'base64,';
   var dataStart = fileContents.indexOf(base64) + base64.length;
   fileContents = fileContents.substring(dataStart);
   console.log('fileContents-->'+fileContents+' type-->'+typeof fileContents);
   dec = window.atob(fileContents);
   dec = JSON.parse(dec);
   console.log('dec>>'+JSON.stringify(dec));
});
objFileReader.readAsDataURL(file);

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com