Importing Excel bytes to spreadJS

Posted by: datta on 1 May 2019, 12:30 am EST

  • Posted 1 May 2019, 12:30 am EST

    Hi,

    My server code uses Apache POI to create a workbook and then converts it to bytes and sends the response. At ui side I am trying to import this byte using excelIO as mentioned in this link https://www.grapecity.com/blogs/how-to-importexport-excel-files-using-javascript-and-spread-sheets .

    Below is my code

    function loadBlob(response)
    {
    	var spread = new GC.Spread.Sheets.Workbook(document.getElementById('wBook'), {
    		sheetCount: 0
    	});
    	var excelIO = new GC.Spread.Excel.IO();  
    	excelIO.open(response.excelStream, function(json){
    		 workbook.fromJSON(json);  
    		 spread.setActiveSheetIndex(0);
    	}, function (message) {  
            console.log(message);  
        });  
    	
    	
    }
    

    But I get below error

    Failed to execute ‘readAsArrayBuffer’ on ‘FileReader’: parameter 1 is not of type ‘Blob’.

    Can someone please tell me what I am doing wrong here

  • Posted 1 May 2019, 4:21 am EST

    Corrected few errors is loadBlob() function. But still it is not working. Here is modified code

    function loadBlob(response)
    {
    	var spread = new GC.Spread.Sheets.Workbook(document.getElementById('wBook'), {
    		sheetCount: 0
    	});
    	var excelIO = new GC.Spread.Excel.IO();  
    	var file = new File(response.excelStream,'ExcelFile.xlsx',{type : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
    	var path = window.URL.createObjectURL(file);
    	$('#down').prop('href',path);
    	excelIO.open(file, function(json){
    		spread.fromJSON(json);  
    		 spread.setActiveSheetIndex(0);
    	}, function (message) {  
            console.log(message);  
        });  
    	
    	
    }
    

    Getting error : {errorCode: 1, errorMessage: “Incorrect file format.”}

  • Posted 2 May 2019, 1:53 am EST

    Hi,

    Are you able to download the received excel file from the server and open it in excel? If yes, could you please share the sample excel file so that we could further investigate the issue

    Regards

  • Posted 2 May 2019, 6:05 am EST

    Hi Thanks for the reply. I am able to import the bytes into SpreadJS after few modifications to code as suggested by support team. Below is the code snippet that worked for me

    function loadBlob(response)
    {
    	var spread = new GC.Spread.Sheets.Workbook(document.getElementById('wBook'), { sheetCount:1 });
    	var exelIo = new GC.Spread.Excel.IO();
    	var binary = response.excelStream;
    	var len = binary.length;
    	var buffer = new ArrayBuffer(len);
    	var view = new Uint8Array(buffer);
    	for (var i = 0; i < len; i++)
    	{
    	view[i]=binary[i];
    	}
    	 
    	var testBlob = new Blob([view], { type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
    	 
    	exelIo.open(testBlob, function (json) {
    	var workbookObj = json;
    	spread.fromJSON(workbookObj);
    	}, function (e) {
    	console.log(e);
    	alert(e.errorMessage);
    	});
    	
    	
    }
    
  • Posted 6 May 2019, 12:15 am EST

    Hi,

    We are glad to hear that you were able to resolve the issue. Thanks for sharing the solution, it would be helpful for other community members who might face the same issue.

    Regards

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels