Style Class Library  

Style.Clone Method (String, String, Integer, Integer, Integer, Integer)

Creates copy of the style.

[Visual Basic]
Public Functions Clone( _
   ByVal styleName As String, _
   ByVal tableName As String, _
   ByVal startRow As Integer, _
   ByVal startCol As Integer, _
   ByVal endRow As Integer, _
   ByVal endCol As Integer) As Style
[C#]
public Style Clone( _
   string styleName,
   string tableName,
   int startRow,
   int startCol,
   int endRow,
   int endCol);

Parameters

styleName
Unique name that will be assigned to the style
tableName
Name of the DataTable with which style will be associated
startRow
Start (top most) index of the DataRow inside of the DataTable/DataView where range of the style begins.
startCol
Start (left most) index of the DataColumn inside of the DataTable/DataView where range of the style begins.
endRow
End (bottom most) index of the DataRow inside of the DataTable/DataView where range of the style ends.
endCol
End (right most) index of the DataColumn inside of the DataTable/DataView where range of the style ends.

Exceptions

Exception Type Condition
ArgumentNullException Occurred if styleName or tableName parameters are blank, are empty strings or null (Nothing in Visual Basic .NET).

Remarks

Clone method of the Style class allows create copy of the style and apply cloned style against another range of the cells inside of the DataTable/DataView. Creating copy of the style is useful in cases when you could define one, base style that shares common settings for multiple styles and then copy base style to create variations of other styles without creating them from the scratch.

Example

[Visual Basic, C#] The following example exports data from ADO.NET DataTable to native Microsoft® Excel binary format setting different formatting styles.

[C#] 
private void DataTableWithStyles(DataTable dataTableToExport, string dateColumnName) {

    VM.xPort.DS2XL xporter;
    VM.xPort.Style headerStyle;
    VM.xPort.Style contentStyle;
    VM.xPort.Style formatStyle;
    System.Drawing.Font customFont;

    xporter = new VM.xPort.DS2XL();

    //Define style for the header. 
    headerStyle = new VM.xPort.Style("HeaderStyle", dataTableToExport.TableName, -1, 0, -1,
        dataTableToExport.Columns.Count - 1);

    //Create font that will be used to format header text.
    customFont = new System.Drawing.Font("Arial", 8, FontStyle.Bold);
    headerStyle.Font = customFont;
    headerStyle.HorizontalAlignment = Style.xpHAlignment.Center;
    headerStyle.VerticalAlignment = Style.xpVAlignment.Center;
    headerStyle.TopBorderLine = Style.xpBorderLineStyle.Double;
    headerStyle.BottomBorderLine = Style.xpBorderLineStyle.Double;
    headerStyle.BackgroundColor = Color.FromArgb(0, 184, 204, 228);
    headerStyle.UnderlineStyle = Style.xpUnderlineStyle.Double;
    headerStyle.ReadingDirection = Style.xpReadingDirection.RightToLeft;

    //Add custom style to the collection of styles. If we do not add style, it will not be applied.
    xporter.Styles.Add(headerStyle);

    //Create style for content
    contentStyle = new VM.xPort.Style("ContentStyle", dataTableToExport.TableName, 0, 0,
        dataTableToExport.Rows.Count - 1, dataTableToExport.Columns.Count - 1);
    customFont = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
    contentStyle.Font = customFont;
    contentStyle.HorizontalAlignment = Style.xpHAlignment.Left;
    contentStyle.VerticalAlignment = Style.xpVAlignment.Center;
    contentStyle.ReadingDirection = Style.xpReadingDirection.RightToLeft;

    //Add custom style to the collection of styles
    xporter.Styles.Add(contentStyle);

    //Make a clone of the contentStyle style and set only specific properties of custom style. 
    formatStyle = contentStyle.Clone("DateValues", dataTableToExport.TableName, 0,
        dataTableToExport.Columns[dateColumnName].Ordinal, dataTableToExport.Rows.Count - 1,
        dataTableToExport.Columns[dateColumnName].Ordinal);

    //Set format of the date values in specific column. 
    //Refer to documentation about which built-in Excel formats 
    //are available. Use custom (not built-in) format string.
    formatStyle.FormatIndex = VM.xPort.Style.xpFormat.Custom;
    formatStyle.Format = @"MMM dd, yyyy";

    //Add custom style to the collection of styles.
    xporter.Styles.Add(formatStyle);

    xporter.Export(dataTableToExport, "DataTableWithStyles",
        xpOutputFormat.Excel8, true, true);
}

[Visual Basic] 
Private Sub DataTableWithStyles(ByVal dataTableToExport As DataTable, ByVal dateColumnName As String)

    Dim xporter As VM.xPort.DS2XL
    Dim headerStyle As VM.xPort.Style
    Dim contentStyle As VM.xPort.Style
    Dim customFont As System.Drawing.Font
    Dim formatStyle As VM.xPort.Style

    xporter = New VM.xPort.DS2XL()

    'Define style for the header. 
    headerStyle = New VM.xPort.Style("HeaderStyle", dataTableToExport.TableName, -1, 0, -1, _
        dataTableToExport.Columns.Count - 1)

    'Create font that will be used to format header text.
    customFont = New System.Drawing.Font("Arial", 8, FontStyle.Bold)
    headerStyle.Font = customFont
    headerStyle.HorizontalAlignment = Style.xpHAlignment.Center
    headerStyle.VerticalAlignment = Style.xpVAlignment.Center
    headerStyle.TopBorderLine = Style.xpBorderLineStyle.Double
    headerStyle.BottomBorderLine = Style.xpBorderLineStyle.Double
    headerStyle.BackgroundColor = Color.FromArgb(0, 184, 204, 228)
    headerStyle.UnderlineStyle = Style.xpUnderlineStyle.Double
    headerStyle.ReadingDirection = Style.xpReadingDirection.RightToLeft

    'Add custom style to the collection of styles. If we do not add style, it will not be applied.
    xporter.Styles.Add(headerStyle)

    'Create style for content
    contentStyle = New Style("ContentStyle", dataTableToExport.TableName, 0, 0, _
        dataTableToExport.Rows.Count - 1, dataTableToExport.Columns.Count - 1)
    customFont = New System.Drawing.Font("Arial", 8, FontStyle.Regular)
    contentStyle.Font = customFont
    contentStyle.HorizontalAlignment = Style.xpHAlignment.Left
    contentStyle.VerticalAlignment = Style.xpVAlignment.Center
    contentStyle.ReadingDirection = Style.xpReadingDirection.RightToLeft

    'Add custom style to the collection of styles
    xporter.Styles.Add(contentStyle)

    'Make a clone of the contentStyle style and set only specific properties of custom style. 
    formatStyle = contentStyle.Clone("DateValues", dataTableToExport.TableName, 0, _
        dataTableToExport.Columns(dateColumnName).Ordinal, dataTableToExport.Rows.Count - 1, _
        dataTableToExport.Columns(dateColumnName).Ordinal)

    'Set format of the date values in specific column. 
    'Refer to documentation about which built-in Excel formats 
    'are available. Use custom (not built-in) format string.
    formatStyle.FormatIndex = VM.xPort.Style.xpFormat.Custom
    formatStyle.Format = "MMM dd, yyyy"

    'Add custom style to the collection of styles.
    xporter.Styles.Add(formatStyle)

    xporter.Export(dataTableToExport, "DataTableWithStyles", _
        xpOutputFormat.Excel8, True, True)

End Sub

Requirements

Platforms: Windows Server 2008 family, Windows Server 2003 family, Windows 2000 family, Windows Vista family, Windows XP family

See Also

Style Class | Style Members | VM.xPort Namespace