/ClosedXML/ClosedXML/ClosedXML_Examples/Misc/AdjustToContents.cs

# · C# · 145 lines · 79 code · 42 blank · 24 comment · 4 complexity · 8c1a7b7e4b6d24d158e237d2dfb4374a MD5 · raw file

  1. using System;
  2. using ClosedXML.Excel;
  3. namespace ClosedXML_Examples.Misc
  4. {
  5. public class AdjustToContents : IXLExample
  6. {
  7. #region Variables
  8. // Public
  9. // Private
  10. #endregion
  11. #region Properties
  12. // Public
  13. // Private
  14. // Override
  15. #endregion
  16. #region Events
  17. // Public
  18. // Private
  19. // Override
  20. #endregion
  21. #region Methods
  22. // Public
  23. public void Create(String filePath)
  24. {
  25. var wb = new XLWorkbook();
  26. var ws = wb.Worksheets.Add("Adjust To Contents");
  27. // Set some values with different font sizes
  28. ws.Cell(1, 1).Value = "Tall Row";
  29. ws.Cell(1, 1).Style.Font.FontSize = 30;
  30. ws.Cell(2, 1).Value = "Very Wide Column";
  31. ws.Cell(2, 1).Style.Font.FontSize = 20;
  32. // Adjust column width
  33. ws.Column(1).AdjustToContents();
  34. // Adjust row heights
  35. ws.Rows(1, 2).AdjustToContents();
  36. // You can also adjust all rows/columns in one shot
  37. // ws.Rows().AdjustToContents();
  38. // ws.Columns().AdjustToContents();
  39. // We'll now select which cells should be used for calculating the
  40. // column widths (same method applies for row heights)
  41. // Set the values
  42. ws.Cell(4, 2).Value = "Width ignored because calling column.AdjustToContents(5, 7)";
  43. ws.Cell(5, 2).Value = "Short text";
  44. ws.Cell(6, 2).Value = "Width ignored because it's part of a merge";
  45. ws.Range(6, 2, 6, 4).Merge();
  46. ws.Cell(7, 2).Value = "Width should adjust to this cell";
  47. ws.Cell(8, 2).Value = "Width ignored because calling column.AdjustToContents(5, 7)";
  48. // Adjust column widths only taking into account rows 5-7
  49. // (merged cells will be ignored)
  50. ws.Column(2).AdjustToContents(5, 7);
  51. // You can also specify the starting row to start calculating the widths:
  52. // e.g. ws.Column(3).AdjustToContents(9);
  53. var ws2 = wb.Worksheets.Add("Adjust Widths");
  54. ws2.Cell(1, 1).SetValue("Text to adjust - 255").Style.Alignment.TextRotation = 255;
  55. for (Int32 co = 0; co < 90; co += 5)
  56. {
  57. ws2.Cell(1, (co / 5) + 2).SetValue("Text to adjust - " + co).Style.Alignment.TextRotation = co;
  58. }
  59. ws2.Columns().AdjustToContents();
  60. var ws4 = wb.Worksheets.Add("Adjust Widths 2");
  61. ws4.Cell(1, 1).SetValue("Text to adjust - 255").Style.Alignment.TextRotation = 255;
  62. for (Int32 co = 0; co < 90; co += 5)
  63. {
  64. var c = ws4.Cell(1, (co / 5) + 2);
  65. c.RichText.AddText("Text to adjust - " + co).SetBold();
  66. c.RichText.AddText(Environment.NewLine);
  67. c.RichText.AddText("World!").SetBold().SetFontColor(XLColor.Blue).SetFontSize(25);
  68. c.RichText.AddText(Environment.NewLine);
  69. c.RichText.AddText("Hello Cruel and unsusual world").SetBold().SetFontSize(20);
  70. c.RichText.AddText(Environment.NewLine);
  71. c.RichText.AddText("Hello").SetBold();
  72. c.Style.Alignment.SetTextRotation(co);
  73. }
  74. ws4.Columns().AdjustToContents();
  75. var ws3 = wb.Worksheets.Add("Adjust Heights");
  76. ws3.Cell(1, 1).SetValue("Text to adjust - 255").Style.Alignment.TextRotation = 255;
  77. for (Int32 ro = 0; ro < 90; ro += 5)
  78. {
  79. ws3.Cell((ro / 5) + 2, 1).SetValue("Text to adjust - " + ro).Style.Alignment.TextRotation = ro;
  80. }
  81. ws3.Rows().AdjustToContents();
  82. var ws5 = wb.Worksheets.Add("Adjust Heights 2");
  83. ws5.Cell(1, 1).SetValue("Text to adjust - 255").Style.Alignment.TextRotation = 255;
  84. for (Int32 ro = 0; ro < 90; ro += 5)
  85. {
  86. var c = ws5.Cell((ro / 5) + 2, 1);
  87. c.RichText.AddText("Text to adjust - " + ro).SetBold();
  88. c.RichText.AddText(Environment.NewLine);
  89. c.RichText.AddText("World!").SetBold().SetFontColor(XLColor.Blue).SetFontSize(10);
  90. c.RichText.AddText(Environment.NewLine);
  91. c.RichText.AddText("Hello Cruel and unsusual world").SetBold().SetFontSize(15);
  92. c.RichText.AddText(Environment.NewLine);
  93. c.RichText.AddText("Hello").SetBold();
  94. c.Style.Alignment.SetTextRotation(ro);
  95. }
  96. ws5.Rows().AdjustToContents();
  97. wb.SaveAs(filePath);
  98. }
  99. // Private
  100. // Override
  101. #endregion
  102. }
  103. }