使用excel activeX保存而不是saveAs

时间:2013-02-16 11:37:21

标签: javascript excel activex

我正在使用javascriptactiveX来保存和编辑Excel文档的数据。到目前为止一切正常。但现在我想写一个新版本,我可以更新一个特定的单元格,并保持其他单元格在哪里。

以下是我正在使用的代码:

function Write() {
    var Excel, Book; // Declare the variables
    Excel = new ActiveXObject("Excel.Application"); // Create the Excel application object.
    Excel.Visible = false; // Make Excel invisible.
    Book = Excel.Workbooks.Add() // Create a new work book.


    Book.ActiveSheet.Cells(1,1).Value = document.A1.value;

    Book.SaveAs("G:/whiteboard.xls");  
    Excel.Quit(); // Close Excel with the Quit method on the Application object.
    alert("Uppgifter sparade");
   }

我想保存,而不是保存。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果有人还在寻找这个。 使用excelObj.ActiveWorkbook.Save();保存Excel文件。 这是一个例子。

function saveExcel() {

  var excel = new ActiveXObject("Excel.Application");  
  excel.Visible = false;
  var fso = new ActiveXObject("Scripting.FileSystemObject");


  var path = "file:\\\\C:\\Users\\User\\Desktop\\TestFile.xls";
  var excel_file = excel.Workbooks.Open(path); 
  var excel_sheet = excel_file.Worksheets(1); 

  //Editing all cells from Row 6 to 10 and Column 1 to 5
  for (var row = 6; row <=10; row++) {
    for (var col = 1; col <=5 ; col++) {
        excel_sheet.Cells(row,col).Value = "Setting New Cell Data"; 
      };
  };
  //Save Excel    
  excel.ActiveWorkbook.Save();

  alert("Excel Saved!");
  //showing excel are editing is done.
  excel.Visible = true;
  excel = null;
  excel_file = null;

};