javascript - 如何使用javascript将Excel工作簿保存到当前用户桌面?

标签 javascript excel activex

我通过以下方式从网页创建工作簿:

var thisTable = document.getElementById("mytable3").innerHTML;
       window.clipboardData.setData("Text", thisTable);
       var objExcel = new ActiveXObject ("Excel.Application");
       objExcel.visible = true;
       var objWorkbook = objExcel.Workbooks.Add();
       var objWorksheet = objWorkbook.Worksheets(1);
       objWorkbook.Worksheets(1).Activate;
       objWorksheet.name = "test";
       objWorksheet.Paste;
       objWorksheet.columns.autofit;
       window.clipboardData.setData("Text","");
       objWorkbook.Worksheets(1).SaveAs("%USERPROFILE%\\Desktop\\xxx.xls");

但是对于 objWorkbook.Worksheets(1).SaveAs("%USERPROFILE%\\Desktop\\xxx.xls"); - 它不会保存到桌面并给出以下错误:

SCRIPT1004: Microsoft Excel cannot access the file 'C:\Users\user\Documents\%USERPROFILE%\Desktop\B612F000'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.

最佳答案

来自@PA。回答 我终于得到了答案

//For Expand environment
  var wshShell = new ActiveXObject("WScript.Shell");
  var userProfile = wshShell.ExpandEnvironmentStrings("%USERPROFILE%\\Desktop\\test.xls");

  //For Save
  objWorkbook.Worksheets(1).SaveAs(userProfile);

他你@PA。

关于javascript - 如何使用javascript将Excel工作簿保存到当前用户桌面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11733128/

相关文章:

javascript - 谷歌地图 API : How do you ensure that the Google Maps Autocomplete text is an actual address before submitting?

javascript - AJAX jQuery新版本关于 "Insert into database without page load"

javascript - node-soap 结果中包含 nil 值

javascript在集合中按标题搜索节点并返回它

excel vba range.find找不到日期

c# - ActiveX 控件在 IE 以外的单独窗口中运行

excel - 如何从Excel列字母中获取列号(或索引)

vba - excel VBA : operating with dates from spread sheet into a while/do

windows-mobile - 将.net紧凑框架转换为activex

c++ - MFC WebBrowser.Navigate 错误 "A null reference pointer was passed to the stub."