Skip to content Skip to sidebar Skip to footer

How To Read And Process Open Document Spreadsheet (*.ods) Files With Javascript?

I am able to read and process *.xlsx files using an input element of type file and the library exceljs. Also see example code below. Unfortunately, exceljs does not seem to support

Solution 1:

Thanks to the comment of Brian I managed to read and process *.ods files with sheetjs/xlsx:

https://github.com/SheetJS/sheetjs

npm install xlsx --save 

If you use script tags fro import, please make sure to import xlsx.full.min.js instead of xlsx.min.js. Otherwise you'll get an error Cannot find file [Content_Types].xml in zip.

static async readOdsFile(file){
        
        await this.__initializeXlsx();  //imports xlsx.full.min.js by script tag
      
        return new Promise((resolve, reject)=>{
            var reader = new FileReader();
            reader.onload = event =>  {
                try{
                    var result = reader.result;
                    var dataArray = new Uint8Array(result);
                    var workbook = Xlsx.read(dataArray, {type: 'array'});

                    const firstSheetName = workbook.SheetNames[0];  
                    const worksheet = workbook.Sheets[firstSheetName];
                    const data = Xlsx.utils.sheet_to_json(worksheet,{header:1});                
                resolve(data); 
                } catch(error){
                    reject(error);
                }
               
            };

            try{                
                reader.readAsArrayBuffer(file);
            } catch (error){
                reject(error);
            }
        }) ;  
       
    }

Post a Comment for "How To Read And Process Open Document Spreadsheet (*.ods) Files With Javascript?"