/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/UsingColors.cs

# · C# · 90 lines · 50 code · 26 blank · 14 comment · 0 complexity · 4d4704c26d69e6a4e0cbf415c4cf000d MD5 · raw file

  1. using System;
  2. using System.Drawing;
  3. using ClosedXML.Excel;
  4. namespace ClosedXML_Examples.Styles
  5. {
  6. public class UsingColors : IXLExample
  7. {
  8. public void Create(String filePath)
  9. {
  10. var wb = new XLWorkbook();
  11. var ws = wb.Worksheets.Add("Using Colors");
  12. Int32 ro = 0;
  13. // From Known color
  14. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.Red;
  15. ws.Cell(ro, 2).Value = "XLColor.Red";
  16. // From Color not so known
  17. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.Byzantine;
  18. ws.Cell(ro, 2).Value = "XLColor.Byzantine";
  19. ro++;
  20. // FromArgb(Int32 argb) using Hex notation
  21. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromArgb(0xFF00FF);
  22. ws.Cell(ro, 2).Value = "XLColor.FromArgb(0xFF00FF)";
  23. // FromArgb(Int32 argb) using an integer (you need to convert the hex value to an int)
  24. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromArgb(16711935);
  25. ws.Cell(ro, 2).Value = "XLColor.FromArgb(16711935)";
  26. // FromArgb(Int32 r, Int32 g, Int32 b)
  27. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromArgb(255, 0, 255);
  28. ws.Cell(ro, 2).Value = "XLColor.FromArgb(255, 0, 255)";
  29. // FromArgb(Int32 a, Int32 r, Int32 g, Int32 b)
  30. // Note: Excel ignores the alpha value
  31. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromArgb(0, 255, 0, 255);
  32. ws.Cell(ro, 2).Value = "XLColor.FromArgb(0, 255, 0, 255)";
  33. ro++;
  34. // FromColor(Color color)
  35. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromColor(Color.Red);
  36. ws.Cell(ro, 2).Value = "XLColor.FromColor(Color.Red)";
  37. ro++;
  38. // FromHtml(String htmlColor)
  39. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromHtml("#FF996515");
  40. ws.Cell(ro, 2).Value = "XLColor.FromHtml(\"#FF996515\")";
  41. ro++;
  42. // FromIndex(Int32 indexedColor)
  43. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromIndex(25);
  44. ws.Cell(ro, 2).Value = "XLColor.FromIndex(25)";
  45. ro++;
  46. // FromKnownColor(KnownColor knownColor)
  47. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromKnownColor(KnownColor.Plum);
  48. ws.Cell(ro, 2).Value = "XLColor.FromKnownColor(KnownColor.Plum)";
  49. ro++;
  50. // FromName(String colorName)
  51. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromName("PowderBlue");
  52. ws.Cell(ro, 2).Value = "XLColor.FromName(\"PowderBlue\")";
  53. ro++;
  54. // From Theme color
  55. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromTheme(XLThemeColor.Accent1);
  56. ws.Cell(ro, 2).Value = "XLColor.FromTheme(XLThemeColor.Accent1)";
  57. // From Theme color with tint
  58. ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromTheme(XLThemeColor.Accent1, 0.5);
  59. ws.Cell(ro, 2).Value = "XLColor.FromTheme(XLThemeColor.Accent1, 0.5)";
  60. ws.Columns().AdjustToContents();
  61. wb.SaveAs(filePath);
  62. }
  63. }
  64. }