Click here to Skip to main content
12,944,121 members (53,376 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
hi
i was trying to read an excel file in JavaScript, using 'for' and 'do-while' loop.

My requirement is to read an excel file and show values in web browser using Javascript, until "null" appears.
I want my program to read values row by row.
I have designed a code but it is reading only one cell of excel.
function my_function()
{
   var Excel;
	Excel = new ActiveXObject("Excel.Application");	
	Excel.Visible = false;
	a=Excel.Workbooks.Open("C:/desktop/TEST.xls").ActiveSheet.Cells(l,i).Value;
	//Excel.Quit()
	return a;
}		
 
//where l is number of rows and i are columns...
var i=1;
var l=1;
do
{
a=my_function()
document.write("value is " +a+"\t");i++;
if (a=="");
{l++;
i=1;
document.write("\n");
}
}
while(a=="");

when i specified the number of rows and columns in same program,it worked.

Please help me why it is not working?
Posted 3-Jun-10 20:18pm
hadi n337
Updated 3-Jun-10 20:25pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

There are lot of logical as well as syntax errors in the code you have provided. I have tried to correct those with minimum changes to your original code.

The below code should work,

function my_function(){
    var Excel;
    Excel = new ActiveXObject("Excel.Application");
    Excel.Visible = false;
    return Excel.Workbooks.Open("C:/desktop/TEST.xls").ActiveSheet.Cells(l,i).Value;
}
 
//where l is number of rows and i are columns...
var i=1;
var l=1;
 
do
{
    a=my_function()
 
    if (a!=null)
    {
        document.write("value is " + a + "&nbsp ;&nbsp ;&nbsp ;&nbsp ;");
        i++;
    }
    else
    {
        l++;
        i=1;
        document.write("<br />");
    }
 
    b = my_function()
}while(a!=null || b!=null);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,653
CHill60 2,970
Maciej Los 2,388
Jochen Arndt 1,900
ppolymorphe 1,765


Advertise | Privacy | Mobile
Web01 | 2.8.170518.1 | Last Updated 4 Jun 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100