導出Excel之Epplus使用教程2(樣式設置)

 導出Excel之Epplus使用教程1(基本介紹)html

 導出Excel之Epplus使用教程2(樣式設置) 字體

 導出Excel之Epplus使用教程3(圖表設置)  spa

 導出Excel之Epplus使用教程4(其餘設置)excel

 

一、公式計算orm

     excel中離不開各類各樣的公式計算,在Epplus中運用公式有兩種方式,你均可以嘗試一下:htm

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的用法你須要本身去了解了

    至於別的公式你們能夠本身嘗試一下。blog

二、設置單元格格式教程

worksheet.Cells[5, 3].Style.Numberformat.Format = "#,##0.00";//這是保留兩位小數

 單元格的格式設置還有不少,我就不一一列出來了,基本上excel上能實現的Epplus都能實現,你們能夠去Epplus的源碼上看。圖片

三、設置字體和單元格樣式get

   設置單元格對齊方式   

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;//自動換行

 設置單元格字體樣式

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;//字體大小

 設置單元格背景樣式

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

 設置單元格邊框,兩種方法

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)); 

   設置單元格的行高和列寬

worksheet.Cells.Style.ShrinkToFit = true;//單元格自動適應大小
worksheet.Row(1).Height = 15;//設置行高
worksheet.Row(1).CustomHeight = true;//自動調整行高
worksheet.Column(1).Width = 15;//設置列寬

四、設置sheet背景

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");//設置背景圖片

五、插入圖片和形狀

   插入圖片

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

 插入形狀

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裏面內置了不少形狀,你們能夠本身試一試。

六、超連接

    給圖片加超連接

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

  給單元格加超連接

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

七、隱藏sheet

worksheet.Hidden = eWorkSheetHidden.Hidden;//隱藏sheet
worksheet.Column(1).Hidden = true;//隱藏某一列
worksheet.Row(1).Hidden = true;//隱藏某一行
相關文章
相關標籤/搜索