Articles in this section

How to export the sparkline column to excel in the WinForms GridGroupingControl?

Excel export


By default, WinForms GridGroupingControl does not have Grid-like support for exporting the Sparkline cells to Excel. But Sparkline cells also can be exported by the following customization.

The Sparkline column or cells in the GridGroupingControl can be exported to Excel by defining the Sparkline Groups in the Excel sheet while exporting. ISparklineGroups interface is used for defining the Sparkline group in the Excel sheet.


ISparklineGroups interface caches the SparklineGroup that needs to be added to the Excel sheet. The Sparklines appears once you select the Data range and the Location range. Data range and Reference range for the Sparkline in Excel can be determined and assigned in QueryImportExportCellInfo event.

 

void converter_QueryImportExportCellInfo(object sender, GridImportExportCellInfoEventArgs e)
{
   if (e.Action == GridConverterAction.Export)
   {
      GridTableCellStyleInfoIdentity id = e.GridCell.CellIdentity as GridTableCellStyleInfoIdentity;
      if (id != null && id.Column != null && id.DisplayElement.IsRecord() &&
e.GridCell.CellType == GridCellTypeName.Control && e.GridCell.Control is SparkLine)
      {
          ISparklineGroup sparklineGroup = e.ExcelCell.Worksheet.SparklineGroups.Add();
          sparklineGroup.SparklineType = SparklineType.Line;
          ISparklines sparklines = sparklineGroup.Add();

          //Specify your needed Column index for starting range
          string startRange = GridRangeInfo.GetAlphaLabel(e.ExcelCell.Column - 3) + e.ExcelCell.Row.ToString();

          //Specify your needed Column index for end range
          string endRange = GridRangeInfo.GetAlphaLabel(e.ExcelCell.Column - 1) + e.ExcelCell.Row.ToString();
          IRange dataRange = e.ExcelCell.Worksheet.Range[startRange + ":" + endRange];
          IRange referenceRange = e.ExcelCell;

          //Adding Sparkline in excel sheet range
          sparklines.Add(dataRange, referenceRange);
          e.Handled = true;
      }
   }
}
Private Sub converter_QueryImportExportCellInfo(ByVal sender As Object, ByVal e As GridImportExportCellInfoEventArgs)
    If e.Action = GridConverterAction.Export Then
        Dim id As GridTableCellStyleInfoIdentity = TryCast(e.GridCell.CellIdentity, GridTableCellStyleInfoIdentity)
        If id IsNot Nothing AndAlso id.Column IsNot Nothing AndAlso id.DisplayElement.IsRecord() AndAlso
           e.GridCell.CellType = GridCellTypeName.Control AndAlso TypeOf e.GridCell.Control Is SparkLine Then

            Dim sparklineGroup As ISparklineGroup = e.ExcelCell.Worksheet.SparklineGroups.Add()
            sparklineGroup.SparklineType = SparklineType.Line
            Dim sparklines As ISparklines = sparklineGroup.Add()

            'Specify your needed Column index for starting range
            Dim startRange As String = GridRangeInfo.GetAlphaLabel(e.ExcelCell.Column - 3) & e.ExcelCell.Row.ToString()

            'Specify your needed Column index for end range
            Dim endRange As String = GridRangeInfo.GetAlphaLabel(e.ExcelCell.Column - 1) & e.ExcelCell.Row.ToString()
            Dim dataRange As IRange = e.ExcelCell.Worksheet.Range(startRange & ":" & endRange)
            Dim referenceRange As IRange = e.ExcelCell

            'Adding Sparkline in Excel sheet range
            sparklines.Add(dataRange, referenceRange)
           .Handled = True
        End If
    End If
End Sub

The screenshot below illustrates the sparkling column in Grid

Sparkline in GridGroupingControl


 
 Figure 1: Sparkline in GridGroupingControl

The screenshot below illustrates the exported sparkling column in Grid

Sparkline in Excelsheet


  Figure
2: Sparkline in ExcelSheet (Exported)


Samples:

C#: Export SparkLine CS

VB: Export SparkLine VB

 

Reference Link: Getting Started 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied