导出Excel之Epplus使用教程2(样式设置) | 您所在的位置:网站首页 › excel表格怎么设置表样式 › 导出Excel之Epplus使用教程2(样式设置) |
导出Excel之Epplus使用教程1(基本介绍) 导出Excel之Epplus使用教程2(样式设置) 导出Excel之Epplus使用教程3(图表设置) 导出Excel之Epplus使用教程4(其他设置)
1、公式计算 excel中离不开各种各样的公式计算,在Epplus中运用公式有两种方式,你都可以尝试一下: 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、设置单元格格式 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、插入图片和形状 插入图片 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里面内置了很多形状,大家可以自己试一试。 6、超链接 给图片加超链接 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);7、隐藏sheet worksheet.Hidden = eWorkSheetHidden.Hidden;//隐藏sheet worksheet.Column(1).Hidden = true;//隐藏某一列 worksheet.Row(1).Hidden = true;//隐藏某一行
|
CopyRight 2018-2019 实验室设备网 版权所有 |