0

这是我的代码。我必须连接到 324k+ 行的电子表格并在 html 页面上的表格中返回值。该函数正常工作并返回值。我坚持让它只是更新当前表而不重新加载页面,然后“再次搜索”按钮无法再次启动该功能。必须是 HTML 网页类型的界面。但我可以在 Javascript 和 VBScript 之间进行选择。出于安全考虑,我已经取了一些名字。

<!DOCTYPE html>
<html>
<head>
<title>CS Number Search Tool</title>
        <table id="Header" border="1" bgcolor="E6E7F5">
            <thead bgcolor="69BDF5">
                <tr>
                    <th width="1130"><font color="F2F2F2" />the title i created</th>
                </tr>
            </thead>
        </table>
        <table id="Incidents" border="1" summary="CS Search Results">
            <thead bgcolor="DAF0F5">
                <tr >
                    <th></th>
                </tr>
            </thead>
        </table>
<button onclick="vbscript:ReadExcel()">Search</button>
</head>


<body>
<script Language="VBScript" type="text/vbscript">

  Option Explicit

  Dim idTimer

  function ReadExcel()

  Dim myXlsFile
  myXlsFile = "path\\reprogrammingticketsPOX.xlsb"
  Dim mySheet
  mySheet = "Sheet1"
  Dim my1stCell
  my1stCell = "A1"
  Dim myLastCell
  myLastCell = "F324375"
  Dim blnHeader
  blnHeader = True



  Dim srcphr, Message
  Dim Title

  Message = "Please Enter A Number To Search"
  Title = "CS Search Tool TycoIS"

  srcphr = InputBox(Message, Title)
  If srcphr = "" Then
  Exit Function
  End If

  Dim arrData( ), i, j
  Dim objExcel, objRS
  Dim strHeader, strRange

  Const adOpenForwardOnly = 0
  Const adOpenKeyset      = 1
  Const adOpenDynamic     = 2
  Const adOpenStatic      = 3

  ' Define header parameter string for Excel object
  If blnHeader Then
  strHeader = "HDR=YES;"
  Else
  strHeader = "HDR=NO;"
  End If
  ' Open the object for the Excel file
  Set objExcel = CreateObject( "ADODB.Connection" )
  ' With IMEX=1 numbers won't be ignored; tip by Thomas Willig.
  ' Connection string updated by Marcel Niënkemper to open Excel 2007 (.xslx) files.
  objExcel.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
  & myXlsFile & ";Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;Format=xlsb;" _
                & strHeader & """"

    ' Open a recordset object for the sheet and range
    Set objRS = CreateObject( "ADODB.Recordset" )
    strRange = mySheet & "$" & my1stCell & ":" & myLastCell
    objRS.Open "SELECT * FROM [" & strRange & "] WHERE [CS_No]= '" & srcphr & "' OR [Cust_No]='" & srcphr & "' OR [Job_No]='" & srcphr & "' OR [New_CS_No]='" & srcphr & "' " , objExcel, adOpenStatic

    ' Read the data from the Excel sheet
    i = 0
    Do Until objRS.EOF
        ' Stop reading when an empty row is encountered in the Excel sheet
        If IsNull( objRS.Fields(0).Value ) Or Trim( objRS.Fields(0).Value ) = "" Then Exit Do
        ' Add a new row to the output array
        ReDim Preserve arrData( objRS.Fields.Count - 1, i )
        ' Copy the Excel sheet's row values to the array "row"
        ' IsNull test credits: Adriaan Westra
        For j = 0 To objRS.Fields.Count - 1
            If IsNull( objRS.Fields(j).Value ) Then
                arrData( j, i ) = ""
            Else
                arrData( j, i ) = Trim( objRS.Fields(j).Value )
            End If
        Next
        ' Move to the next row
        objRS.MoveNext
        ' Increment the array "row" number
        i = i + 1
    Loop

    ' Close the file and release the objects
    objRS.Close
    objExcel.Close
    Set objRS    = Nothing
    Set objExcel = Nothing

    ' Return the results
    ReadExcel = arrData

Call writeTable(arrData)

End function


sub writeTable(arrData)

Dim intCount, objSheet
For intCount = 0 To UBound( arrData, 2 )

document.write "<!DOCTYPE html>"
document.write "<html>"
document.write "<head>"
document.write "<title>CS Number Search Tool</title>"
document.write "<table id='Header' border='1' bgcolor='E6E7F5'>"
document.write "<thead bgcolor='69BDF5'>"
document.write "<tr>"
document.write "<th width='1130'><font color='F2F2F2' />Tyco Integrated Security - Panel Reprogramming Project CS Search</th>"
document.write "</tr>"
document.write "</thead>"
document.write "</table>"
document.write "<table id='Incidents' border='1'>"
document.write "<tbody bgcolor='DAF0F5'>"
document.write "<tr>"
document.write "<th width='200px'>Old CS Number <BR>"& arrData( 0, intCount ) &"</th>"
document.write "<th width='150px'>Site Number <BR>"& arrData( 1, intCount ) &"</th>"
document.write "<th width='150px'>Customer Number <BR>"& arrData( 2, intCount ) &"</th>"
document.write "<th width='150px'>Job Number <BR>"& arrData( 3, intCount ) &"</th>"
document.write "<th width='150px'>New CS Number <BR>"& arrData( 4, intCount ) &"</th>"
document.write "<th width='300px'>Source Sheet <BR>"& arrData( 5, intCount ) &"</th>"
document.write "</tr>"
document.write "</tbody>"
document.write "</table>"
document.write "</head>"
document.write "</html>"

Next

Call searchExcel()

End sub

Sub searchExcel()
    document.write "<button onclick=ReadExcel()>Search Again</button>"
End Sub
</script>
</body>
</html>
4

1 回答 1

0

正如我正确理解的那样,您说,该Search Again按钮不起作用。那是因为附加到 onclick 事件的函数调用不在双引号之间。改变

Sub searchExcel()
    document.write "<button onclick=ReadExcel()>Search Again</button>"
End Sub

Sub searchExcel()
    document.write "<button onclick=""VBScript:ReadExcel()"">Search Again</button>"
End Sub

而这部分会表现得更好。

您将遇到的下一个挑战是 Search Again 操作之后的表写在第一个表之后。您可以通过创建具有 ID 的表、获取表并使用新数据document.getElementById设置属性来使其更干净。innerHtml

于 2013-02-19T06:57:48.543 回答