Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Loop through a large number of cells, setting styles on each cell individually.
  • Set group of cells to an area, then apply that style to the area.
    Code Block
    /* INCORRECT CODE:
    
    Style randDataStyle = wb.CreateStyle();
    randDataStyle.BackgroundColor = wb.Palette.GetClosestColor(162, 221, 139);
    randDataStyle.HorizontalAlignment = Style.HAlign.Center;
    ColumnProperties columnProperties;
    
    System.Random rand = new System.Random();
    for (int iRow = 1; iRow < 50; iRow++)
    {
        for (int iCol = 0; iCol < 3; iCol++)
        {
            ws.Cells[iRow, iCol].Value = rand.Next(100);
    
            ws.Cells[iRow, iCol].ApplyStyle(randDataStyle);
        }
    } */

Why this method causes issues:

...

  • Apply styles to large numbers of cells by setting the style to either rows/columns or to singular data marker cells.
  • If more than one style is needed in a row/column, and it can't be handled by switching the style for the other (ie, setting style on a row to change part of a column), try to keep the style that takes a majority of the row/column as the one set by that row/column.
    Code Block
    // POSSIBLE CORRECT CODE:
    
    Style randDataStyle = wb.CreateStyle();
    randDataStyle.BackgroundColor = wb.Palette.GetClosestColor(162, 221, 139);
    randDataStyle.HorizontalAlignment = Style.HAlign.Center;
    ColumnProperties columnProperties;
    
    // Loop through the 2-4 columns to apply formatting
    for (int i = 1; i < 4; i++)
    {
        columnProperties = ws.GetColumnProperties(i);
        columnProperties.Width = 100;
        columnProperties.ApplyStyle(randDataStyle);
    }

When Possible, Use DataReaders Instead of DataTables

...