格式单元格
介绍
使用GetStyle和SetStyle方法格式化单元格
在单元格上应用不同种类的格式样式,设置背景或前景颜色、边框、字体、水平和垂直对齐、缩进级别、文本方向、旋转角度等。
使用GetStyle和SetStyle方法
如果开发人员需要对不同单元格应用不同的格式样式,则最好使用Style方法获取单元格,使用Cell.GetStyle方法指定样式属性,然后使用Cell.SetStyle方法应用格式。下面给出了一个示例,演示了如何在单元格上应用各种格式。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Hello Aspose!"); | |
// Defining a Style object | |
Aspose.Cells.Style style; | |
// Get the style from A1 cell | |
style = cell.GetStyle(); | |
// Setting the vertical alignment | |
style.VerticalAlignment = TextAlignmentType.Center; | |
// Setting the horizontal alignment | |
style.HorizontalAlignment = TextAlignmentType.Center; | |
// Setting the font color of the text | |
style.Font.Color = Color.Green; | |
// Setting to shrink according to the text contained in it | |
style.ShrinkToFit = true; | |
// Setting the bottom border color to red | |
style.Borders[BorderType.BottomBorder].Color = Color.Red; | |
// Setting the bottom border type to medium | |
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium; | |
// Applying the style to A1 cell | |
cell.SetStyle(style); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
如何使用样式对象为不同单元格设置格式
如果开发人员需要将相同的格式应用于不同的单元格,他们可以使用Style对象。请按照下面的步骤使用Style对象:
- 通过调用Workbook类的CreateStyle方法添加Style对象
- 访问新添加的Style对象
- 设置Style对象的所需属性/属性,以应用所需的格式设置
- 将配置的Style对象分配给所需的单元格
这种方法可以极大地提高您的应用程序的效率,并节省内存。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Excel object | |
int i = workbook.Worksheets.Add(); | |
// Obtaining the reference of the first worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[i]; | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Hello Aspose!"); | |
// Adding a new Style | |
Style style = workbook.CreateStyle(); | |
// Setting the vertical alignment of the text in the "A1" cell | |
style.VerticalAlignment = TextAlignmentType.Center; | |
// Setting the horizontal alignment of the text in the "A1" cell | |
style.HorizontalAlignment = TextAlignmentType.Center; | |
// Setting the font color of the text in the "A1" cell | |
style.Font.Color = Color.Green; | |
// Shrinking the text to fit in the cell | |
style.ShrinkToFit = true; | |
// Setting the bottom border color of the cell to red | |
style.Borders[BorderType.BottomBorder].Color = Color.Red; | |
// Setting the bottom border type of the cell to medium | |
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium; | |
// Assigning the Style object to the "A1" cell | |
cell.SetStyle(style); | |
// Apply the same style to some other cells | |
worksheet.Cells["B1"].SetStyle(style); | |
worksheet.Cells["C1"].SetStyle(style); | |
worksheet.Cells["D1"].SetStyle(style); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
如何使用Microsoft Excel 2007预定义样式
如果您需要为Microsoft Excel 2007应用不同的格式样式,请使用Aspose.Cells API应用样式。下面的示例演示了如何使用这种方法在单元格上应用预定义样式。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Create a style object . | |
Style style = workbook.CreateStyle(); | |
// Input a value to A1 cell. | |
workbook.Worksheets[0].Cells["A1"].PutValue("Test"); | |
// Apply the style to the cell. | |
workbook.Worksheets[0].Cells["A1"].SetStyle(style); | |
// Save the Excel 2007 file. | |
workbook.Save(dataDir + "book1.out.xlsx"); |
如何格式化单元格中的选定字符
处理字体设置解释了如何格式化单元格中的文本,但它只解释了如何格式化所有单元格内容。如果您只想格式化选定的字符怎么办?
Aspose.Cells也支持此功能。本主题解释了如何有效使用此功能。
如何格式化选定的字符
Aspose.Cells提供一个代表Microsoft Excel文件的类Workbook。Workbook类包含允许访问Excel文件中每个工作表的Worksheets集合。工作表由Worksheet类表示。Worksheet类提供了Cells集合。Cells集合中的每个项都表示Cell类的对象。
Cell类提供Characters方法,该方法接受以下参数以选择单元格中的字符范围:
- 起始索引,选择开始的字符的索引。
- 字符数,要选择的字符数。
Characters方法返回FontSetting类的实例,允许开发人员以与单元格相同的方式格式化字符,如下面的代码示例所示。在输出文件中,A1单元格中的’Visit’单词将使用默认字体格式,但’Aspose!‘将以粗体和蓝色格式显示。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first(default) worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Visit Aspose!"); | |
// Setting the font of selected characters to bold | |
cell.Characters(6, 7).Font.IsBold = true; | |
// Setting the font color of selected characters to blue | |
cell.Characters(6, 7).Font.Color = Color.Blue; | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
如何格式化行和列
有时,开发人员需要在行或列上应用相同的格式。逐个单元格应用格式通常需要更长时间,不是一个好的解决方案。 为解决这个问题,Aspose.Cells提供了一个在本文中详细讨论的简单、快速的方法。
格式化行和列
Aspose.Cells提供了一个类,Workbook,它代表Microsoft Excel文件。Workbook类包含一个Worksheets集合,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了一个Cells集合。Cells集合提供了一个Rows集合。
如何格式化一行
集合中的每个项目代表一个Row对象。Row对象提供了ApplyStyle方法,用于设置行的格式。要将相同的格式应用于一行,请使用Style对象。以下步骤显示如何使用它。
- 通过调用其CreateStyle方法向Workbook类添加一个Style对象。
- 设置Style对象的属性以应用格式设置。
- 使StyleFlag对象的相关属性为ON。
- 将配置的Style对象分配给Row对象。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first (default) worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding a new Style to the styles | |
Style style = workbook.CreateStyle(); | |
// Setting the vertical alignment of the text in the "A1" cell | |
style.VerticalAlignment = TextAlignmentType.Center; | |
// Setting the horizontal alignment of the text in the "A1" cell | |
style.HorizontalAlignment = TextAlignmentType.Center; | |
// Setting the font color of the text in the "A1" cell | |
style.Font.Color = Color.Green; | |
// Shrinking the text to fit in the cell | |
style.ShrinkToFit = true; | |
// Setting the bottom border color of the cell to red | |
style.Borders[BorderType.BottomBorder].Color = Color.Red; | |
// Setting the bottom border type of the cell to medium | |
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium; | |
// Creating StyleFlag | |
StyleFlag styleFlag = new StyleFlag(); | |
styleFlag.HorizontalAlignment = true; | |
styleFlag.VerticalAlignment = true; | |
styleFlag.ShrinkToFit = true; | |
styleFlag.Borders = true; | |
styleFlag.FontColor = true; | |
// Accessing a row from the Rows collection | |
Row row = worksheet.Cells.Rows[0]; | |
// Assigning the Style object to the Style property of the row | |
row.ApplyStyle(style, styleFlag); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
如何格式化一列
Cells集合还提供一个Columns集合。集合中的每个项目代表一个Column对象。类似于Row对象,Column对象还提供了ApplyStyle方法,用于格式化列。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first (default) worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding a new Style to the styles | |
Style style = workbook.CreateStyle(); | |
// Setting the vertical alignment of the text in the "A1" cell | |
style.VerticalAlignment = TextAlignmentType.Center; | |
// Setting the horizontal alignment of the text in the "A1" cell | |
style.HorizontalAlignment = TextAlignmentType.Center; | |
// Setting the font color of the text in the "A1" cell | |
style.Font.Color = Color.Green; | |
// Shrinking the text to fit in the cell | |
style.ShrinkToFit = true; | |
// Setting the bottom border color of the cell to red | |
style.Borders[BorderType.BottomBorder].Color = Color.Red; | |
// Setting the bottom border type of the cell to medium | |
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium; | |
// Creating StyleFlag | |
StyleFlag styleFlag = new StyleFlag(); | |
styleFlag.HorizontalAlignment = true; | |
styleFlag.VerticalAlignment = true; | |
styleFlag.ShrinkToFit = true; | |
styleFlag.Borders = true; | |
styleFlag.FontColor = true; | |
// Accessing a column from the Columns collection | |
Column column = worksheet.Cells.Columns[0]; | |
// Applying the style to the column | |
column.ApplyStyle(style, styleFlag); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |