excel_plus v2.14.3
pub.dev GitHub

How to style Excel cells in Dart

Fonts, colours, fills, borders and alignment, applied through a single CellStyle.

Install#

dart pub add excel_plus
import 'package:excel_plus/excel_plus.dart';

Font, colour, fill and alignment#

sheet.updateCell(
  CellIndex.indexByString('A1'),
  TextCellValue('Header'),
  cellStyle: CellStyle(
    bold: true,
    italic: true,
    fontSize: 14,
    fontColorHex: ExcelColor.white,
    backgroundColorHex: ExcelColor.fromHexString('#21A366'),
    horizontalAlign: HorizontalAlign.Center,
    verticalAlign: VerticalAlign.Center,
  ),
);

You can also assign a style to a cell that already holds a value:

sheet.cell(CellIndex.indexByString('A1')).cellStyle = CellStyle(bold: true);

Colours#

Use a named colour or any hex string. Theme and indexed colours are supported too, which matters when a workbook should follow the theme it was created with.

ExcelColor.red;
ExcelColor.fromHexString('#21A366');
ExcelColor.theme(ThemeColor.accent1);
ExcelColor.indexed(12);

Borders#

sheet.cell(CellIndex.indexByString('A1')).cellStyle = CellStyle(
  leftBorder: Border(borderStyle: BorderStyle.Thin),
  rightBorder: Border(borderStyle: BorderStyle.Thin),
  topBorder: Border(borderStyle: BorderStyle.Medium),
  bottomBorder: Border(borderStyle: BorderStyle.Medium, borderColorHex: ExcelColor.red),
);

Gradient fills#

// Linear, sweeping top to bottom. 0 degrees runs left to right.
sheet.cell(CellIndex.indexByString('A1')).cellStyle = CellStyle(
  gradientFill: GradientFill.linear(
    degree: 90,
    stops: [
      GradientStop(0, ExcelColor.fromHexString('#2962FF')),
      GradientStop(1, ExcelColor.white),
    ],
  ),
);

// Path, radiating from the centre outwards.
sheet.cell(CellIndex.indexByString('A2')).cellStyle = CellStyle(
  gradientFill: GradientFill.path(
    left: 0.5, right: 0.5, top: 0.5, bottom: 0.5,
    stops: [GradientStop(0, ExcelColor.white), GradientStop(1, ExcelColor.red)],
  ),
);

Wrapping and rotation#

CellStyle(
  textWrapping: TextWrapping.WrapText,
  rotation: 45,
);

Reusing one style#

Build the style once and apply it across a header row rather than constructing a new one per cell.

final header = CellStyle(
  bold: true,
  fontColorHex: ExcelColor.white,
  backgroundColorHex: ExcelColor.fromHexString('#21A366'),
  horizontalAlign: HorizontalAlign.Center,
);

for (var col = 0; col < 5; col++) {
  sheet.cell(CellIndex.indexByColumnRow(columnIndex: col, rowIndex: 0)).cellStyle = header;
}