Click here to Skip to main content
14,366,234 members
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
Updated 1-Feb-19 8:56am
v2
Rate this:
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);
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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);
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100