当前位置:   article > 正文

epplus 一般使用教程_epplus教程

epplus教程

1、打开文件

  1. FileInfo fileInfo = new FileInfo(strFilePath);
  2. using (ExcelPackage package = new ExcelPackage(fileInfo))
  3. {
  4. ExcelWorksheet Mapping = package.Workbook.Worksheets.Add(SheetName);
  5. ExcelWorksheet Mst = package.Workbook.Worksheets.Add("Mst");
  6. package.Save(); //Save the workbook.
  7. }

2、赋值

  1. int skip = 0;
  2. //填充Mst
  3. for (int i = 1; i < ds.Tables.Count; i++)
  4. {
  5. Column = IndexToColumn(2 * i - 1);
  6. //填充列,连同数据源表头一起填充
  7. Mst.Cells[Column + "1"].LoadFromDataTable(ds.Tables[i], true);
  8. var ListValidation = Mapping.DataValidations.AddListValidation(Mapping.Cells[Startx + Endx + 1, i + skip, Startx + Endx + effectRow, i + skip].Address);//设置下拉框显示的数据区域
  9. ListValidation.Formula.ExcelFormula = "=Mst!$" + Column + "$2:$" + Column + "$" + (ds.Tables[i].Rows.Count + 1).ToString();//数据区域的名称
  10. if (i == 4)
  11. {
  12. skip = 1;
  13. Column = IndexToColumn(i);
  14. string strColumn = Column + (Startx + Endx + 1).ToString();
  15. Mapping.Cells[Startx + Endx + 1, i + skip, Startx + Endx + effectRow, i + skip].Formula = "=IFERROR(RIGHT(" + strColumn + ",LEN(" + strColumn + ") - FIND(\"@\"," + strColumn + ")), \"\")";
  16. }
  17. ListValidation.ShowErrorMessage = true;
  18. ListValidation.Error = Language.GetChiEng("Please choose options from the drop down only.", "请选择存在于下拉框的值");
  19. ListValidation.ErrorTitle = Language.GetChiEng("Entry was invalid.", "输入的数据无效");
  20. }
  21. var intValidation = Mapping.DataValidations.AddIntegerValidation(Mapping.Cells[Startx + Endx + 1, Endy + PostionCount + Other.Length, Startx + Endx + effectRow, Endy + PostionCount + Other.Length].Address);
  22. intValidation.Operator = ExcelDataValidationOperator.between;
  23. intValidation.Formula.Value = DateTime.Now.Year - 50;//有效值,取当前年的前后50年
  24. intValidation.Formula2.Value = DateTime.Now.Year + 50;
  25. intValidation.ShowErrorMessage = true;
  26. intValidation.Error = Language.GetChiEng("Please input year.", "请输入年份");
  27. intValidation.ErrorTitle = Language.GetChiEng("Entry was invalid.", "输入的数据无效");

3、样式

  1. //冻结表头
  2. Mapping.View.FreezePanes(4, 1);
  3. //合并行列
  4. Mapping.Cells[1, 1, 3, 5].Merge = true;
  5. Mapping.Cells[1, 6, 3, 11].Merge = true;
  6. Mapping.Cells[1, 1].Value = " Sample Type Mapping and Time Per Style";
  7. Mapping.Cells[1, 6].Value = "Position Time Per Style(H)";
  8. Mapping.Cells[1, 1, 30, 11].Style.Font.Bold = true;//字体为粗体
  9. //设置背景颜色
  10. Mapping.Cells[1, 1, 30, 11].Style.Fill.PatternType = ExcelFillStyle.Solid;
  11. Mapping.Cells[1, 1, 30, 11].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(221, 235, 247));
  12. //负数则红色字体显示
  13. ExcelAddress excelAddress = new ExcelAddress(1, 1, 30, 11);
  14. var cond = Mapping.ConditionalFormatting.AddLessThan(excelAddress);
  15. cond.Style.Font.Color.Color = Color.Red;
  16. cond.Formula = "0";
  17. //设置边框及颜色
  18. using (ExcelRange r = Mapping.Cells[1, 1, 30, 11])
  19. {
  20. r.Style.Border.Top.Style = ExcelBorderStyle.Thin;
  21. r.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
  22. r.Style.Border.Left.Style = ExcelBorderStyle.Thin;
  23. r.Style.Border.Right.Style = ExcelBorderStyle.Thin;
  24. r.Style.Border.Top.Color.SetColor(Color.Black);
  25. r.Style.Border.Bottom.Color.SetColor(Color.Black);
  26. r.Style.Border.Left.Color.SetColor(Color.Black);
  27. r.Style.Border.Right.Color.SetColor(Color.Black);
  28. }
  29. Mapping.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;//水平居中
  30. Mapping.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;//垂直居中
  31. Mapping.Cells.AutoFitColumns();//宽度自适应
  32. Mapping.Cells.Style.WrapText = true;//自动换行

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/118560
推荐阅读
相关标签
  

闽ICP备14008679号