• C#生成导出Excel文档(EPPlus)


    1、公式计算

    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";//这是保留两位小数

     

    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";//形状的内容
    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;//隐藏某一行

  • 相关阅读:
    图解Eclipse中配置Maven并创建Maven的Web工程
    Bzoj1922: [Sdoi2010]大陆争霸
    Bzoj2007: [Noi2010]海拔
    Bzoj5212: [Zjoi2018]历史
    NOIP2017:列队
    NOIP2017:逛公园
    CF908D New Year and Arbitrary Arrangement
    HDU4652:Dice
    HDU4336:Card Collector(min-max容斥)
    [NOI2017]游戏
  • 原文地址:https://www.cnblogs.com/aghk/p/13300070.html
Copyright © 2020-2023  润新知