HOW TO:將樣式套用至活頁簿中的範圍
更新: 2008 年 7 月
適用於 |
---|
本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。 專案類型
Microsoft Office 版本
如需詳細資訊,請參閱依應用程式和專案類型提供的功能。 |
您可以將具名樣式套用至活頁簿中的區域。Excel 提供了一些預先定義的樣式。
[設定儲存格格式] 對話方塊會顯示可用於格式化儲存格的所有選項,而且當中每個選項都可以從程式碼加以使用。若要在 Excel 中顯示這個對話方塊,請按一下 [格式] 功能表上的 [儲存格]。
若要使用文件層級自訂將樣式套用至命名範圍
建立新樣式並設定其屬性。這段程式碼必須放置在工作表類別中,而不是 ThisWorkbook 類別中。
Dim style As Excel.Style = Globals.ThisWorkbook.Styles.Add("NewStyle") style.Font.Name = "Verdana" style.Font.Size = 12 style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red) style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray) style.Interior.Pattern = Excel.XlPattern.xlPatternSolid
Excel.Style style = Globals.ThisWorkbook.Styles.Add("NewStyle", missing); style.Font.Name = "Verdana"; style.Font.Size = 12; style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray); style.Interior.Pattern = Excel.XlPattern.xlPatternSolid;
建立 NamedRange 控制項,並將文字指派給它,然後套用新樣式。這段程式碼必須放置在工作表類別中,而不是 ThisWorkbook 類別中。
Dim rangeStyles As Microsoft.Office.Tools.Excel.NamedRange = _ Me.Controls.AddNamedRange(Me.Range("A1"), "rangeStyles") rangeStyles.Value2 = "'Style Test" rangeStyles.Style = "NewStyle" rangeStyles.Columns.AutoFit()
Microsoft.Office.Tools.Excel.NamedRange rangeStyles = this.Controls.AddNamedRange(this.Range["A1", missing], "rangeStyles"); rangeStyles.Value2 = "'Style Test"; rangeStyles.Style = "NewStyle"; rangeStyles.Columns.AutoFit();
若要使用應用程式層級增益集將樣式套用至命名範圍
建立新樣式並設定其屬性。
Dim style As Excel.Style = Me.Application.ActiveWorkbook.Styles.Add("NewStyle") style.Font.Name = "Verdana" style.Font.Size = 12 style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red) style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray) style.Interior.Pattern = Excel.XlPattern.xlPatternSolid
Excel.Style style = this.Application.ActiveWorkbook.Styles.Add("NewStyle", missing); style.Font.Name = "Verdana"; style.Font.Size = 12; style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray); style.Interior.Pattern = Excel.XlPattern.xlPatternSolid;
建立 Microsoft.Office.Interop.Excel.Range,並指派文字給它,然後套用新樣式。
Dim rangeStyles As Excel.Range = Me.Application.Range("A1") rangeStyles.Value2 = "'Style Test" rangeStyles.Style = "NewStyle" rangeStyles.Columns.AutoFit()
Excel.Range rangeStyles = this.Application.get_Range("A1", missing); rangeStyles.Value2 = "'Style Test"; rangeStyles.Style = "NewStyle"; rangeStyles.Columns.AutoFit();
請參閱
工作
概念
全域存取 Visual Studio Tools for Office 專案中的物件
變更記錄
日期 |
記錄 |
原因 |
---|---|---|
2008 年 7 月 |
加入可以用於應用程式層級增益集的程式碼範例。 |
客戶回函。 |