windows 如何使用批处理脚本和命令行参数打开特定的 excel 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45442830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:55:00  来源:igfitidea点击:

How to open a specific excel file with a batch script and command line arguments?

excelwindowsbatch-file

提问by alluppercase

I designed an excel spreadsheet that takes data from a server using an RTD feed and processes it. I want the excel file to open automatically during the computers startup. The way that I have decided to go about doing this is to write a batch script that opens the excel file and to then put that batch script in the computers startup folder.

我设计了一个 Excel 电子表格,它使用 RTD 提要从服务器获取数据并对其进行处理。我希望在计算机启动期间自动打开 excel 文件。我决定这样做的方法是编写一个批处理脚本来打开 excel 文件,然后将该批处理脚本放在计算机启动文件夹中。

The problem I am running into relates to the batch script. The RTD feed does not work if I use the default shortcut for excel. Instead I have to use a shortcut that has the following target line:

我遇到的问题与批处理脚本有关。如果我使用 Excel 的默认快捷方式,则 RTD 提要不起作用。相反,我必须使用具有以下目标行的快捷方式:

 "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" /a "CompanyExcelAddin.CompanyFunctions"

I am able to open the file using this command line

我可以使用此命令行打开文件

start `"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" "C:\...\filename.xlsm"`

but I am not able to open a file using the following bash command

但我无法使用以下 bash 命令打开文件

start "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" /a "CompanyExcelAddin.CompanyFunctions" "C:\...\filename.xlsm"

If I open it using the first bash script the RTD feed doesn't work. If I try to run the second script the bash script doesn't run.

如果我使用第一个 bash 脚本打开它,则 RTD 提要不起作用。如果我尝试运行第二个脚本,则 bash 脚本不会运行。

How do I write a bash script that takes command line arguments for the startup program?

如何编写一个 bash 脚本,该脚本为启动程序采用命令行参数?

回答by Sergio Muriel

@echo off
set params=%*
start excel "MyWorkbook.xlsm" /e/%params%

Let's suppose you named it "MyBatch.bat", then you call it like this:

假设您将其命名为“MyBatch.bat”,然后您这样称呼它:

MyBatch.bat Hello/World1

Use space " " to separate parameters. Use slash "/" instead of space for parameters with spaces.

使用空格“”来分隔参数。对于带空格的参数,使用斜杠“/”而不是空格。

In case you do not like the string I believe you can also do this (in a *.bat file):
start excel "MyWorkbook.xlsm" /e/%param1%/%param2%/%param3%.....

如果您不喜欢该字符串,我相信您也可以这样做(在 *.bat 文件中):
start excel "MyWorkbook.xlsm" /e/%param1%/%param2%/%param3%.....

In case you need to open several Excel instances:

如果您需要打开多个 Excel 实例:

@echo off
set params=%*
for %%C in (%params%) do (
    start excel "MyWorkbook.xlsm" /e/%%C
)

回答by Maximilian Burszley

First, I'd recommend using a scheduled task over the startup folder (consistent behavior). Trigger on login, or however you need it, and execute powershell -ExecutionPolicy Bypass -NoProfile -File 'thing.ps1'. It looks like you may be passing the arguments out of order for the Excel exe.

首先,我建议在启动文件夹上使用计划任务(一致的行为)。在登录时触发,或者根据需要触发,然后执行powershell -ExecutionPolicy Bypass -NoProfile -File 'thing.ps1'. 看起来您可能为 Excel exe 传递参数乱序。

$EXE = "${env:ProgramFiles(x86)}\Microsoft Office\root\Office16\EXCEL.EXE"
& $EXE 'C:\...\filename.xlsm' /a 'CompanyExcelAddin.CompanyFunctions'