Aspose.Cells如何设置Excel单元格格式

作者:控件中国网   出处:控件中国网   2015-12-02 14:26:21   阅读:110

如果Excel工作表具有良好的格式会让客户更容易直观地读取和分析数据,Aspose.Cells提供多种方法用于格式化单元格和内容,Aspose.Cells为单元格类提供了GetStyle和SetStyle方法,这两个方法用于设置和获取单元格样式,并且控件还提供了一个Style类,可以帮助开发人员为单元格设置不同的格式化样式,包括单元格背景色、边框、字体、垂直和水平对齐方式、缩进层次、文本方向、旋转角度等,下面的代码主要用于各种情况下设置单元格格式:
 
1.Aspose.Cells提供的GetStyle和SetStyle方法的具体用法可以参考下面的代码:
 
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Clear the worksheets
workbook.Worksheets.Clear();
//Adding a new worksheet to the Workbook 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!");
//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("c:\\book1.xls");
 
2.使用Style对象来格式化不同的Excel单元格
Workbook workbook = new Workbook();
//Clear the worksheets
workbook.Worksheets.Clear();
//Adding a new worksheet to the Workbook 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 to the styles collection of the Excel object
//Accessing the newly added Style to the Workbook object
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("C:\\book1.xls");
 
3.Aspose.Cells使用Microsoft Excel 2007预定义样式
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Create a style object based on a predefined Excel 2007 style.
Style style = workbook.Styles.CreateBuiltinStyle(BuiltinStyleType.Accent1);
//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(@"e:\test\MyFile.xlsx");
 
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat