EPPLUS中单元格的属性

//1,赋值
worksheet.Cells[int row, int col].Value = "xxx";
//或者
worksheet.Cells["A1"].Value = "xxx";
//或者
worksheet.SetValue(row,col,value);

//2,单元格合并

//指定开始行,开始列,结束行,结束列
worksheet.Cells[fromRow, fromCol, toRow, toCol].Merge = true;

//行合并
worksheet.Cells["A1:A5"].Merge = true;//合并A列的1-5行

//列合并
worksheet.Cells["A1:G1"].Merge = true;//合并第1行的A-G列

 

//3,样式

worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;//水平居中,全局
worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;//垂直居中,全局
worksheet.Cells.AutoFitColumns();//全局
worksheet.Cells.Style.WrapText = true;//自动换行,全局
worksheet.Cells.Style.Font.Name = "宋体";//全局

worksheet.Cells["A1"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;//垂直居中,只针对特定单元格

worksheet.Cells["A1:A5"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;//垂直居中,只针对某范围单元格

worksheet.Cells[1, 1].Style.Font.Bold = true;//字体为粗体
worksheet.Cells[1, 1].Style.Font.Color.SetColor(Color.White);//字体颜色
worksheet.Cells[1, 1].Style.Font.Size = 12;//字体大小
 

worksheet.Cells["A5"].Style.TextRotation = 180;//内容旋转

worksheet.Cells["P5"].Style.SetTextVertical(); //文字竖排

 

//调整行高
double rowHeight = 15;
worksheet.Row(1).Height = rowHeight;

//调整列宽
double columnWidth = 50;
worksheet.Column(1).Width = columnWidth;

//自动适应长宽

worksheet.Column(1).BestFit = true;

//公式计算


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的用法你需要自己去了解了
    至于别的公式大家可以自己尝试一下。

//设置单元格格式
worksheet.Cells[5, 3].Style.Numberformat.Format = "#,##0.00";//这是保留两位小数
 单元格的格式设置还有很多,我就不一一列出来了,基本上excel上能实现的Epplus都能实现,大家可以去Epplus的源码上看。


// 设置单元格背景样式


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";//形状的内容

//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;//隐藏某一行
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dudke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值