【问题标题】:delete rows in excel sheet using javascript使用javascript删除excel表中的行
【发布时间】:2010-01-29 17:55:53
【问题描述】:

我正在尝试使用 Javascript 中的 activex 对象编辑 Excel 工作表。

下面的函数会打开一个 Excel 工作表并创建一些条目。

function test() {
var ExcelApp = new ActiveXObject("Excel.Application");
var ExcelSheet = ExcelApp.Workbooks.Open("c:\\jan29.xls");
ExcelSheet.Application.Visible = true;

ExcelSheet.ActiveSheet.Cells(0,1).Value = i;

ExcelSheet.Save;
ExcelSheet.Application.Quit();
}

但是如何删除行,获取工作表数并通过 rowColumn 获取值。我在哪里可以获得 APIS 列表?

问候, 米通

【问题讨论】:

    标签: javascript excel activex


    【解决方案1】:

    您可以找到Developer Reference for Excel 2007 on MSDN 以及(有些不守规矩的)API Reference

    为了直接回答您的问题,我创建了一个包含行删除的演示。

    工作演示

    (需要 IE、Excel 和 ActiveX 权限)
    http://jsbin.com/izule(可通过http://jsbin.com/izule 编辑)

    JavaScript 源代码

    function test() {
      if (!window['ActiveXObject']) {
        log('Error: ActiveX not supported');
        return;
      }
    
      try {
        var
          ExcelApp = new ActiveXObject("Excel.Application"),
          ExcelBook = ExcelApp.Workbooks.Add();
    
        ExcelBook.Application.Visible = true;
        log('Opened Excel');
    
        wait(2, enterData);
      }
      catch (ex) {
        log('An error occured while attempting to open Excel');
        console.log(ex);
      }
    
      function enterData() {
        try {
          ExcelBook.ActiveSheet.Cells(1, 1).Value = 'foo';
          ExcelBook.ActiveSheet.Cells(2, 1).Value = 'bar';
          log('Entered data');
        }
        catch (ex) {
          log('An error occured while attempting to enter data');
          console.log(ex);
        }
    
        wait(2, deleteRow);
      }
    
      function deleteRow () {
        try {
          ExcelBook.ActiveSheet.Rows(1).Delete();
          log('Deleted first row');
        }
        catch (ex) {
          log('An error occured while attempting to delete a row');
          console.log(ex);
        }
    
        wait(2, quitExcel);
      }
    
      function quitExcel () {
        try {
          // Allow excel to quit without prompting user to save.
          ExcelBook.Saved = true;
          ExcelBook.Application.Quit();
          log('Quit excel');
        }
        catch (ex) {
          log('An error occured while attempting to quit excel');
          console.log(ex);
        }
      }
    }
    
    function wait (time, action) {
      setTimeout(action, time * 1000);
    }
    
    function log (message) {
      var
        list = document.getElementById('log'),
        newLog = document.createElement('li');
      newLog.innerHTML = message;
      list.appendChild(newLog);
    }
    if (!window['console'] || !window.console['log']) { console = {log: log}; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      相关资源
      最近更新 更多