Home Javascript Javascriptexamples JavaScript get excel file data
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JavaScript get excel file data

Advertisement
This page discusses - 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

        

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Ask Questions?    Discuss: JavaScript get excel file data  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
kurremus
September 28, 2011
Excellent

Excellent documentation. Thank you. JavaScript is kind of an light way of creating applications. Writing and reading documents/directories seems not possible without aktiveX components.
T. Meganathan
January 10, 2012
not working

everthing is ok, created C:\\amitdata.xls file and html page. but after clicking nothing is happening pl. give full detail code i am running in windows 7
Sohail
January 19, 2013
wow, thats great

Is there a way we can call a specific Excel cell value in HTML files. I mean in your example you called the values with the help of a button. But If I want to call the values directly to HTML, what would be the code?
efra
February 24, 2012
browser not supported?

Hi there, How to make this browser proof / compatible? It seems that mozilla browser this not accept.
sanjay
April 20, 2012
nice

nice work..but soem time automation object error
Abdul Wazid
May 9, 2012
Excel Tag

Using hyperlinks i prepared some report in excel which is not secure for me any one can modify that. I want to do the same in Excel but the entire data is in excel. Please guide me how to dump in html or create a page
Renato
June 29, 2012
Thanks and Help me

Dear friend, Thanks for your article. I was very helpful. However, I need a small change and I can not. I have a table in html. In each HTML cell will review a fixed and a variable that I bring from excel. I'm not able to put the variable as a complement to my comment. Below I pasted the HTML code for easy. Can you help me? grateful. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" /> <title>Teste de Excel através do Javascript</title> <link rel="stylesheet" type="text/css" href="style.css" /> <script type="text/javascript" src="js/boxOver.js"></script> <script language="javascript" > function GetData(cell,row){ var excel = new ActiveXObject("Excel.Application"); var excel_file = excel.Workbooks.Open("C:\\aria.xlsx"); var excel_sheet = excel.Worksheets("Plan1"); var data = excel_sheet.Cells(cell,row).Value; document.getElementById('div1').innerText =data; } </script> </head> <body> <div id="header"> </div> <table width="50" height="50" border="4" cellpadding="10" bgColor="white" class="adjust_table"> <!-- os dados da planilha são capturados por GetData e passados através de "div1". Deveria aparecer depois de Boxes --> <!-- Ao passar o mouse deveria aparecer "1801 - Boxes Dado A1" --> <tr><td align="center" bgcolor="#FFFFFF"> <div id="div1" > </div> <input type="" value="" onMouseOver="GetData(1,1);" /> <a href="#"><text="" title="header=[1801 - Boxes ] body=[&nbsp;] fade=[on]");"/>1801</a></td></tr> <!-- Ao passar o mouse deveria aparecer "1701 - Boxes 13, 14 e 15 Dado B1" --> <tr><td align="center" bgcolor="#EFEFEF"> <div id="div1" > </div> <input type="" value="" onMouseOver="GetData(1,2);" /> <a href="#"><text="" title="header=[<p>1701 - Boxes 13, 14 e 15] body=[&nbsp;] fade=[on]);">1701</a></td></tr> </table> </body> </html>
Gabby
July 15, 2012
Re Data from excel sheets

I have copied your code and changed the location of the excel sheet from which the data is to be fetched but when I click the cells nothing happens please help this is the exact code am looking for so my users can put in a password which will in turn call and display their outstanding balance from a cell in my spreadsheet
Safan Abdul
August 24, 2012
data Fetch

Hi, Im very new to Javascript and was hoping someone could help me run a program that fetches the inventory list from a website. I downloaded the scraper from here: http://sourceforge.net/projects/walmartscraper/ I am an idiot when it comes to JavaScript, and have no idea what I'm doing. PLEASE HELP!!!
Chris
September 28, 2012
Can you pass cell data to a variable?

Can you pass cell data to a variable?
Guilherme
October 18, 2012
submit file

Hi, I understand your example, but i would like of read xls data by file uploaded. How I can do this? thanks bye
oliver
November 13, 2012
write to excel

any way to use this process in reverse? ie, take a piece of data and write it to an existing excel file?
Pedro Manuel
March 26, 2013
Congrats

Congrats... I've been solving an issue, and this post saved me. Thanks a lot. Pedro Maths
Ahmad Radi
May 2, 2013
Thanks man

Thanks alot!!!
ZecaPOA
July 17, 2012
Really good job.

To be perfect I need to know how do I make show 'div1' like a tooltip when the mouse over any button.
DMCA.com