如何通过命令行打印excel?

时间:2017-10-24 02:13:29

标签: excel command-line cmd command-prompt

我需要在不打开Microsoft Excel的情况下进行打印。简单来说,我们可以右键单击excel文件。 Windows将显示上下文菜单,用于从上下文菜单中选择“打印”选项。

另一方面,我可以通过命令行打开excel。喜欢这些

   "excel c:\Users\loginid\Documents\data.xlsx"

但是如果我需要通过命令行打印那么可能。 什么命令行打印?

3 个答案:

答案 0 :(得分:0)

如果您只想打印单张工作表,请尝试以下操作:

1. Use the Explorer, My Computer, or any Open dialog box to display the workbook that you want to print.
2.Right-click on the file. Windows displays a Context menu.
3.Choose the Print option from the Context menu.
4.Windows loads Excel (if it is not already open), opens the workbook, and prints it. The workbook is then closed.

答案 1 :(得分:0)

PowerShell可以轻松做到这一点。您可以从字面上调用“ Print”动词-完全按照Kyoujin的建议从命令行执行。我正在运行Win 10 1809,但我相信从PowerShell 1.0一直可以使用它。

powershell -command "start-process -filepath .\printermaintenancesheet.xlsx -verb print"

在这种情况下,我正在当前目录中启动文件。如果需要完整路径,请将其放在单引号中(因为对PowerShell的批处理调用使用双引号):

powershell -command "start-process -filepath 'c:\users\fred\documents\sheetToPrint.xlsx' -verb print"

如Kyoujin所示,这将打开Excel,加载文件,打印并关闭。您可能需要确保预先设置打印区域。

答案 2 :(得分:0)

我发现@user193479 answer不能始终如一地关闭excel,并且缓慢/打开了UI等,并且我对VBA感到满意,因此VBS很容易。我创建了一个VBS脚本,该脚本将文件作为参数,并可以选择使用工作表名称。我故意省略了“副本”和/或“打印机”规范,因为我可以通过PrintUI在VBS之外处理该规范,而只需重复调用脚本即可。我已经有执行此操作的方法来打印PDF / Word等,并且不想添加更多参数。

print_excel.vbs:

Dim XLApp
Dim XLWkbk
Dim ObjArgs
Dim strFileName
Dim strWorkSheetName

Set ObjArgs = wscript.arguments
Set XLApp= CreateObject("Excel.Application")
XLApp.Visible = False
Set XLWkbk = XLApp.Workbooks.Open(objargs(0))
If objArgs.count > 1 Then
 Set ws = XLWkbk.Sheets(objargs(1))
ELSE
 Set ws = XLWkbk.Worksheets(1)
End If
ws.PrintOut
XLWkbk.Close False
XLApp.Quit

Set XLWkbk = Nothing
Set XLApp = Nothing
Set ObjArgs = Nothing

测试:

wscript "C:\tmp\print_excel.vbs" "C:\tmp\Book1.xlsx" "Sheet2"
::wscript "C:\tmp\print_excel.vbs" "C:\tmp\Book1.xlsx"

https://www.freesoftwareservers.com/display/FREES/Print+Excel+Via+Command+Line+-+VBA+VBScript+-+Excel.exe+Switches

也。...Word和PowerPoint具有“打印”开关,分别切换到它们各自的可执行文件... Microsoft到底是什么。