【C#】Aspose.Cells 设置单元格样式

2022/2/8 20:17:12

本文主要是介绍【C#】Aspose.Cells 设置单元格样式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
//Adding the current system date to "A1" cell
worksheet.Cells["A1"].PutValue(DateTime.Now);
//Getting the Style of the A1 Cell
Style style = worksheet.Cells["A1"].GetStyle();
//Setting the display format to number 15 to show date as "d-mmm-yy"
style.Number = 15;
//Applying the style to the A1 cell
worksheet.Cells["A1"].SetStyle(style);
//Adding a numeric value to "A2" cell
worksheet.Cells["A2"].PutValue(20);
//Getting the Style of the A2 Cell
style = worksheet.Cells["A2"].GetStyle();
//Setting the display format to number 9 to show value as percentage
style.Number = 9;
//Applying the style to the A2 cell
worksheet.Cells["A2"].SetStyle(style);
//Adding a numeric value to "A3" cell
worksheet.Cells["A3"].PutValue(2546);
//Getting the Style of the A3 Cell
style = worksheet.Cells["A3"].GetStyle();
//Setting the display format to number 6 to show value as currency
style.Number = 6;
//Applying the style to the A3 cell
worksheet.Cells["A3"].SetStyle(style);
//Saving the Excel file
workbook.Save("C:\\book1.xls", SaveFormat.Excel97To2003);
当然开发人员还可以为单元格设置自定义显示样式,下面的代码就怎么设置单元格自定义显示样式做举例:
//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 newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
//Adding the current system date to "A1" cell
worksheet.Cells["A1"].PutValue(DateTime.Now);
//Getting the style of A1 cell
Style style = worksheet.Cells["A1"].GetStyle();
//Setting the custom display format to show date as "d-mmm-yy"
style.Custom = "d-mmm-yy";
//Applying the style to A1 cell
worksheet.Cells["A1"].SetStyle(style);
//Adding a numeric value to "A2" cell
worksheet.Cells["A2"].PutValue(20);
//Getting the style of A2 cell
style = worksheet.Cells["A2"].GetStyle();
//Setting the custom display format to show value as percentage
style.Custom = "0.0%";
//Applying the style to A2 cell
worksheet.Cells["A2"].SetStyle(style);
//Adding a numeric value to "A3" cell
worksheet.Cells["A3"].PutValue(2546);
//Getting the style of A3 cell
style = worksheet.Cells["A3"].GetStyle();
//Setting the custom display format to show value as currency
style.Custom = "£#,##0;[Red]$-#,##0";
//Applying the style to A3 cell
worksheet.Cells["A3"].SetStyle(style);
//Saving the Excel file
workbook.Save("C:\\book1.xls", SaveFormat.Excel97To2003);

 

 

using Aspose.Cells;    
using System.Drawing;
 
Workbook workbook=new Workbook();
Style style=workbook.Styles[workbook.Styles.Add()];
WorkSheet worksheet=workbook.WorkSheet[0];
//字体样式
style.Font.Color = Color.Red;//字体颜色
style.Font.Size = 10;//字体大小
style.Font.IsBold = true;//字体加粗
style.Font.Name = "宋体";//文字字体 
 
//单元格样式
//单元格背景颜色
style.ForegroundColor = Color.Red;//红色
style.ForegroundColor = Color.Gray;//灰色
style.ForegroundColor = Color.Yellow;//黄色
style.ForegroundColor = Color.Magenta;//紫红色
style.ForegroundColor = Color.Orange;//橙色
style.ForegroundColor = Color.Pink;//粉红
style.ForegroundColor = Color.Aqua;//浅蓝
style.ForegroundColor = Color.PaleGreen;//浅绿
 
style.Pattern = BackgroundType.Solid;
style.HorizontalAlignment = TextAlignmentType.Center;//水平居中
style.IsTextWrapped = true;//单元格内容自动换行
//边框样式
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; //左边框 
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //右边框  
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; //上边框  
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //下边框
 
Range range= worksheet.Cells.CreateRange(0, 0, 1, 1);//第一行第一列单元格
range.ApplyStyle(style, new StyleFlag() { All=true});

 

https://www.cnblogs.com/mahatmasmile/p/7806118.html

https://blog.csdn.net/qq_38974638/article/details/108631101

 



这篇关于【C#】Aspose.Cells 设置单元格样式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程