从Excel读取并将该数据写入HTA应用程序

时间:2016-05-30 06:29:27

标签: excel twitter-bootstrap vbscript hta

这是VBscript和HTA代码,用于从Excel读取数据并使用HTA应用程序将数据粘贴到可编辑的<div>中,并从<div>写入更新的数据,然后在Excel中再次粘贴更新的数据相应地点击按钮。

<html>
<head>
<link href="D:\VBScriptTrainee\bootstrap-3.3.6-dist\css\bootstrap.min.css" rel="stylesheet">
<HTA:application
  ICON= "D:\VBScriptTrainee\Ampeross-Qetto-Icon-developer.ico"
  APPLICATIONNAME="myApp"
  BORDER="dialog"
  BORDERSTYLE="complex"
  CAPTION="yes"
  MAXIMIZEBUTTON="yes"
  MINIMIZEBUTTON="yes"
  SHOWINTASKBAR="yes"
  SINGLEINSTANCE="no"
  SYSMENU="yes"
  VERSION="2.0"
  WINDOWSTATE="maximize"
  SCROLLFLAT="yes"
  SYSMENU="no"/>
<title>Read Excel</title>

<style type="text/css">
body { background-color: #1abc9c; }
p    { font:bold 18px arial; }
</style>

<script language="vbscript">
Public mySpan,selectexcel,objExcel,objWorkbook,rowCount,colCount,rowData,textdata,sp,b,word

On Error Resume Next

Sub ReadExcelData()
    Set mySpan = document.getElementById("Span_id_two")

    mySpan.InnerText = " " 

    selectexcel = InputBox("Enter the location","Location of the excel file(Xls/xlsx)","Enter your path here !")

    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open(selectexcel)
    objExcel.Visible = True

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

    Dim mySpan

    colCount = colCount+1

    MsgBox("colCount" & colCount)

    For intRow=1 To rowCount Step 1
        rowData = Null

        For intCol=1 To colCount Step 1        
            rowData = rowData & "    " & objExcel.Cells(intRow, intCol).Value & "|"
        Next

         Set mySpan = document.getElementById("Span_id_two")
         mySpan.InnerHTML =  mySpan.InnerHTML & rowData & "<br>"
    Next
End Sub

Sub WriteExcelData()
    Set mySpan = document.getelementbyid("Span_id_two")

    textdata = mySpan.InnerHTML

    ReDim words(-1)
    For Each line In Split(textdata, "|")
       For Each word In Split(line, "<BR>")
           For Each wordTwo In Split(word,vbnewLine)
               ReDim Preserve words(UBound(words)+1)
               words(UBound(words)) = wordTwo
           Next
       Next
    Next

    k = 0

    MsgBox("colCount" & colCount)

    For i=1 To rowCount Step 1
       For j=1 To colCount Step 1
          objExcel.Cells(i, j).Value = words(k)
          k = k+1
       Next
    Next

    objExcel.Activeworkbook.Save
    objExcel.Quit

    Set objExcel = Nothing
End Sub
</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/>
<button class="btn btn-success" name="Read" value="Click To Read" onclick="ReadExcelData()">Click To Read</button>
<button class="btn btn-danger" name="Write" value="Click To Write" onclick="WriteExcelData()" >Click To Write</button>
</center>

<br>
<br>

<div>
  <center>
  <table border="2px" color="Red">
    <th id="thead"></th>
    <tr>
      <td>
        <br>
        <div class="jumbotron">
          <div contenteditable="true" ID="Span_id_two" style="color:blue;" name="text_name"></div>
        </div>
        <br>
      </td>
    </tr>
  </table>
  <marquee direction="Right" style="color:red;" >****Note : Only Select Excel file****</marquee>
  </center>
</div>
</body>
</html>

在此,我使用|字符拆分了Excel值,但我希望使用每个单元格值的边框以表格格式显示单元格值。

0 个答案:

没有答案