javascript从excel中读取数据_自己规定行数列数

来源:互联网 发布:网络胜利组漫画178 编辑:程序博客网 时间:2024/06/03 17:11

我以前写的,转过来。

 

<Html>
<HEAD>
<TITLE>javascript从excel中读取数据_自己规定行数列数_容错校验</TITLE>

<SCRIPT LANGUAGE = JavaScript>
/*
常用的语句:

alert(opf.value);       //id为opf的元件的值

document.write("");  //在网页中显示代码或文本

exWSheet.Cells(rows, cols).Value     //excel文件每个格子里的值

try{  } catch(e){  } finally{  }      //容错语句
*/

function addfile(){

var exApp = new ActiveXObject("Excel.Application");
var exWBook = exApp.workbooks.open(opf.value);
var exWSheet = exWBook.Worksheets(1);

var i=1,j=1;
var col = parseInt(colnum.value,10),row = parseInt(rownum.value,10);       //由于输入的内容不能够直接作为整形数据使用,因此要强行转换
//
alert("行数:" + row);
//
alert("列数:" + col);
if (isNaN(row) == true){  //isNaN是校验强制转换以后的数据是否为整形数据
 alert("行号只能为数字");
 
return 0;
}

if (isNaN(col) == true){
 alert(
"列号只能为数字");
 
return 0;
}


try{
document.write(
"<table width=200%>");
 
while (i<=row) {
     document.write(
"<tr>");
     
while (j<=col) {
       document.write(
"<td>");
    
if (exWSheet.Cells(i, j).Value != null ){
   document.write(exWSheet.Cells(i, j).Value);
          }

    
else{
   document.write(
"");
    }

       document.write(
"</td>");
       j 
= j + 1;
     }

     i 
= i + 1;
  j 
= 1;
  nullmark 
= 0;
   document.write(
"</tr>");
 }

document.write(
"</table>");
}

catch(e){
 alert(
"错误代号是:" + e);
}

finally {
exWBook.Close (savechanges
=true);
exApp.Quit();
exApp 
= null;
//释放excel进程,关闭当前浏览器后有效。
}

}

</SCRIPT>

</HEAD>

<BODY BGCOLOR="white">
<input type="file" id="opf" name="opfile"/><br />
行数:
<input type="text" id="rownum"><br />
列数:
<input type="text" id="colnum"><br />
<input type="submit" onclick="addfile()">
 
</BODY>
</HTML>

 

 

原创粉丝点击