使用javascript和hta

时间:2016-06-14 05:30:55

标签: javascript vbscript hta

以下是从Excel工作表中读取数据并将该数据粘贴到html表中的代码。它显示一个错误,当我将数据从html表转换为excel表时,但同时这个编码在vbscript中工作,而不是在javascript中工作是什么解决方案..

<html>
<head>
<HTA:application
Applicationname="MyApp"
BORDER="dialog"
BORDERSTYLE="complex"
Caption="yes"
MAXIMIZEBUTTON="yes"
MINIMIZEBUTTON="yes"
SINGLEINSTANCE="no"
SYSMENU="yes"
VERSION="2.0"
WINDOWSTATE="maximize"/>
<title>Read Excel</title>
<style type="text/css">
body
{
background-color:White;
}
P
{
font:bold 18px arial;
}
</style>
<script language="javascript" type="text/javascript">

var rowCount,colCount,selectexcel,objExcel,tempcol,intRow,intCol;
var rowData;
var mySpan;
var textData;


function ReadExcelData()
{

        selectexcel = prompt("Please enter the file path", "D:\VBScriptTrainee\Sample.xlsx");
        objExcel = new ActiveXObject("Excel.Application");
        objExcel.Visible = false;
        objExcel.Workbooks.Open(selectexcel);

        rowCount=objExcel.ActiveWorkbook.Sheets(1).UsedRange.Rows.Count
        colCount=objExcel.ActiveWorkbook.Sheets(1).UsedRange.Columns.Count
        alert(rowCount);
        alert(colCount);

        //colCount=colCount+1;  
        tempcol=colCount;

        rowData="<table border=2>";


  for (intRow=1;intRow <= rowCount;intRow++)
  {

    rowData = rowData + "<tr>";
    for (intCol=1;intCol <= colCount;intCol++)

    {

         if ((intRow != 1) && (intCol ==tempcol))
             {

                    rowData = rowData + "<td>" + "<input type='checkbox' id='flag_id' name='flag' />" + "</td>";
             }       


        else if (intRow ==1) 
            {            
                    rowData = rowData + "<td>"  + objExcel.Cells(intRow,intCol).Value  + "</td>";           
            }


        else
            {


                    rowData= rowData + "<td><div contentEditable='True'>"  + objExcel.Cells(intRow,intCol).Value  + "</div></td>";

            }            


    }
                    rowData = rowData + "</tr>";

  }
                    rowData =rowData + "</table>";

//document.write(rowData);   

             mySpan = document.getElementById('Span_id_two');
             mySpan.innerHTML = rowData + "<br>";

             //objExcel = undefined;

}


function WriteExcelData()  
{

       //mwresult = Msgbox ("Are you Sure to Write ?",vbOKCancel)
                     //var objExcelTwo;
                   //objExcelTwo = new ActiveXObject("Excel.Application");
                   //objExcelTwo.Visible = true;
                   //objExcelTwo.Workbooks.Open(selectexcel);
           var tableValue;
           var mySpan=document.getElementById('Span_id_two');
           textData=mySpan.innerHTML;

           var tab=document.getElementsByTagName("table")(0);  

           var mytable = document.getElementsByTagName("table")(0).rows.length;                 
           //document.write(mytable);
           var mytable1= document.getElementsByTagName("table")(0).rows(0).cells.length;  
           //document.write(mytable1);

                for(n = 0;n<=(mytable-1);n++)  
                    {
                    for(j = 0;j<=(mytable1-1);j++)  
                      {   
                        objExcel.Cells(n+1,j+1).Value = tab.Rows(n).Cells(j).innerHTML;  
                         tableValue = tab.Rows(n).Cells(j).innerHTML; 

                          if(tableValue == "<INPUT id=flag_id type=checkbox CHECKED name=flag>") 
                            {                        
                                objExcel.Cells(n+1,mytable1).Value="Yes";
                            }                      


                          if(tableValue == "<INPUT id=flag_id type=checkbox name=flag>") 
                            {

                                objExcel.Cells(n+1,mytable1).Value="No";
                            }


                       }  

                    }   

                //var find ="<*>";
                //var rplace =" ";

                //objExcel.Cells.Replace find,rplace;

                //MsgBox "Data Exported Successfully",vbInformation                          

                //objExcel.ActiveWorkbook.Save;
                //Set objExcel=nothing





}


</script>

</head>

<body>
<center>
<img src="Excel.png" height=200 width=300/>
</center>

<center>

<h1 align="center" style="color:blue"><img src="icon-developer-icon.png" height=100px width=100px/>
Read Excel (Row by Row)</h1><br>

<input type="button" name="Read" value="Click To Read" onclick="ReadExcelData()"/>
<input type="button" name="Write" value="Click To Write" onclick="WriteExcelData()"/>
</center>
<br>
<br>
<div>
<center>
<!--<table border="2px" color="Red">
<th id="thead"></th>
<tr>
<td>-->
<br>
<!--<div class="jumbotron">-->
<span ID="Span_id_two" Style="Color:blue;" name="text_name">
</span>
<br>
<!--</td>
</tr>
</table>-->
<marquee direction="Right" style="Color:red;">**Note : Select only excel files**</marquee>
</center>
</div>
</div>
</div>
</div>
</body>
</html>

它显示以下错误&#39;对象不支持此属性或方法&#39; 在WriteExcelData函数中。

0 个答案:

没有答案