Export of Epplus Excel Tutorial 2 (styling)

 Export of Epplus Excel Tutorial 1 (Basic Introduction)

 Export of Epplus Excel Tutorial 2 (styling) 

 Export of Epplus Excel Tutorial 3 (chart settings)  

 Export of Epplus Excel Tutorial 4 (additional settings)

 

1, formula

     excel in a variety of formula can not be separated, use a formula in Epplus There are two ways that you can try:

1
2
worksheet.Cells[ "D2:D5" ].Formula =  "B2*C2" ; //这是乘法的公式,意思是第二列乘以第三列的值赋值给第四列,这种方法比较简单明了
worksheet.Cells[6, 2, 6, 4].Formula =  string .Format( "SUBTOTAL(9,{0})" new  ExcelAddress(2, 2, 5, 2).Address); //这是自动求和的方法,至于subtotal的用法你需要自己去了解了

    As for the other formula you can try it yourself.

2, format cells

1
worksheet.Cells[5, 3].Style.Numberformat.Format =  "#,##0.00" ; //这是保留两位小数

 Formatting cells, there are many, I will not list them here, basically on excel can achieve Epplus can achieve, you can go Epplus source point of view.

3, set the font styles and cell

   Set Cell Alignment   

1
2
3
4
worksheet.Cells[1, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; //水平居中
worksheet.Cells[1, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center; //垂直居中
worksheet.Cells[1, 4, 1, 5].Merge =  true ; //合并单元格
worksheet.Cells.Style.WrapText =  true ; //自动换行

 Set cell font style

1
2
3
4
worksheet.Cells[1, 1].Style.Font.Bold =  true ; //字体为粗体
worksheet.Cells[1, 1].Style.Font.Color.SetColor(Color.White); //字体颜色
worksheet.Cells[1, 1].Style.Font.Name =  "微软雅黑" ; //字体
worksheet.Cells[1, 1].Style.Font.Size = 12; //字体大小

 Cell background pattern provided

1
2
worksheet.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(128, 128, 128)); //设置单元格背景色

 Cell borders, two methods

1
2
3
worksheet.Cells[1, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); //设置单元格所有边框
worksheet.Cells[1, 1].Style.Border.Bottom.Style = ExcelBorderStyle.Thin; //单独设置单元格底部边框样式和颜色(上下左右均可分开设置)
worksheet.Cells[1, 1].Style.Border.Bottom.Color.SetColor(Color.FromArgb(191, 191, 191)); 

   Set the cell row height and column width

1
2
3
4
worksheet.Cells.Style.ShrinkToFit =  true ; //单元格自动适应大小
worksheet.Row(1).Height = 15; //设置行高
worksheet.Row(1).CustomHeight =  true ; //自动调整行高
worksheet.Column(1).Width = 15; //设置列宽

4, set the sheet background

1
2
3
4
worksheet.View.ShowGridLines =  false ; //去掉sheet的网格线
worksheet.Cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells.Style.Fill.BackgroundColor.SetColor(Color.LightGray); //设置背景色
worksheet.BackgroundImage.Image = Image.FromFile( @"firstbg.jpg" ); //设置背景图片

5, inserting pictures and shapes

   Insert Picture

1
2
3
ExcelPicture picture = worksheet.Drawings.AddPicture( "logo" , Image.FromFile( @"firstbg.jpg" )); //插入图片
picture.SetPosition(100, 100); //设置图片的位置
picture.SetSize(100, 100); //设置图片的大小

 Insert Shape

1
2
3
4
5
6
7
8
9
ExcelShape shape = worksheet.Drawings.AddShape( "shape" , eShapeStyle.Rect); //插入形状
shape.Font.Color = Color.Red; //设置形状的字体颜色
shape.Font.Size = 15; //字体大小
shape.Font.Bold =  true ; //字体粗细
shape.Fill.Style = eFillStyle.NoFill; //设置形状的填充样式
shape.Border.Fill.Style = eFillStyle.NoFill; //边框样式
shape.SetPosition(200, 300); //形状的位置
shape.SetSize(80, 30); //形状的大小
shape.Text =  "test" ; //形状的内容

 Epplus which built many shapes, you can try it for yourself.

6, hyperlinks

    Add hyperlinks to pictures

1
ExcelPicture picture = worksheet.Drawings.AddPicture( "logo" , Image.FromFile( @"firstbg.jpg" ),  new  ExcelHyperLink( "http:\\www.baidu.com" , UriKind.Relative));

  Giga hyperlink to the unit

1
worksheet.Cells[1, 1].Hyperlink =  new  ExcelHyperLink( "http:\\www.baidu.com" , UriKind.Relative);

7, hidden sheet

1
2
3
worksheet.Hidden = eWorkSheetHidden.Hidden; //隐藏sheet
worksheet.Column(1).Hidden =  true ; //隐藏某一列
worksheet.Row(1).Hidden =  true ; //隐藏某一行

Guess you like

Origin www.cnblogs.com/itjeff/p/12055824.html