win cmd在Excel中打开特定电子表格的内容是什么?

inq*_*one 3 windows vbscript excel cmd

我知道你可以从win cmd行打开一个Excel文件.但是如何使用win cmd在该文件中打开特定的电子表格?

bre*_*tdj 5

  1. 将以下代码粘贴到文本编辑器(NotePad,写字板,Word等)
  2. 例如,使用"vbs"扩展名保存文件
    ExcelSheet2.vbs
  3. 将此行更改strFileName = "c:\temp\testa.xlsx"为所需的Excel文件路径
  4. 然后,您可以通过输入vbs文件的路径名从命令行运行此命令

如果文件路径错误或第二张表不存在,代码会进行错误处理.

[ 更新:添加了进一步的错误处理以测试隐藏的第二张纸]

样品

Const xlVisible = -1
Dim objExcel
Dim objWb
Dim objws
Dim strFileName
strFileName = "c:\temp\test.xlsx"
On Error Resume Next
Set objExcel = CreateObject("excel.application")
Set objWb = objExcel.Workbooks.Open(strFileName)
Set objws = objWb.Sheets(2)
On Error GoTo 0
If Not IsEmpty(objws) Then
    If objws.Visible = xlVisible Then
        objExcel.Goto objws.Range("a1")
    Else
        wscript.echo "the 2nd sheet is present but is hidden"
    End If
    objExcel.Visible = True
Else
    objExcel.Quit
    Set objExcel = Nothing
    If IsEmpty(objWb) Then
        wscript.echo strFileName & " not found"
    Else
        wscript.echo "sheet2 not found"
    End If
End If
Run Code Online (Sandbox Code Playgroud)