ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2022-02-08 17:32:59  阅读:344  来源: 互联网

标签:A1 style C# worksheet Cells 单元格 cell workbook


//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

 

标签:A1,style,C#,worksheet,Cells,单元格,cell,workbook
来源: https://www.cnblogs.com/Mars-0603/p/15872104.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有