以表格格式获取Javascript输出

时间:2015-03-14 03:20:22

标签: javascript

我是编程新手,需要一些帮助。

我有以下javascript,其中我从excel中提取数据,但是我无法以表格格式显示变量输出。有人可以指导我如何在表格中显示变量(“exData1”)和(“exData2”)。

<script class="ms-rteThemeForeColor-2-0" type="text/javascript"> ExecuteOrDelayUntilScriptLoaded(init,'sp.js'); var currentUser; var idData; var titleData; function init(){
    this.clientContext = new SP.ClientContext.get_current();
    this.oWeb = clientContext.get_web();
    currentUser = this.oWeb.get_currentUser();
    this.clientContext.load(currentUser);
    this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed)); }

function onQuerySucceeded() {
    document.getElementById('userLoginName').innerHTML = currentUser.get_loginName(); 
    document.getElementById('userTitle').innerHTML = currentUser.get_title(); 
    idirData = currentUser.get_loginName();
    titleData = currentUser.get_title(); }

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace()); }

function GetExcelData1(){
    var excel = new ActiveXObject("Excel.Application");
    var excelFile = excel.Workbooks.Open("Usage.xlsx");
    var excelSheet = excelFile.Worksheets.Item(2);
    var column = 1;
    for (;;){
        var excelData = excelSheet.Cells(column,1).Value;
        if (idData.toLowerCase() == "id\\"+excelData.toLowerCase() || excelData == "") break;
        column++;
    }
    var excelData = excelSheet.Cells(column,3).Value;
    document.getElementById("exData1").innerHTML = excelData+"GB";
    excel.DisplayAlerts = false;
    excel.Quit();
    excel = null;
    idTmr = window.setInterval("Cleanup();",1); }



</script>

1 个答案:

答案 0 :(得分:0)

    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Excel</title>
    <script type="text/javascript">

      function GetExcelData(){
      var excel = new ActiveXObject("Excel.Application");
      excel.DisplayAlerts = false;
      try
      {
        var excelFile1 = excel.Workbooks.Open("test1.xlsx");
        var excelFile2 = excel.Workbooks.Open("test2.xlsx");
        var excelSheet1 = excelFile1.Worksheets.Item(1);
        var excelSheet2 = excelFile2.Worksheets.Item(1);
        var column1 = 1;
        var column2 = 1;
        var r = 1;
        var tbl = document.getElementById("mytable");
        for (;;){
          var excelData1 = excelSheet1.Cells(column1,r).Value;
          var excelData2 = excelSheet2.Cells(column2,r).Value;
          if (excelData1 == undefined || excelData2 == undefined) break;
          if (excelData1 == "" || excelData2 == "") break;
          r++;

          var row = tbl.insertRow();       // this creates a row
          var cell1 = row.insertCell();    // this creates cell1 in the row
          var cell2 = row.insertCell();    // this creates cell2 in the row
          var cell3 = row.insertCell();    // this creates cell3 in the row, for the sum of cell1 + cell2
          cell1.innerHTML = "<p>"+excelData1+"</p>";
          cell2.innerHTML = "<p>"+excelData2+"</p>";
          cell3.innerHTML = "<p>"+excelData1+" "+excelData2+"</p>";
       }
     }
     catch(ex)
     {
       alert("Error load Excel files"+ ex.message);
     }
     excel.Quit();
     excel = null;
    }

  </script>
    </head>
    <body>
       <div>
       <table id='mytable' cellspacing='1' cellpadding='3'>
       </table>
       <input type="button" onclick="GetExcelData()" />
       </div>
    </body>
    </html>