This is a migrated thread and some comments may be shown as answers.

[Solved] Export to Excel - AlignmentElement.VerticalAlignment

1 Answer 382 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tiffany
Top achievements
Rank 1
Tiffany asked on 17 Jun 2009, 03:50 PM

I am trying to export a RadGrid to Excel.  However, I am not able to set the VerticalAligment to Top.  One of the columns in the my radgrid has a long text (more than 100 chars with a few line breaks).  I'd tried to set the VerticalAlign to Top as shown in the code below, but the Excel Report always have VerticalAlign set to Bottom.  Please advice!  Thanks!

 

protected

 

void RadGrid_ExcelMLExportRowCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)

 

{

 

if (e.RowType == Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowType.DataRow)

 

{

 

if (e.Row.Cells[0] != null && ((string)e.Row.Cells[0].Data.DataItem).Contains("U"))

 

{

e.Row.Cells[0].StyleValue =

"MyCustomStyle";

 

}

}

}

 

 

protected void RadGrid_ExcelMLExportStylesCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLStyleCreatedArgs e)

 

{

 

foreach (Telerik.Web.UI.GridExcelBuilder.StyleElement style in e.Styles)

 

{

 

if (style.Id == "headerStyle")

 

{

style.FontStyle.Bold =

true;

 

style.FontStyle.Color = System.Drawing.

Color.Gainsboro;

 

style.InteriorStyle.Color = System.Drawing.

Color.Wheat;

 

style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.

InteriorPatternType.Solid;

 

style.AlignmentElement.HorizontalAlignment = Telerik.Web.UI.GridExcelBuilder.

HorizontalAlignmentType.Left;

 

style.AlignmentElement.VerticalAlignment = Telerik.Web.UI.GridExcelBuilder.

VerticalAlignmentType.Top;

 

}

 

else if (style.Id == "itemStyle")

 

{

style.InteriorStyle.Color = System.Drawing.

Color.WhiteSmoke;

 

style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.

InteriorPatternType.Solid;

 

style.AlignmentElement.HorizontalAlignment = Telerik.Web.UI.GridExcelBuilder.

HorizontalAlignmentType.Left;

 

style.AlignmentElement.VerticalAlignment = Telerik.Web.UI.GridExcelBuilder.

VerticalAlignmentType.Top;

 

}

 

else if (style.Id == "alternatingItemStyle")

 

{

style.InteriorStyle.Color = System.Drawing.

Color.LightGray;

 

style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.

InteriorPatternType.Solid;

 

style.AlignmentElement.HorizontalAlignment = Telerik.Web.UI.GridExcelBuilder.

HorizontalAlignmentType.Left;

 

style.AlignmentElement.VerticalAlignment = Telerik.Web.UI.GridExcelBuilder.

VerticalAlignmentType.Top;

 

 

}

}

 

//Telerik.Web.UI.GridExcelBuilder.AlignmentStyleElement alignStyle = new Telerik.Web.UI.GridExcelBuilder.AlignmentStyleElement();

 

 

 

 

 

//alignStyle.VerticalAlignment = Telerik.Web.UI.GridExcelBuilder.VerticalAlignmentType.Top;

 

 

 

 

 

//e.Styles.Add(alignStyle);  // I'd tried this, but it does not work.

 

 

 

 

Telerik.Web.UI.GridExcelBuilder.

StyleElement myStyle = new Telerik.Web.UI.GridExcelBuilder.StyleElement("MyCustomStyle");

 

myStyle.FontStyle.Bold =

true;

 

myStyle.FontStyle.Italic =

true;

 

myStyle.InteriorStyle.Color = System.Drawing.

Color.Gray;

 

myStyle.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.

InteriorPatternType.Solid;

 

e.Styles.Add(myStyle);

}

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 22 Jun 2009, 05:44 AM
Hello Tiffany,

I believe your custom style overrides the inline alignment. To check this, please test the following snippet:
protected void RadGrid1_ExcelMLExportRowCreated(object source, GridExportExcelMLRowCreatedArgs e) 
    
protected void RadGrid1_ExcelMLExportStylesCreated(object source, GridExportExcelMLStyleCreatedArgs e) 
    foreach (StyleElement style in e.Styles) 
    { 
        switch (style.Id) 
        {  
            case "itemStyle"
            case "alternatingItemStyle": style.AlignmentElement.VerticalAlignment = VerticalAlignmentType.Top; break
        } 
    } 

Kind regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Tiffany
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or