【问题标题】:VBScript Excel Formatting .xlsx filesVBScript Excel 格式化 .xlsx 文件
【发布时间】:2010-09-13 14:46:09
【问题描述】:

基本上我想知道如何使用 VBScript 为单元格设置中心对齐方式...

我一直在谷歌上搜索,似乎找不到任何有用的东西。

【问题讨论】:

  • VBA 中的 VBScript 或从 cscript 运行的外部 vbs 文件?

标签: vbscript


【解决方案1】:
Set excel = CreateObject("Excel.Application")

excel.Workbooks.Add() ' create blank workbook

Set workbook = excel.Workbooks(1)

' set A1 to be centered.
workbook.Sheets(1).Cells(1,1).HorizontalAlignment = -4108 ' xlCenter constant.

workbook.SaveAs("C:\NewFile.xls")

excel.Quit()

set excel = nothing

'If the script errors, it'll give you an orphaned excel process, so be warned.

将其保存为 .vbs 并使用命令提示符或双击运行它。

【讨论】:

  • 酷,这正是我想要的。非常感谢。
  • 谢谢!我试图在 JavaScript 中使用 xlCenter,但我不知道从哪里获取常量对象。这在 Excel 2007 中完美运行。
【解决方案2】:

有很多方法可以选择一个单元格或一系列单元格,但以下方法适用于单个单元格。

'Select a Cell Range
Range("D4").Select

'Set the horizontal and vertical alignment
With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlBottom
End With

Horizo​​ntalAlignment 选项是 xlLeft、xlRight 和 xlCenter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 2020-09-26
    相关资源
    最近更新 更多