JavaScript get excel file data

This page discusses - JavaScript get excel file data

JavaScript get excel file data

JavaScript get excel file data

        

By the use of JavaScript we can get the excel file data as well. Here is the example which gets the data from the excel file with the cell and row index values.

To explain this we have created a simple HTML page into which we have four buttons which calls the method GetData(cell,row) as defined in the JavaScript.

 function GetData(cell,row){
  var excel = new ActiveXObject("Excel.Application");
  var excel_file = excel.Workbooks.Open("C:\\amitdata.xls");
  var excel_sheet = excel.Worksheets("Sheet1");
  var data = excel_sheet.Cells(cell,row).Value;
  document.getElementById('div1').innerText =data;
   }

First line of the function creates a new reference object of ActiveXObject for excel application. In the second line we have located the excel file and third line describes which sheet's data we are going to use. Now we can get the data at the specific cell and row position from this sheet. It works with Internet Explorer browser.

Excel file data is as  :

Here is the full example code as follows :

 <html>
<head>
<title>
Style Get data from excel sheet
</title>
<script language="javascript" >
  function GetData(cell,row){
  var excel = new ActiveXObject("Excel.Application");
  var excel_file = excel.Workbooks.Open("C:\\amitdata.xls");
  var excel_sheet = excel.Worksheets("Sheet1");
  var data = excel_sheet.Cells(cell,row).Value;
  document.getElementById('div1').innerText =data;
  }
  </script>
</head>
<body>
<p>&nbsp;</p>
<div style="background: #009955; width:'100%';" align="center">
  <font color="#000080" size="12pt">
<b>Get data from excel sheets</b>
  </font>
</div>
<center>
<p>&nbsp;</p>
<div id="div1" style="background: #DFDFFF; width:'100%';" align="center">
Click buttons to fetch data from c:\amitdata.xls
</div>
<input type="button" value="cell(1),row(1)" onClick="GetData(1,1);" />
<input type="button" value="cell(1),row(2)" onClick="GetData(1,2);" />
<input type="button" value="cell(2),row(1)" onClick="GetData(2,1);" />
<input type="button" value="cell(2),row(2)" onClick="GetData(2,2);" />
</center>
</body>
</html>

Output :

Click on the button "cell(1), row(1)"

Click on "Yes". It will show you the cell(1), row(1) value i.e "amit".

Click on next button to fetch data from the cell(1) and row(2)

Above image shows fetched data from cell(1) and row(2). Click on next button to fetch data from the cell(2) and row(1)

Above image shows fetched data from cell(2) and row(1). Click on next button to fetch data from the cell(2) and row(2)

Above image shows fetched data from cell(2) and row(2).

Download Source Code