get row count of excel sheet from javascript

2019-08-29 10:05发布

问题:

i am trying to get total number of rows in my excel sheet with javascript . here is my code

      var Excel;
      Excel = new ActiveXObject("Excel.Application");
      Excel.Visible = false;
      a= Excel.Workbooks.open("C:/work/ind.xls").ActiveSheet.Cells(1,1).value;
      alert(a);

this show the value at 1st index , but i was wondering if there is any option of getting the total rows of this active sheet .

Thanks

回答1:

Replace

a = Excel.Workbooks.open("C:/work/ind.xls").ActiveSheet.Cells(1,1).value;

with

a = Excel.Rows.Count;   //Depending on excel version, you will get either 65536 or 1048576

or

a = Excel.Workbooks.open("C:/work/ind.xls").ActiveSheet.Cells.UsedRange.Count;    //It will provide total used rows.

Note: ActiveXObject is available only on IE browser. So every other useragent will throw an error