Load excel from binary using control

I've been going off of several examples to do this, but none of these options work. 

I have previously saved an Excel copy of a spreadsheet as a binary string (for storage in our database). I now want the user to be able to load that binary back into an existing spreadsheet using a separate button or menu option. 

I'm using the following code:

function loadExcelData(base64EncodedExcelData) {
    // Decode Base64 string to binary data
    var binaryData = atob(base64EncodedExcelData);

    // Convert binary data to Blob
    var blob = new Blob([binaryData], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
    var theFile = new File([blob], "Sample.xlsx"); //convert the blob into file

    // Update the data source of the existing Spreadsheet component
    spreadsheet.open({file:blob});
}

(I've tried with spreadsheet.open({file:blob}), spreadsheet.open({file:theFile}), spreadsheet.open({file:binaryData}) - either nothing happens, or I get a message that it's an unsupported file format.))



I'm unable to attach an example binary file to this, but it is the same binary that was created successfully (and tested) from this code:

  response.blob().then((data) => {
var reader = new FileReader();
reader.readAsDataURL(data);
reader.onloadend = function() {
var base64data = reader.result;
FileMaker.PerformScriptWithOption ( 'import', base64data, 5 );
}

Thank you.

4 Replies

KE Ken May 24, 2024 06:15 PM UTC

Base64encoded Binary file that I'm testing with attached.


Attachment: binary_excel.txt_d3ff372a.zip


BP Babu Periyasamy Syncfusion Team May 27, 2024 04:15 PM UTC

Hi Ken,


We have reviewed your reported query along with the attached base64 string. To investigate your issue, we prepared a sample where we loaded your provided base64 string into the Spreadsheet by converting it into a file and opening it using the open method of our Spreadsheet on a button click. The file is opened properly on our end without any issues.


In your provided code snippet, we noticed that you are attempting to convert the byteCharacters directly into a Blob. However, you need to convert the byteCharacters into a byteArray first, then convert it into a Blob, and finally convert it into a file.


For your convenience, we have attached the prepared sample along with the code snippet and video demonstration for your reference,


Code snippet:


function loadExcelData() {

    //Shared base64 string.

    var base64String = ‘Your provided base64 string’

    var byteCharacters = atob(base64String);

    var byteNumbers = new Array(byteCharacters.length);

    for (var i = 0; i < byteCharacters.length; i++) {

        byteNumbers[i] = byteCharacters.charCodeAt(i);

    }

    //Convert to byte array.

    var byteArray = new Uint8Array(byteNumbers);

    //Convert the byte array to blob.

    var fileBlob = new Blob([byteArray], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });

   //Convert the blob to file to open into Spreadsheet

    var file = new File([fileBlob], 'Sample.xlsx');

    spreadsheet.open({ file: file });

}


Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Load_Base64-752209257


Video link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Load_base64_string-1971287593


Kindly, check the above information and get back to us for further clarifications.



KE Ken May 28, 2024 04:56 PM UTC

That's great. Thank you.



FL Florence Lilian Awino Syncfusion Team May 29, 2024 07:29 AM UTC

Hi Ken,

Thank you for your update. We will wait to hear from you.

Regards,

Florence L.


Loader.
Up arrow icon