EPPLus语法应用

EPPLus

语法

  • 初始化EPPlus对象
using(ExcelPackage package = new ExcelPackage(new FileInfo(@"D:\Sample.xlsx")))
{
    //所有生成xlsx的语句必须写在using领域内部
}

以下语句必须在using语句中编写

  • 初始化Sheet()
ExcelWorksheet sheet = package.Workbook.Worksheets.Add("sheetName");
  • 单元格赋值
   sheet.Cells[1, 1].Value = "ID";
   sheet.Cells[1, 2].Value = "Product";
   sheet.Cells["A4"].Value = "Price";
  • 合并单元格
    //将从[a,b]到[A,B]中间的单元格合并。
    sheet.Cells[int a,int b,int A,int B].Merge = True;
  • 获取某一单元格
    var cell = sheet.Cells[1,1];
  • 字体样式
    //字体名
    sheet.Cells[2, 1, 2, 10].Style.Font.Name = "宋体";
    //字体粗体
    sheet.Cells[2, 1, 2, 10].Style.Font.Bold = True;
    //字体大小
    sheet.Cells[1, 1].Style.Font.Size = 1;
    
  • 单元格对齐
    //水平对齐
    Sheet.Cells[1,1,a,b].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
    //垂直对齐
    worksheet.Cells.Style.VerticalAlignment =  ExcelVerticalAlignment.Bottom; 
  • 表单整体颜色
     worksheet.Cells.Style.Fill.PatternType= ExcelFillStyle.Solid; 
    worksheet.Cells.Style.Fill.BackgroundColor.SetColor(Color.LightBlue); 
  • 自动换行
    sheet.Cells[a,b].Style.WrapText= true;
  • 格式化单元格值
    sheet.Cells[a,b,A,B].Style.Numberformat.Format= "0.00";
  • 单元格内容颜色
    sheet.Cells[1, 1].Style.Font.Color.SetColor(Color.Red);
  • 单元格边框
    //设置边框,及其颜色
    sheet.Cells[1, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
  • 设置宽高
    //设置列宽
    sheet.Column(1).Width = 15;
    //WPF 以与设备无关的单位(每个单位 1/96 英寸)。
    sheet.Column(1).Width=column.ActualWidth;
    //设置行高
    sheet.Row(b).Height = 12;
    //单元格自适应宽高
    sheet.Cells.Style.ShrinkToFit = true;

  • 设置超链接
    sheet.Cells[1, 1].Hyperlink = new ExcelHyperLink("http:\\www.baidu.com", UriKind.Relative);
  • 全局表格自适应高宽
    sheet.Cells.AutoFitColumns();
    sheet.Cells[1, 1].AutoFitColumns();
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值