0

我正在使用javascriptandactiveX来保存和编辑 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");
   }

我想保存,而不是 saveAs。

有任何想法吗?

4

1 回答 1

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;

};
于 2016-01-14T10:49:51.090 回答