Saving C1FlexGrid to excel and print with cell borders

Posted by: wknauf on 6 November 2019, 7:38 pm EST

  • Posted 6 November 2019, 7:38 pm EST - Updated 3 October 2022, 1:22 pm EST

    Hi C1,

    see attached sample: a C1FlexGrid is saved to excel. When printing, no borders/grid lines are printed.

    C1ExcelExport.zip

    A customer asked whether it would be possible to export the C1FlexGrid cell borders (of the “Normal” style) to excel, too. Thus, the borders would be visible in the printed document.

    The customer could activate printing the gridlines in the print settings (see screenshot, taken from German office):



    But the C1FlexGrid borders might be altered (removed for some cells) in the original grid, so it would be best if the grid borders were exported.

    Do you see any chance to add this to the grid - e.g. by adding a “FileFlags” enum value?

    I will try to add the borders by doing some postprocessing using C1Excel.

    Best regards

    Wolfgang

  • Posted 6 November 2019, 8:55 pm EST

    Hi Wolfgang,

    I’ve asked the developers to consider this as an enhancement [Internal Tracking ID: 404771].

    I’ll let you know once we receive more information on this.

    Regards,

    Jitender

  • Posted 6 November 2019, 10:33 pm EST

    Thanks!

    While coding my own workaround, I noticed some special cases that should be kept in mind:

    a) current cell style has DefinedElements.Border - then set the style color of this cell to the right/bottom border. The width might also be matched to an excel line style.

    b) current cell style has no defined border - then it has the default border. The RGB value 240/240/240 might be too bright to be usable in excel printout - better use a darker color?

    c) first row/col should also have a left/top border.

    Best regards

    Wolfgang

  • Posted 7 November 2019, 5:33 pm EST

    Wolfgang, thanks for posting your observations while working on the workaround. I’m sure these would be helpful to others while this is not directly supported.

    Regards,

    Jitender

  • Posted 29 December 2020, 6:57 pm EST

    As another user asked the same question, I post here my code to copy the borders:

    The code uses C1Excel to read the saved grid and apply the borders.

    
    /// <summary>
    /// Copies the FlexGrid Borders (Gridlines) to Excel.
    /// </summary>
    /// <param name="flexGrid">source Flexgrid</param>
    /// <param name="_strFileName">Grid was saved to this file. Will be opened by C1Excel and saved afterwards.</param>
    private static void SaveGridExcel_ApplyBorders(C1FlexGrid flexGrid, string _strFileName)
    {
      // default border color of  Normal-Style:
      Color COLOR_BORDER_REPLACE = Color.FromArgb(240, 240, 240);
    
      // default border color is replaced by something darker:
      COLOR_BORDER_DEFAULT = Color.FromArgb(212, 208, 200);
    
      C1XLBook excelBook = new C1XLBook();
      excelBook.Load(_strFileName);
      XLSheet sheetDispo = excelBook.Sheets[0];
    
      int intExcelRow = 0;
      for (int intRow = 0; intRow < flexGrid.Rows.Count; intRow++)
      {
        //Ignore invisible rows:
        if (flexGrid.Rows[intRow].Visible == true)
        {
          int intExcelCol = 0;
          for (int intCol = 0; intCol < flexGrid.Cols.Count; intCol++)
          {
            //Ignore invisible cols
            if (flexGrid.Cols[intCol].Visible == true)
            {
              //Get cell style in grid:
              CellStyle styleCell = flexGrid.GetCellStyleDisplay(intRow, intCol);
              if (styleCell != null)
              {
                bool bolCanExportBorder = false;
                //Dies the style define a border? If yes then check whether it has a width:
                if (styleCell.DefinedElements.HasFlag(StyleElementFlags.Border) == true && styleCell.Border.Style != BorderStyleEnum.None && styleCell.Border.Width != 0)
                {
                  bolCanExportBorder = true;
                }
                else if (styleCell.DefinedElements.HasFlag(StyleElementFlags.Border) == false)
                {
                  //"Normal" style has no border definition: 
                  bolCanExportBorder = true;
                }
                if (bolCanExportBorder == true)
                {
                  XLCell cellAktuell = sheetDispo.GetCell(intExcelRow, intExcelCol);
                  if (cellAktuell == null)
                  {
                    throw new EXSystemException("Could not find cell " + intExcelRow + "/" + intExcelCol + " (grid cell: " + intRow + "/" + intCol + ") in Excel file!");
                  }
    
                  //Create new style:
                  XLStyle styleAktuell = cellAktuell.Style.Clone();
                  //Default Grid border color is very light replace by something darker
                  Color colorBorder = styleCell.Border.Color;
                  if (styleCell.Border.Color.R == COLOR_BORDER_REPLACE.R && styleCell.Border.Color.G == COLOR_BORDER_REPLACE.G && styleCell.Border.Color.B == COLOR_BORDER_REPLACE.B)
                  {
                    colorBorder = COLOR_BORDER_DEFAULT;
                  }
                  //Set BorderStyle for all:
                  //Actually, the width should be set, too...
                  if (styleCell.Border.Direction == BorderDirEnum.Both || styleCell.Border.Direction == BorderDirEnum.Horizontal)
                  {
                    //Set only Bottom border - thats the way the grid does it.
                    styleAktuell.BorderColorBottom = colorBorder;
                    styleAktuell.BorderBottom = XLLineStyleEnum.Thin;
                    //In row 0 set also top border
                    if (intExcelRow == 0)
                    {
                      styleAktuell.BorderColorTop = colorBorder;
                      styleAktuell.BorderTop = XLLineStyleEnum.Thin;
                    }
                  }
                  if (styleCell.Border.Direction == BorderDirEnum.Both || styleCell.Border.Direction == BorderDirEnum.Vertical)
                  {
                    //Set only Right border - thats the way the grid does it.
                    styleAktuell.BorderColorRight = colorBorder;
                    styleAktuell.BorderRight = XLLineStyleEnum.Thin;
    
                    //In col 0 set also left border
                    if (intExcelCol == 0)
                    {
                      styleAktuell.BorderColorLeft = colorBorder;
                      styleAktuell.BorderLeft = XLLineStyleEnum.Thin;
                    }
                  }
    
                  cellAktuell.Style = styleAktuell;
                }
              } 
    
              //Increment excel col index:
              ++intExcelCol;
            }
          }
    
          //Increment excel row index:
          ++intExcelRow;
        }
      }
      excelBook.Save(_strFileName);
      excelBook.Dispose();
    }
    
    
  • Posted 29 December 2020, 7:00 pm EST

  • Posted 30 December 2020, 9:31 pm EST

    Hi Wolfgang,

    Thank You for sharing the workaround code. While the team investigates on this feature, it will definitely help others.

    Regards

  • Posted 31 March 2022, 9:06 pm EST

    Hi,

    Did you find proper solution? The workaround is not acceptable for me.

  • Posted 3 April 2022, 7:41 pm EST

    Hi,

    We have asked the development team for an update on this. We will let you know as soon as we have an update.

    Kind Regards,

    Kartik

  • Posted 4 April 2022, 9:47 pm EST

    Hi,

    As per the development team, this feature is on the to-do list of the C1FlexGrid, but we do not have an ETA yet. Please let us know if you need any other information.

    Kind Regards,

    Kartik

  • Posted 5 April 2022, 11:07 pm EST - Updated 3 October 2022, 1:22 pm EST

    Yes, actually I have question about designer, I’m not able to edit flex grid in design time, I don’t see option to edit flexgrid in desinger:

  • Posted 6 April 2022, 5:13 pm EST

    Hi,

    Could you please let us know the version of .NET you are using? From the screenshot you shared, it seems like you are using .NET5/6 version of C1 controls in your project. The .NET5/6 designer is not as rich as the .NET framework designer, and currently, the Custom styles and column editors for the C1FlexGrid are not available in .NET5/6 designer. This is due to some breaking changes done by Microsoft in the .NET5/6 designer. The development team is working on it, but currently, the designer for .NET5/6 contains very limited customizations.

    Kind Regards,

    Kartik

  • Posted 6 April 2022, 5:15 pm EST

    Hi,

    Yes I use .NET6, do you have ETA for this issue?

    Thanks

  • Posted 7 April 2022, 2:23 pm EST

    Hi,

    We have asked the development team for an ETA on the issue. We will let you know as soon as we have an update.

    Kind Regards,

    Kartik

    [Internal Tracking ID: C1WIN-25761]

  • Posted 7 April 2022, 6:03 pm EST - Updated 3 October 2022, 1:22 pm EST

    Though you hijacked my thread, I am also interested in this question, as I have a small .NET6 app currently and we plan to update our large app to .NET 6 next year.

    You can edit columns and styles through the property grid (see screenshot). Only the “Tasks” menu does not support invoking those designers.

    Best regards

    Wolfgang

  • Posted 9 April 2022, 9:59 pm EST

    I must have been blind - those dialogues for Columns and Styles are just the Visual Studio default forms for classes, but not the custom C1 form. So yes, there is a limitation in C1.

  • Posted 5 July 2022, 10:41 pm EST

    What is the roadmap for support of the Styles and Column designer for .NET6? We plan to switch to .NET6 this summer, and without a C1FlexGrid column designer, we would have to delay the migration.

    Best regards

    Wolfgang

  • Posted 6 July 2022, 10:55 pm EST

    Hi Wolfgang,

    We checked your request with the development team, and as per the latest update, the design-time support for WinForms .NET 6 controls is still a work in progress both from our and Microsoft’s sides. The developers are in discussion with Microsoft about this, to make further improvements to the designers, and they are also constantly working on improving the designers for .NET 6 with every release.

    Unfortunately, we do not have an ETA for now, but we will surely update you as soon as we get any information on this. In the meantime, you can use the Property Grid to edit the properties of the .NET6 C1FlexGrid.

    If there is any specific issue that you are facing with the .NET6 WinForms designer, please let us know and we will help you accordingly.

    We really appreciate your patience and cooperation.

    Thanks and Kind Regards,

    Kartik

  • Posted 10 July 2022, 8:01 pm EST

    I created a new thread for the .NET6 designer: https://www.grapecity.com/forums/winforms-edition/c1flexgrid-in-net-6

    The initial issue of this thread (save C1FlexGrid to excel and render borders) is still open.

  • Posted 11 July 2022, 8:26 pm EST

    Hi Wolfgang,

    Thank you for creating a separate thread for a different issue. We have replied to your queries there and we can continue the thread there only.

    Best Regards,

    Kartik

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels