Export Radgrid to excel

9 posts, 0 answers
  1. Vitaly
    Vitaly  avatar
    76 posts
    Member since:
    Aug 2011

    Posted 14 Sep 2012 Link to this post

    Good afternoon guys,

    Before I export my grid to excel I have following logic:

    Telerik.Web.UI.GridExcelBuilder.

     

    RowElement row = new Telerik.Web.UI.GridExcelBuilder.RowElement();

     

    Telerik.Web.UI.GridExcelBuilder.

     

    CellElement cell = new Telerik.Web.UI.GridExcelBuilder.CellElement();

     

    cell.Data.DataItem =

     

    "NYC";

     

    cell.StyleValue =

     

    "MyHeaderStyle";

     

    cell.MergeAcross = e.Row.Cells.Count - 1;

    row.Cells.Add(cell);

    e.Worksheet.Table.Rows.Insert(0, row);

    And then I style this header:

     

    Telerik.Web.UI.GridExcelBuilder.

     

    StyleElement myStyleH = new Telerik.Web.UI.GridExcelBuilder.StyleElement("MyHeaderStyle");

     

    myStyleH.FontStyle.Size = 25;

    myStyleH.FontStyle.Bold =

     

    true;

     

    System.Drawing.

     

    Color col = System.Drawing.ColorTranslator.FromHtml("#2C78FF");

     

    myStyleH.FontStyle.Color = col;

     

     

     

     

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

     

    HorizontalAlignmentType.Center;

     

    e.Styles.Add(myStyleH);


    My question is:I need colored "NYC" header like this:
    "N" - with the green color
    "Y" - with the orange color
    "C" - with the blue color.
    How can I do that?

    Thanks so much for your help.
     

  2. Eyup
    Admin
    Eyup avatar
    3660 posts

    Posted 19 Sep 2012 Link to this post

    Hello Vitaly,

    I am afraid the requested functionality is not achievable when exporting ExcelML since there is no way to distinguish the letters in the cell's text content.

    Sorry for any inconvenience caused.

    All the best,
    Eyup
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. Vitaly
    Vitaly  avatar
    76 posts
    Member since:
    Aug 2011

    Posted 19 Sep 2012 Link to this post

    Thanks so much for your respond.
    Now I have another question:
    I am creating inside my grid column image:

    RadGrid

     

     

    CFNGrid = (RadGrid)CfnReport.FindItemByValue("CfnDataRpt").FindControl("CfnReportGrid");

     

     

     

    GridHeaderItem headerItem = (GridHeaderItem)CFNGrid.MasterTableView.GetItems(GridItemType.Header)[0];

     

     

     

    Image img = new Image();

     

    img.ImageUrl =

     

    "~/Image/Header_Descrp.jpg";

     

    img.ImageAlign =

     

    ImageAlign.Bottom;

     

    img.ToolTip =

     

    "Employee FileNo";

     

    headerItem[

     

    "EIS_ID"].Controls.AddAt(1, img);

    When I do export to PDF this image stays inside the column.How can I remove it?

    Thanks again.

     

  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 Sep 2012 Link to this post

    Hi,

    Try the following code to hide the image when exporting.
    C#:
    Image img = new Image();
     img.ID = "Image1";
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExportToPdfCommandName)
        {
             GridHeaderItem headerItem = (GridHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
              Image img = (Image)headerItem.FindControl("Image1");
              img.Visible = false;
            }
    }

    Thanks,
    Shinu.
  5. Vitaly
    Vitaly  avatar
    76 posts
    Member since:
    Aug 2011

    Posted 20 Sep 2012 Link to this post

    Thanks so much for you responding.
    I was able to put this logic to ItemCommand event but it is not working.
    For some reason when I click Export to PDf button this event not firing.

    protected

     

     

    void CfnReportGrid_ItemCommand(object sender, GridCommandEventArgs e)

     

    {


    if

     

     

    (e.CommandName == "Approved")

     

    {

     

     

    GridEditableItem editItem = (GridEditableItem)e.Item;

     

     

     

    string User = ((EISUser)Session["User"]).TSOUserID;

     

     

     

    string EditFlag = string.Empty;

     

     

     

    string Pri = editItem.Cells[2].Text;

     

     

     

    string Loc = editItem.Cells[5].Text;

     

     

     

    string Bschool = Loc.Substring(2, 4);

     

     

     

    string Job = editItem.Cells[6].Text;

     

     

     

    string Svcdt = editItem.Cells[8].Text;

     

    ViewState[

     

    "User"] = null;

     

    ViewState[

     

    "Pri"] = null;

     

    ViewState[

     

    "BSchool"] = null;

     

    ViewState[

     

    "Job"] = null;

     

    ViewState[

     

    "SvcDate"] = null;

     

     

     

    PerDiemCFNReport.CFN_AG_Edit(CFNList1.Year, User, Pri, Bschool, Job, Svcdt, ref EditFlag);

     

     

     

    switch (EditFlag)

     

    {

     

     

    case "F":

     

     

     

    string scriptstring = "radalert('Job issue.Please try again later.', 300, 100,'Job Information');";

     

     

     

    ScriptManager.RegisterStartupScript(this, this.GetType(), "radalert", scriptstring, true);

     

    HeaderPanel.Visible =

     

    false;

     

    Panel1.Visible =

     

    false;

     

    CFNList1.CFN =

     

    " ";

     

    CFNList1.Location =

     

    " ";

     

    EMP_IDs1.SelectedID =

     

    " ";

     

     

     

    break;

     

     

     

    case "S":

     

     

     

    string scriptstring1 = "radalert('System error.Please try again later.', 300, 100,'System Information');";

     

     

     

    ScriptManager.RegisterStartupScript(this, this.GetType(), "radalert", scriptstring1, true);

     

    HeaderPanel.Visible =

     

    false;

     

    Panel1.Visible =

     

    false;

     

    CFNList1.CFN =

     

    " ";

     

    CFNList1.Location =

     

    " ";

     

    EMP_IDs1.SelectedID =

     

    " ";

     

     

     

    break;

     

     

     

    case "V":

     

     

     

    string scriptstring2 = "radalert('Security violation.You cannot approve this record.', 300, 100,'Security Information');";

     

     

     

    ScriptManager.RegisterStartupScript(this, this.GetType(), "radalert", scriptstring2, true);

     

    HeaderPanel.Visible =

     

    false;

     

    Panel1.Visible =

     

    false;

     

    CFNList1.CFN =

     

    " ";

     

    CFNList1.Location =

     

    " ";

     

    EMP_IDs1.SelectedID =

     

    " ";

     

     

     

    break;

     

     

     

    case "Y":

     

    ViewState[

     

    "User"] = User;

     

    ViewState[

     

    "Pri"] = Pri;

     

    ViewState[

     

    "BSchool"] = Bschool;

     

    ViewState[

     

    "Job"] = Job;

     

    ViewState[

     

    "SvcDate"] = Svcdt;

     

    RadWindowManager1.RadConfirm(

     

    "This action will approve the selected record. Are you sure?", "confirmCallbackFn", 300, 100, null, "Confirmation Message");

     

     

     

    break;

     

     

     

    default:

     

     

     

    string scriptstring3 = "radalert('System error.Please try again later.', 300, 100,'System Information');";

     

     

     

    ScriptManager.RegisterStartupScript(this, this.GetType(), "radalert", scriptstring3, true);

     

    HeaderPanel.Visible =

     

    false;

     

    Panel1.Visible =

     

    false;

     

    CFNList1.CFN =

     

    " ";

     

    CFNList1.Location =

     

    " ";

     

    EMP_IDs1.SelectedID =

     

    " ";

     

     

     

    break;

     

    }

     

    }

     

     

    else

     

    {

     

     

    if (e.CommandName == RadGrid.ExportToPdfCommandName)

     

    {

     

     

    RadGrid CFNGrid = (RadGrid)CfnReport.FindItemByValue("CfnDataRpt").FindControl("CfnReportGrid");

     

     

     

    GridHeaderItem headerItem = (GridHeaderItem)CFNGrid.MasterTableView.GetItems(GridItemType.Header)[0];

     

     

     

    Image img = (Image)headerItem.FindControl("PRIImage");

     

    img.Visible =

     

    false;

     

    }

    }


    Thanks.
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Sep 2012 Link to this post

    Hi,

    Unfortunately I cannot replicate the issue at my end. Here is the sample code that I tried.
    C#:
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
            if (e.CommandName == "Export")//exporting with a button in CommandItemTemplate
            {
               //your code
            }
     }
    protected void Button2_Click(object sender, EventArgs e)//exporting in external button click.
    {
      foreach(GridHeaderItem headerItem in RadGrid1.MasterTableView.GetItems(GridItemType.Header))
       {
         Image img = (Image)headerItem.FindControl("Image1");
          img.Visible = false;
       }
            RadGrid1.MasterTableView.ExportToExcel();
    }

    Thanks,
    Shinu.
  7. Vitaly
    Vitaly  avatar
    76 posts
    Member since:
    Aug 2011

    Posted 21 Sep 2012 Link to this post

    Good morning Shinu,
    thanks for your responding.Lets start from the beginning:
    this is aspx page where my image button for "Export to Pdf" located:

     

     

    <telerik:RadPanelBar ID="CfnReport" ExpandMode="MultipleExpandedItems" runat="server" Width="98%" BorderWidth="1px">

     

     

     

     

    <Items>

     

     

     

    <telerik:RadPanelItem Expanded="true" Text="CFN REPORT" runat="server" Selected="true" Font-Bold="false" Font-Size="13" Font-Names="Verdana" >

     

     

     

    <Items>

     

     

     

     

    <telerik:RadPanelItem Value="CfnDataRpt" runat="server">

     

     

     

     

    <ItemTemplate>

     

     

     

    <div id="exportDiv" style="position:relative; left :550px;">

     

     

     

    <asp:ImageButton ID="ExporttoExcel" BackColor="Transparent" runat="server"

     

     

     

    ImageUrl="~/Image/Export_To_Excel4.jpg" onclick="ExporttoExcel_Click1" ToolTip="Export to Excel" Height="20px" BorderWidth="2px" />&nbsp;&nbsp;&nbsp;

     

     

     

    <asp:ImageButton ID="ExporttoPdf" BackColor="Transparent" runat="server"

     

     

     

    ImageUrl="~/Image/Export_To_Pdf4.bmp"

     

     

     

    onclick="ExporttoPdf_Click1" ToolTip="Export to Pdf" Height="20px"

     

     

     

    BorderWidth="2px" />

     

    <%

     

    --<asp:Button ID="ExporttoExcel" Width="115px" CssClass="button" runat="server"

     

    Text="Export to Excel" onclick="ExporttoExcel_Click" />

    <asp:Button ID="ExporttoPdf" Width="115px" CssClass="button"

    runat="server" Text="Export to Pdf" style="margin-left :10px;"

    onclick="ExporttoPdf_Click" />--

     

     

    %>

     

     

     

    </div>

     

     

     

     

    <telerik:RadGrid ID="CfnReportGrid" runat="server" GridLines="None" Skin="Office2010Blue"

     

     

     

    AllowPaging="true" AllowSorting="true"

     

     

     

    onexcelmlexportrowcreated="CfnReportGrid_ExcelMLExportRowCreated"

     

     

     

    onexcelmlexportstylescreated="CfnReportGrid_ExcelMLExportStylesCreated"

     

     

     

    onitemcommand="CfnReportGrid_ItemCommand"

     

     

     

    onitemcreated="CfnReportGrid_ItemCreated"

     

     

     

    onneeddatasource="CfnReportGrid_NeedDataSource"

     

     

     

    onpageindexchanged="CfnReportGrid_PageIndexChanged"

     

     

     

    onpagesizechanged="CfnReportGrid_PageSizeChanged"

     

     

     

    onpdfexporting="CfnReportGrid_PdfExporting"

     

     

     

    onsortcommand="CfnReportGrid_SortCommand"

     

     

     

    onprerender="CfnReportGrid_PreRender" >

     


    Now this is my code-behind where I am creating a image inside the column :

    protected

     

     

    void CfnReportGrid_PreRender(object sender, EventArgs e)

     

    {

     

     

    RadGrid CFNGrid = (RadGrid)CfnReport.FindItemByValue("CfnDataRpt").FindControl("CfnReportGrid");

     

     

     

    GridHeaderItem headerItem = (GridHeaderItem)CFNGrid.MasterTableView.GetItems(GridItemType.Header)[0];

     

     

     

    Image img = new Image();

     

    img.ID =

     

    "PRIImage";

     

    img.ImageUrl =

     

    "~/Image/Header_Descrp.jpg";

     

    img.ImageAlign =

     

    ImageAlign.Bottom;

     

    img.ToolTip =

     

    "Employee FileNo";

     

    headerItem[

     

    "EIS_ID"].Controls.AddAt(1, img);

     


    Now I am exporting Grid to PDF:

    bool

     

     

    IsExport = false;

     

     

     

    protected void ExporttoPdf_Click1(object sender, ImageClickEventArgs e)

     

    {

     

     

    RadGrid CFNGrid = (RadGrid)CfnReport.FindItemByValue("CfnDataRpt").FindControl("CfnReportGrid");

     

    IsExport =

     

    true;

     

    CFNGrid.MasterTableView.BorderStyle =

     

    BorderStyle.None;

     

    CFNGrid.MasterTableView.GridLines =

     

    GridLines.Vertical;

     

    CFNGrid.MasterTableView.ExportToPdf();

     

     

    }


    I try to put this logic to this event but getting error:
    Object reference not set to an instance of an object.

    Please help me to resolve this issue.Thanks so much.
  8. Eyup
    Admin
    Eyup avatar
    3660 posts

    Posted 25 Sep 2012 Link to this post

    Hi Vitaly,

    Just add the following condition in your code-behind:
    bool IsExport = false;
    protected void CfnReportGrid_PreRender(object sender, EventArgs e)
    {
        ...
        headerItem["EIS_ID"].Controls.AddAt(1, img);
        if (IsExport==true)
        {
             img.Visible = false;
        }
    }
    protected void ExporttoPdf_Click(object sender, ImageClickEventArgs e)
    {
        ...
        IsExport = true;
        CFNGrid.MasterTableView.ExportToPdf();
    }

    That should do the trick. Please give it a try and let me know about the result.

    Greetings,
    Eyup
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  9. Vitaly
    Vitaly  avatar
    76 posts
    Member since:
    Aug 2011

    Posted 25 Sep 2012 Link to this post

    Good morning,
    thanks so much for your responding but Jayesh Goyani helped me out yesterday to resolve this issue in very similar style which you just proposed to me.

    Again thanks so much for your help.
Back to Top