如何从 VSTO 代码创建带边框的样式?
How can I create a style with borders from VSTO code?
我正在使用 VSTO 在 C# 中开发一个 Excel 插件。在这个插件中,我创建了一些样式并将它们添加到当前工作簿中。一切正常,直到我尝试为我的风格设置一些边界。
这是我使用的代码:
var style = workbook.styles.Add("My style");
style.IncludeAlignment = false;
style.IncludeFont = false;
style.IncludeNumber = false;
style.IncludeProtection = false;
style.IncludePatterns = false;
style.IncludeBorder = true;
foreach (XlBordersIndex borderIndex in new[] {
XlBordersIndex.xlEdgeLeft,
XlBordersIndex.xlEdgeRight,
XlBordersIndex.xlEdgeTop,
XlBordersIndex.xlEdgeBottom })
{
style.Borders[borderIndex].LineStyle = XlLineStyle.xlContinuous;
}
我希望这段代码创建一个设置了四个边框的样式,但似乎只设置了左边缘(我可以通过在循环后查看调试器中的样式对象看到这一点,并在Excel 通过编辑 "My style").
我尝试录制一个 VBA 宏来查看 Excel 生成的代码,我得到了这个:
ActiveWorkbook.Styles.Add Name:="My style"
With ActiveWorkbook.Styles("My style")
.IncludeNumber = False
.IncludeFont = False
.IncludeAlignment = False
.IncludeBorder = True
.IncludePatterns = False
.IncludeProtection = False
End With
With ActiveWorkbook.Styles("My style").Borders(xlLeft)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
With ActiveWorkbook.Styles("My style").Borders(xlRight)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
With ActiveWorkbook.Styles("My style").Borders(xlTop)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
With ActiveWorkbook.Styles("My style").Borders(xlBottom)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
此宏按预期工作。我注意到它使用 xlTop
、xlLeft
等而不是 xlEdgeTop
、xlEdgeLeft
,但我找不到关于这些常量的任何文档,它们在VSTO 枚举 XlBordersIndex
。从我发现的情况来看,后者似乎代表范围的边缘,而前者代表每个单元格的边缘,但我对此不确定,我认为在谈论样式时差异没有多大意义,适用于单个单元格 AFAICT。
为什么我的 C# 插件和这个 VBA 宏之间有不同的行为?如何从我的 VSTO 代码创建带边框的样式?
根据 this discussion, it seems that there is a difference between the borders of a style and the borders of a range: the latter are indexed by XlBordersIndex
(xlEdgeTop
, xlEdgeLeft
,...), as stated in the documentation, but the former are indexed by Constants
(xlTop
、xlLeft
、...)。
如果我们认为样式适用于单个单元格而不适用于范围,这可能是有道理的,但这也意味着要访问样式边框,我们必须绕过 Borders
接口的接口,使用不相关的常量。 AnushRudaa here 提出的这个解决方法似乎有效:
foreach (XlBordersIndex borderIndex in new[] {
(XlBordersIndex)Constants.xlLeft,
(XlBordersIndex)Constants.xlRight,
(XlBordersIndex)Constants.xlTop,
(XlBordersIndex)Constants.xlBottom })
{
style.Borders[borderIndex].LineStyle = XlLineStyle.xlContinuous;
}
我正在使用 VSTO 在 C# 中开发一个 Excel 插件。在这个插件中,我创建了一些样式并将它们添加到当前工作簿中。一切正常,直到我尝试为我的风格设置一些边界。
这是我使用的代码:
var style = workbook.styles.Add("My style");
style.IncludeAlignment = false;
style.IncludeFont = false;
style.IncludeNumber = false;
style.IncludeProtection = false;
style.IncludePatterns = false;
style.IncludeBorder = true;
foreach (XlBordersIndex borderIndex in new[] {
XlBordersIndex.xlEdgeLeft,
XlBordersIndex.xlEdgeRight,
XlBordersIndex.xlEdgeTop,
XlBordersIndex.xlEdgeBottom })
{
style.Borders[borderIndex].LineStyle = XlLineStyle.xlContinuous;
}
我希望这段代码创建一个设置了四个边框的样式,但似乎只设置了左边缘(我可以通过在循环后查看调试器中的样式对象看到这一点,并在Excel 通过编辑 "My style").
我尝试录制一个 VBA 宏来查看 Excel 生成的代码,我得到了这个:
ActiveWorkbook.Styles.Add Name:="My style"
With ActiveWorkbook.Styles("My style")
.IncludeNumber = False
.IncludeFont = False
.IncludeAlignment = False
.IncludeBorder = True
.IncludePatterns = False
.IncludeProtection = False
End With
With ActiveWorkbook.Styles("My style").Borders(xlLeft)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
With ActiveWorkbook.Styles("My style").Borders(xlRight)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
With ActiveWorkbook.Styles("My style").Borders(xlTop)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
With ActiveWorkbook.Styles("My style").Borders(xlBottom)
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlThin
End With
此宏按预期工作。我注意到它使用 xlTop
、xlLeft
等而不是 xlEdgeTop
、xlEdgeLeft
,但我找不到关于这些常量的任何文档,它们在VSTO 枚举 XlBordersIndex
。从我发现的情况来看,后者似乎代表范围的边缘,而前者代表每个单元格的边缘,但我对此不确定,我认为在谈论样式时差异没有多大意义,适用于单个单元格 AFAICT。
为什么我的 C# 插件和这个 VBA 宏之间有不同的行为?如何从我的 VSTO 代码创建带边框的样式?
根据 this discussion, it seems that there is a difference between the borders of a style and the borders of a range: the latter are indexed by XlBordersIndex
(xlEdgeTop
, xlEdgeLeft
,...), as stated in the documentation, but the former are indexed by Constants
(xlTop
、xlLeft
、...)。
如果我们认为样式适用于单个单元格而不适用于范围,这可能是有道理的,但这也意味着要访问样式边框,我们必须绕过 Borders
接口的接口,使用不相关的常量。 AnushRudaa here 提出的这个解决方法似乎有效:
foreach (XlBordersIndex borderIndex in new[] {
(XlBordersIndex)Constants.xlLeft,
(XlBordersIndex)Constants.xlRight,
(XlBordersIndex)Constants.xlTop,
(XlBordersIndex)Constants.xlBottom })
{
style.Borders[borderIndex].LineStyle = XlLineStyle.xlContinuous;
}