1

I'm trying to create a table in excel with closedxml and i want it to be having names it's columns with spaces as in thhis picture Table with spaces in its column's names. in the default case the names should not have spaces, i tried rewrite these names after creating the table but the names become transparent even if i mmake sure that the core is black.

the script of creating the table is :

public static void CreateTable(IXLWorksheet ws, int beginRow, int beginColumn, List<SitesTableModel> list)
{ 
    ws.Cell(beginRow, beginColumn).InsertTable(list.AsEnumerable());
    var range5 = ws.Range(beginRow, beginColumn, beginRow+list.Count, beginColumn+5);

    range5.Style.Alignment.Vertical = XLAlignmentVerticalValues.Top;
    range5.Style.Font.FontColor = XLColor.TealBlue;
    range5.Style.Font.FontName = "Arial";
    range5.Style.Font.FontSize = 10;
    range5.Style.Alignment.WrapText = true;

    Cell cell = new Cell
    {    
        fontColor = XLColor.Black,
        textValue = "Nom du site",
        fontName = "Arial",
        bold = false,
        fontSize = 10,
        alignment = XLAlignmentHorizontalValues.Left,    
    };

    SetCellValue(ws, beginRow, beginColumn, cell);
    cell.textValue = "Adresse d'audit";
    SetCellValue(ws, beginRow, beginColumn+1, cell);
    cell.textValue = "Certifications demandées";
    SetCellValue(ws, beginRow, beginColumn+2, cell);
    cell.textValue = "Activités du site";
    SetCellValue(ws, beginRow, beginColumn+3, cell);
    cell.textValue = "Produits du site";
    SetCellValue(ws, beginRow, beginColumn+4, cell);
}
Daxtron2
  • 1,239
  • 1
  • 11
  • 19
Soufien Hajji
  • 477
  • 1
  • 8
  • 24

1 Answers1

2

I'm no expert in ClosedXML, but it looks like you are creating one cell and using it over and over. I should think that something like this would work:

public static void CreateTable(IXLWorksheet ws, int beginRow, int beginColumn, List<SitesTableModel> list)
{ 
    var wsTable = ws.Cell(beginRow, beginColumn).InsertTable(list.AsEnumerable());

    // Removed range stuff I don't understand

    wsTable.Cell(beginRow, beginColumn).SetValue("Nom du site");
    wsTable.Cell(beginRow, beginColumn+1).SetValue("Adresse d'audit");
    wsTable.Cell(beginRow, beginColumn+2).SetValue("Certifications demandées");
    wsTable.Cell(beginRow, beginColumn+3).SetValue("Activités du site");
    wsTable.Cell(beginRow, beginColumn+4).SetValue("Produits du site");
}

Edit: There seems to be a good table intro here: https://github.com/ClosedXML/ClosedXML-wiki/blob/master/Inserting-Tables.md

Palle Due
  • 5,929
  • 4
  • 17
  • 32
  • Thanks that solved my problem. PS : there was a little mistake, it should be like this : wsTable.Cell(1, 1).SetValue("Nom du site"); wsTable.Cell(1, 2).SetValue("Adresse d'audit"); wsTable.Cell(1, 3).SetValue("Certifications demandées"); wsTable.Cell(1, 4).SetValue("Activités du site"); wsTable.Cell(1, 5).SetValue("Produits du site"); – Soufien Hajji Apr 24 '18 at 15:22