1、公式計算
excel中離不開各種各樣的公式計算,在Epplus中運用公式有兩種方式,你都可以嘗試一下:
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的用法你需要自己去了解了
|
至於別的公式大家可以自己嘗試一下。
2、設置單元格格式
1
|
worksheet.Cells[5, 3].Style.Numberformat.Format =
"#,##0.00"
;
//這是保留兩位小數
|
單元格的格式設置還有很多,我就不一一列出來了,基本上excel上能實現的Epplus都能實現,大家可以去Epplus的源碼上看。
3、設置字體和單元格樣式
設置單元格對齊方式
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
;
//自動換行
|
設置單元格字體樣式
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;
//字體大小
|
設置單元格背景樣式
1
2
|
worksheet.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(128, 128, 128));
//設置單元格背景色
|
設置單元格邊框,兩種方法
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));
|
設置單元格的行高和列寬
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、設置sheet背景
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、插入圖片和形狀
插入圖片
1
2
3
|
ExcelPicture picture = worksheet.Drawings.AddPicture(
"logo"
, Image.FromFile(
@"firstbg.jpg"
));
//插入圖片
picture.SetPosition(100, 100);
//設置圖片的位置
picture.SetSize(100, 100);
//設置圖片的大小
|
插入形狀
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里面內置了很多形狀,大家可以自己試一試。
6、超鏈接
給圖片加超鏈接
1
|
ExcelPicture picture = worksheet.Drawings.AddPicture(
"logo"
, Image.FromFile(
@"firstbg.jpg"
),
new
ExcelHyperLink(
"http:\\www.baidu.com"
, UriKind.Relative));
|
給單元格加超鏈接
1
|
worksheet.Cells[1, 1].Hyperlink =
new
ExcelHyperLink(
"http:\\www.baidu.com"
, UriKind.Relative);
|
7、隱藏sheet
1
2
3
|
worksheet.Hidden = eWorkSheetHidden.Hidden;
//隱藏sheet
worksheet.Column(1).Hidden =
true
;
//隱藏某一列
worksheet.Row(1).Hidden =
true
;
//隱藏某一行
|