Reading Excel file in AngularJS


This article demonstrates how to read excel file in AngularJS using client side code that means java script and jquery. This demonstration reads first sheet of excel file which contains details about students in the table format and displays students details in UI in table format.

Getting Started

AnglarJS client side code(java script) does not support reading excel file directly, but GitHub provides SheetJS library(Spreadsheet Parser and Writer) to read excel sheet using jquery. currently two jquery libraries (xlsx.core.min.js,xls.core.min.js) available in GitHub. As name describes this two libraries are used to read excel sheet having extension .xlsx and xls.

This demonstrator browses excel file using HTML5 control and help of above mentioned library parses each row of excel sheet into JSON object.This library treats each column header as properties of class and cell values as value of each properties. Go through the below code for more about demonstration.


 <HTML ng-app="studentapp">  
 Learning AngularJS  
 <script src="angular.min.js"></script>   
 <script src="jquery-1.10.2.min.js" type="text/javascript"></script>   
 <script src=""></script>   
 <script src=""></script>   
 <script type="text/javascript">  
 var app=angular.module("studentapp",[]);   
    {StudentRollNo:1,StudentName:"Kailash",Class:"1 std",Div:"A"},   
    {StudentRollNo:2,StudentName:"Sudhir",Class:"1 std",Div:"B"},   
    {StudentRollNo:3,StudentName:"Pankaj",Class:"2 std",Div:"C"},   
    {StudentRollNo:4,StudentName:"Panxi",Class:"2 std",Div:"D"},   
    {StudentRollNo:5,StudentName:"Praban",Class:"3 std",Div:"E"},   
    {StudentRollNo:6,StudentName:"Praphul",Class:"3 std",Div:"F"},   
 $scope.ReadExcelData=function() {  
       var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xlsx|.xls)$/;  
       /*Checks whether the file is a valid excel file*/  
       if (regex.test($("#ngexcelfile").val().toLowerCase())) {  
         var xlsxflag = false; /*Flag for checking whether excel is .xls format or .xlsx format*/  
         if ($("#ngexcelfile").val().toLowerCase().indexOf(".xlsx") > 0) {  
           xlsxflag = true;  
         /*Checks whether the browser supports HTML5*/  
         if (typeof (FileReader) != "undefined") {  
           var reader = new FileReader();  
           reader.onload = function (e) {  
             var data =;  
             /*Converts the excel data in to object*/  
             if (xlsxflag) {  
               var workbook =, { type: 'binary' });  
             else {  
               var workbook =, { type: 'binary' });  
             /*Gets all the sheetnames of excel in to a variable*/  
             var sheet_name_list = workbook.SheetNames;  
             var cnt = 0; /*This is used for restricting the script to consider only first sheet of excel*/  
             sheet_name_list.forEach(function (y) { /*Iterate through all sheets*/  
               /*Convert the cell value to Json*/  
               if (xlsxflag) {  
                 var exceljson = XLSX.utils.sheet_to_json(workbook.Sheets[y]);  
               else {  
                 var exceljson = XLS.utils.sheet_to_row_object_array(workbook.Sheets[y]);  
               if (exceljson.length > 0) {  
                 for (var i = 0; i < exceljson.length; i++) {  
           if (xlsxflag) {/*If excel file is .xlsx extension than creates a Array Buffer from excel*/  
           else {  
         else {  
           alert("Sorry! Your browser does not support HTML5!");  
       else {  
         alert("Please upload a valid Excel file!");  
 <BODY ng-controller="studentcontroller" border="1">  
 <h2>Reading Excel data in AngularJS</h2>  
 <input type="file" id="ngexcelfile" />   
 <input type="button" value="Read Data" ng-click="ReadExcelData()" />   
    <br />   
    <br />  
 <table border=1>  
 <th>Student Name</th>  
 <th>Roll No.</th>  
 <tr ng-repeat="item in students">   


In this article we see, how to read excel file using java script in AngularJS. Above demonstration displays excel data in the UI, but it can be pase to remote service as above libraries are converting excel data to JSON obje. Hope this article may help you, enjoy and happy coding.

Kailash Chandra Behera

No comments:

Post a Comment