首先,我要感谢这个社区的出色网站。我对编码世界和excel vba完全陌生。在了解了excel中的可能性之后,我开始了使我们在业务中执行的日常数据输入自动化的旅程。在过去的几周中,我能够将一些代码组合在一起,从而可以访问安全的网站并根据输入的日期生成我需要的报告。但是,现在我陷入了如何将表值实际拉入excel的问题。我附了几张照片。现在第三和第四
答案 0 :(得分:2)
我将使用带有两个输入参数的函数来处理它:
<!doctype html>
<head>
<meta charset='utf-8'>
<script>
function fnExcelReport(){
var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>"; // bgcolor will give color to your first row
var textRange; var j=0;
tab = document.getElementById('tblData'); // id of table
var table = document.getElementById("tblData");
for(j = 0 ; j < tab.rows.length ; j++)
{
tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
}
tab_text=tab_text+"</table>";
//alert(tab_text);
tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table
tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
txtArea1.document.open("txt/html","replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
sa=txtArea1.document.execCommand("SaveAs",true,"Say Thanks to Submit.xlsx");
}
else //other browser not tested on IE 11
sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
return (sa);
}
</script>
</head>
<body>
<table id="tblData">
<tr>
<th>Test</th>
<th>Comments</th>
<th>Pass or Fail</th>
</tr>
<tr>
<td>Test 1</td>
<td><textarea rows="2" cols="15" title="Enter comments if necessary" name="comment" placeholder="Comments" form="usrform"></textarea></td>
<td>
<select name="cars" autocomplete="off" class="passFail")">
<option name="default" value="default" selected="selected" disabled="disabled">Pass or Fail</option>
<option value="pass">Pass</option>
<option value="fail">Fail</option>
</select>
</td>
</tr>
<tr>
<td>Test 2</td>
<td><textarea rows="2" cols="15" title="Enter comments if necessary" name="comment" placeholder="Comments" form="usrform"></textarea></td>
<td>
<select name="cars" autocomplete="off" onchange="java_script_:show(this.options[this.selectedIndex].value)">
<option name="default" value="default" selected="selected" disabled="disabled">Pass or Fail</option>
<option value="pass">Pass</option>
<option value="fail">Fail</option>
</select>
</td>
</tr>
<tr>
<td>Test 3</td>
<td><textarea rows="2" cols="15" title="Enter comments if necessary" name="comment" placeholder="Comments" form="usrform"></textarea></td>
<td>
<select name="cars" autocomplete="off" onchange="java_script_:show(this.options[this.selectedIndex].value)">
<option name="default" value="default" selected="selected" disabled="disabled">Pass or Fail</option>
<option value="pass">Pass</option>
<option value="fail">Fail</option>
</select>
</td>
</tr>
</table>
<button id="btnExport" onclick="fnExcelReport('headerTable', 'test results');"> EXPORT </button>
</body>
</html>
) ...并以NET SALES
的形式返回所需的值。
不幸的是,如果没有真正的HTML并能够进行测试,我将无济于事,因此,这是我的盲目尝试(获得启发,但不要期望复制/粘贴,并且代码会工作):
String
基本上,以上功能是:
Private Function getValueByLabel(ByVal ie As SHDocVw.InternetExplorer, ByVal label As String) As String
Dim listOfRows As Object: Set listOfRows = ie.document.getElementsByTagName("tr")
Dim cellsInsideARow As Object
For Each tRow In listOfRows
Set cellsInsideARow = tRow.getElementsByTagName("td")
If cellsInsideARow(2).getElementsByTagName("div")(1).innerText = label Then
getValueByLabel = cellsInsideARow(3).getElementsByTagName("div")(1).innerText
Exit Function
End If
Next tRow
getValueByLabel = "N/A"
End Function
实例的tr
的{{1}}(行)的列表中document.body
(单元格)元素的InternetExplorer
是否等于您要抓取的.innerText
(在您的情况下,td
)label
元素的NET SALES
的函数.innerText
您可以通过使用宏中的好参数来调用该函数,例如:
td
P.s。我之所以要执行一个函数,是因为您可以通过传递期望的标签(N/A
而不是Range("A1") = "NET SALES"
Range("B1") = getValueByLabel(IE, Range("A1").Value)
等)来将相同代码重复使用到同一表中的任何元素>