Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
JavaScript
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
Updated 1-Feb-19 7:56am
v2

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,

JavaScript
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);
 
Share this answer
 
I am trying to export one cell at a time and was not able to tweak your code to work.



Javascript function




function my_function(){
var Excel;
Excel = new ActiveXObject("Excel.Application");
Excel.Visible = false;
return Excel.Workbooks.Open("C:/desktop/test_rates.xls").ActiveSheet.Cells(2,1).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);
 
Share this answer
 
Comments
Dave Kreskowiak 1-Feb-19 14:20pm    
You posted this as an ANSWER to a nine year old question.

DO NOT DO THIS. Open your own question by going to the "Quick Answers" menu and clicking "Ask a question...".

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900