使用javascript和hta读写excel
下面是从excel工作表中读取数据并将数据粘贴到html表中的代码。当我将数据从html表格转换为excel表格时,它显示了一个错误,但与此同时,这种编码在vbscript中工作,而不是在javascript中工作。解决方案是什么使用javascript和hta读写excel,javascript,vbscript,hta,Javascript,Vbscript,Hta,下面是从excel工作表中读取数据并将数据粘贴到html表中的代码。当我将数据从html表格转换为excel表格时,它显示了一个错误,但与此同时,这种编码在vbscript中工作,而不是在javascript中工作。解决方案是什么 <html> <head> <HTA:application Applicationname="MyApp" BORDER="dialog" BORDERSTYLE="complex" Caption="yes" MAXIMIZEBUTT
<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>
阅读Excel
身体
{
背景色:白色;
}
P
{
字体:粗体18px arial;
}
var rowCount、colCount、selectexcel、objExcel、tempcol、intRow、intCol;
var数据;
var mySpan;
var文本数据;
函数ReadExcelData()
{
选择Excel=prompt(“请输入文件路径”,“D:\VBScriptTrainee\Sample.xlsx”);
objExcel=新的ActiveXObject(“Excel.Application”);
objExcel.Visible=false;
objExcel.Workbooks.Open(选择Excel);
rowCount=objExcel.ActiveWorkbook.Sheets(1).UsedRange.Rows.Count
colCount=objExcel.active工作簿.Sheets(1).UsedRange.Columns.Count
警报(行计数);
警报(colCount);
//colCount=colCount+1;
tempcol=colCount;
rowData=“”;
对于(intRow=1;intRow
您正在尝试访问objExcel.Cells
,我想它应该类似于objExcel.ActiveWorkbook.Sheets(1).Cells
objExcel
从未在您的WriteExcelData
函数中声明,仅在ReadExcelData
中声明,而且由于您没有在函数之间传递对象,我认为这是您的问题。您似乎已经注释掉了objExcelTwo
的定义,这将是您需要引用的内容?@d现在我看到objExcel
变量是全局声明的,所以如果首先启动ReadExcelData()
,它应该可以工作,WriteExcelData()
之后,Excel实例将继续运行。尽管我不确定事情是否会以这种方式运行。我向您的解决方案中添加了一条信息,即objExcel
全局声明可能不必要。tab.Rows(n).Cells(j)
应该是tab.Rows(n).Cells(j)
,JS是区分大小写的。在JS中,您总是将函数的参数包装到括号中-->…替换(查找,替换)
。您试图访问objExcel.Cells
,我想它应该类似于objExcel.ActiveWorkbook.Sheets(1).Cells
objExcel
从未在您的WriteExcelData
函数中声明,仅在ReadExcelData
中声明,而且由于您没有在函数之间传递对象,我认为这是您的问题。您似乎已经注释掉了objExcelTwo
的定义,这将是您需要引用的内容?@d现在我看到objExcel
变量是全局声明的,所以如果首先启动ReadExcelData()
,它应该可以工作,WriteExcelData()
之后,Excel实例将继续运行。尽管我不确定事情是否会以这种方式运行。我向您的解决方案中添加了一条信息,即objExcel
全局声明可能不必要。tab.Rows(n).Cells(j)
应该是tab.Rows(n).Cells(j)
,JS区分大小写。在JS中,您总是将函数的参数包装到括号中-->…Replace(find,rplace)
。