excel中离不开各种各样的公式计算,在Epplus中运用公式有两种方式,你都可以尝试一下:
worksheet.Cells["D2:D5"].Formula="B2*C2";//这是乘法的公式,意思是第二列乘以第三列的值赋值给第四列,这种方法比较简单明了worksheet.Cells[6,2,6,4].Formula=string.Format("SUBTOTAL(9,{0})",newExcelAddress(2,2,5,2).Address);//这是自动求和的方法,至于subtotal的用法你需要自己去了解了至于别的公式大家可以自己尝试一下。
2、设置单元格格式
worksheet.Cells[5,3].Style.Numberformat.Format="#,##0.00";//这是保留两位小数单元格的格式设置还有很多,我就不一一列出来了,基本上excel上能实现的Epplus都能实现,大家可以去Epplus的源码上看。
3、设置字体和单元格样式
设置单元格对齐方式
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;//设置列宽4、设置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");//设置背景图片5、插入图片和形状
插入图片
ExcelPicturepicture=worksheet.Drawings.AddPicture("logo",Image.FromFile(@"firstbg.jpg"));//插入图片picture.SetPosition(100,100);//设置图片的位置picture.SetSize(100,100);//设置图片的大小插入形状
ExcelShapeshape=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、超链接
给图片加超链接
worksheet.Hidden=eWorkSheetHidden.Hidden;//隐藏sheetworksheet.Column(1).Hidden=true;//隐藏某一列worksheet.Row(1).Hidden=true;//隐藏某一行