如何编写下拉框以报告excel文件?

时间:2014-06-09 20:02:06

标签: javascript html excel

我有一个简单的问题。我确信每个人都很容易......但是我。我无法通过下拉框报告所选的选项。有人可以帮忙吗?此代码是通用的,但实际使用的代码与此类似。由于某种原因,它没有从下拉框向excel wb报告所选的选项。任何人都可以让我知道我失踪的是什么吗?提前感谢你!

<SCRIPT LANGUAGE=JAVASCRIPT> 
var currentDate = new Date();
var date1 = currentDate.getDate();
var mon = currentDate.getMonth()+1;
var year = currentDate.getYear();
var today = mon+"/"+date1+"/"+year;
var filePath = "Somefile.xlsx";

function setDate() {
f1.tDate.value=today;
}

function saveToExcel() {
var myApp = new ActiveXObject("Excel.Application");
myApp.visible = false;
var xlCellTypeLastCell = 11;

var myWorkbook = myApp.Workbooks.Open(filePath);
var myWorksheet = myWorkbook.Worksheets(1);
myWorksheet.Activate;
objRange = myWorksheet.UsedRange;
objRange.SpecialCells(xlCellTypeLastCell).Activate;
newRow = myApp.ActiveCell.Row + 1;

strNewCell = "A" + newRow;

myApp.Range(strNewCell).Activate;
myWorksheet.Cells(newRow,1).value = f1.tDate.value;
myWorksheet.Cells(newRow,2).value = f1.tAgent.value;
myWorksheet.Cells(newRow,3).value = f1.tIssid.value;
myWorksheet.Cells(newRow,4).value = f1.tLOB.value;
myWorksheet.Cells(newRow,5).value = f1.tResp.value;
myWorksheet.Cells(newRow,6).value = f1.tResp.value;
myWorksheet.Cells(newRow,7).value = f1.tTaken.value;
myWorksheet.Cells(newRow,8).value = f1.tAlred.value;
myWorksheet.Cells(newRow,9).value = f1.tAlred.value;
myWorksheet.Cells(newRow,10).value = f1.tUnab.value;
myWorksheet.Cells(newRow,11).value = f1.tNotes.value;

myWorkbook.Close(true);
myApp.Workbooks.Close;
myApp.Close;
alert('Data successfully saved');
}
</SCRIPT>
<BODY onLoad="setDate()">
<form name="f1" method="post">
    <h1>Some title</h1><br />
        <h3>Payment Entry Log</h3>
<hr>
<table cellpadding="5" width="475">
<tr>
    <td class="tb_bor"align="left" ><b>Date of Call</b><br />
    <input type=text name=tDate ></td>

    <td class="tb_bor" ><b>Entered By:<br /></b>
    <select name="tAgent" id="tAgent" style="80% !important;">

    <option value="Agent 1" >Agent 1</option>
    <option value="Agent 2" >Agent 2</option>
    <option value="Agent 3" >Agent 3</option>
    <option value="Agent 4" >Agent 4</option>
    <option value="Agent 5" >Agent 5</option>
    <option value="Agent 6" >Agent 6</option>
    <option value="Agent 7" >Agent 7</option>
    <option value="Agent 8" >Agent 8</option>
    <option value="Agent 9" >Agent 9</option>
    <option value="Agent 10" >Agent 10</option>
    </select>
    </td>
</tr>
<tr>
    <td class="tb_bor" ><b>Subscriber ID:</b></td>
    <td align="left" ><input type=text name="tIssid" ></td>
</tr>
<tr>
    <td class="tb_bor" ><b>L.O.B:<br /><br /></b>
    <td align="left" >
    <input id="az" type="radio" value="1" name="tLOB" >AZ<br />
    <input id="ca" type="radio" value="2" name="tLOB" >CA<br />
    <input id="or" type="radio" value="3" name="tLOB" >OR<br /><hr></td>

</tr>
<tr>
    <td width="" class="tb_bor" ><b>Positive Response:<br /><br /></b>
    <td align="left" width="235" >
    <input id="yes" type="radio" value="4" name="tResp" >Yes<br />
    <input id="no"  type="radio" value="5" name="tResp" >No <b><font color="red" size="2">(Indicate only if negative response)</font></b><br /><hr></td>
</tr>
<tr>        
    <td width="" class="tb_bor" ><b>Was a payment taken? <br /><br /></b>
    <td align="left" >
    <input id="yes" type="radio" value="6" name="tTaken" >Yes<br />
    <input id="no"  type="radio" value="7" name="tTaken" >No <br /><hr></td>
</tr>
<tr>        
    <td width="225" class="tb_bor" ><b>Was a payment already made?<br /> <font color="red" size="2">(Must be located in systems, i.e. ABS, CHKSEA, LEDINQ, etc...)</font></b><br /><br /> </b>
    <td align="left" >
    <input id="yes" type="radio" value="8" name="tAlred" >Yes<br />
    <input id="no"  type="radio" value="9" name="tAlred" >No <br /><hr></td>
</tr>
<tr>        
    <td width="225" class="tb_bor" align="right"><b>Could the member be reached?<br /> <font color="red" size="2">(Busy, no awnser , wrong number etc please indicate)</font></b><br /><br /> </b>
    <td align="left" >
    <input id="yes" type="radio" value="10" name="tUnab" >Yes<br />
    <input id="no"  type="radio" value="11" name="tUnab" >No <br /><hr></td>
</tr>

<tr>
    <td class="tb_bor" colspan="3" align="left"><b>Notes:</b><br />
    <textarea rows="6" cols="55" name="tNotes" ></textarea><br />
        <font color="red" size="2">(If a payment was taken, please add notes that were entered in the account)</font></td>
</tr>
</table>

<hr>
<table>
<tr>
    <td align="center" colspan="2">
        <input type="reset" name="Reset" value="Clear" >
        <input type="button" name=save value="Submit" onClick='saveToExcel();'>
    </td>
</tr>
</table>
</form>
</BODY>

1 个答案:

答案 0 :(得分:0)

代码实际上对我有用。我不确定你收到了什么错误。

我做了什么:

  1. 从Internet Explorer打开PC(Windows 8)上的html文件,确保在显示选项时启用ActiveX。
  2. 创建Excel文件,将其放在与html文件相同的位置并关闭它。
  3. 更新了javascript中Excel文件的路径。我使用了绝对路径(从&#34; C:/&#34;开始)并仅使用正斜杠。
  4. 在Internet Explorer的下拉框中更改了选择,然后点击了&#34; Post&#34;选择不同。
  5. Excel文件在检查时已适当更新。

    此外,您应该能够在Internet Explorer&#34;开发者工具&#34;中找到运行您的javascript代码的任何错误消息。使用&#34;控制台&#34;。