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

Radgrid doesnt export image in excel

30 Answers 773 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Albert
Top achievements
Rank 1
Albert asked on 10 Sep 2008, 06:23 AM
I have one template column which has an image. But the image doesnt get exported, and I ALREADY TRIED the ExportOnlyData= false and it still doesnt work

30 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 10 Sep 2008, 07:14 AM
Hello Albert,

Unfortunately images can not be exported in the resulting excel file. It is a limitation of the Excel that images cannot be placed in an excel cell. You can hide the image column prior to the grid export or configure the grid to export data only.

Find more about RadGrid exporting here

Regards,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Albert
Top achievements
Rank 1
answered on 10 Sep 2008, 07:54 AM
Bummer, so there is no other way of working around this? :(
0
Iana Tsolova
Telerik team
answered on 10 Sep 2008, 08:59 AM
Hello Albert,

You can export RadGrid to Pdf for instance if you like the images to present in the resulting file.

Best wishes,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Robert
Top achievements
Rank 1
answered on 25 Sep 2009, 01:41 PM
This statement is not true: "It is a limitation of the Excel that images cannot be placed in an excel cell. "

We do this all the time with ActiveReports exporting.

As of the date of this forum has anyone shed light to this or is this a Telerik limitation and not an Excel limitation.

0
Vlad
Telerik team
answered on 25 Sep 2009, 02:41 PM
Hello Robert,

Indeed you are right however this will work only for MS Excel binary format which is not the case with RadGrid where only HTML and ExcelML formats are supported.

Best wishes,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Mike
Top achievements
Rank 1
answered on 10 Mar 2010, 03:27 PM
Hi,

Is there any plan to include this in future iterations of the radgrid? It seems to be a fundamental limitation which many of our customers - and I guess therefore many of yours would be interested in having sorted.

If you have no plans - how to I go about raising a change request to at least explore how I might go about changing your minds on this?

Best regards

Nick Graham
0
Daniel
Telerik team
answered on 10 Mar 2010, 05:02 PM
Hello Nick,

I'm afraid none of the supported formats (except PDF) support embedded images. In the next few days, I will create a code-library project that will show how to export images with a popular (free) third-party library.

Kind regards,
Daniel
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Mike
Top achievements
Rank 1
answered on 22 Mar 2010, 04:45 PM
Hi,

You said you would post a solution using a popular free component.

You havent done this yet - I am having time pressures here and need to know what effort is involved in solving this problem.

Best regards

Nick
0
Daniel
Telerik team
answered on 24 Mar 2010, 06:56 PM
Hello Nick,

Please excuse me for the delay. I will do my best to provide a simple project for you tomorrow.

Kind regards,
Daniel
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Mike
Top achievements
Rank 1
answered on 25 Mar 2010, 10:31 PM
Hi - if you could provide this I would appreciate it - we are coming under some serious pressure. I have used the I am awaiting further information from telerik as my response several times now. Its not making either me or telerik look very good.

Best regards
Nick
0
Daniel
Telerik team
answered on 29 Mar 2010, 07:43 PM
Hello Nick,

I hope the sample project was helpful for you. We will create a code-library project as soon as we get over some limitations.

Regards,
Daniel
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Paul Coakley
Top achievements
Rank 2
answered on 15 Apr 2010, 05:26 AM
Is this sample project available?  Are you able to email to me if you're not posting?
0
Daniel
Telerik team
answered on 19 Apr 2010, 09:43 PM
Hello Paul,

The code-library project is not ready yet - I will try to finish it until the end of the week. In the meantime you can submit a support ticket (with reference to this thread) if you want to test the project I sent to Nick.

Best regards,
Daniel
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Daniel
Telerik team
answered on 03 May 2010, 02:16 PM
Hello,

The code-library project is ready - you can download it from our code-library section:
Export to Excel with third-party library (NPOI)

Regards,
Daniel
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Bill Sting
Top achievements
Rank 1
answered on 04 May 2010, 08:32 AM
Hi All,
I have a radgrid and I load image data by html tag <img> in GridTemplateColumn. I tried to use library NPOI, but can not display columns image in excel.
Here my code:

<telerik:GridTemplateColumn HeaderText="" UniqueName="Images">
<ItemTemplate>
          <img src="<%=Global.Root %>/<%= Global.GetConfigKey("PresentationPath") %>/<%#DataBinder.Eval(Container.DataItem, "IconPath")%>" alt="" title="<%#DataBinder.Eval(Container.DataItem, "ResourceName")%>" style="width:128px; height:128px;" />
</ItemTemplate>
 </telerik:GridTemplateColumn>

Please help me,
Thanks
Nguyen Trung Tin
0
Daniel
Telerik team
answered on 07 May 2010, 01:08 PM
Hello Bill,

Client-side elements (such as img) won't be exported since they are not available on the server. I recommend you replace the img element with ASP.NET Image or ImageButton.

Best regards,
Daniel
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Bill Sting
Top achievements
Rank 1
answered on 08 May 2010, 05:02 AM
Hi Daniel,

Thanks for your reply. I tried to edit code in RadGridExporter.cs (function CreateExcelRow), convert img to Image object and I was successful. :). But the images in excel file have incorrect image size (the width is correct maybe, but the height not).

Here is my edit code:

private void CreateExcelRow(int rowNum, GridItem item, HSSFSheet sheet) 
    { 
        sheet.CreateRow(rowNum); 
 
        for (int cellNum = 0; cellNum < item.Cells.Count; cellNum++) 
        { 
            TableCell cell = item.Cells[cellNum]; 
 
            //The command item is handled separately since its cells should be merged 
            if (item is GridCommandItem) 
            { 
                sheet.GetRow(rowNum).CreateCell(cellNum).SetCellValue(StripText(cell.Text)); 
                sheet.AddMergedRegion(new NPOI.HSSF.Util.Region(0, 0, 0, item.OwnerTableView.RenderColumns.Length - 1)); 
            } 
 
            if (cell.HasControls()) 
            { 
                foreach (Control ctrl in cell.Controls) 
                { 
                    //Handle TextBox controls 
                    //if (ctrl is ITextControl) 
                    //    sheet.GetRow(rowNum).CreateCell(cellNum).SetCellValue(StripText((ctrl as ITextControl).Text)); 
                    if (ctrl is ITextControl) 
                    { 
                        if (!(ctrl as ITextControl).Text.Contains(".jpg") && !(ctrl as ITextControl).Text.Contains(".gif") && !(ctrl as ITextControl).Text.Contains(".png")) 
                        { 
                            if (!(ctrl as ITextControl).Text.Contains("tr>") && !(ctrl as ITextControl).Text.Contains("td>") && !(ctrl as ITextControl).Text.Contains("/>")) 
                                sheet.GetRow(rowNum).CreateCell(cellNum).SetCellValue(StripText((ctrl as ITextControl).Text)); 
                        } 
                        else 
                        { 
                            string textTemp = (ctrl as ITextControl).Text.Replace("\r\n", "@"); 
                            string[] listText = textTemp.Split('@'); 
                            foreach (string text in listText) 
                            { 
                                if (!text.Contains("tr>") && !text.Contains("td>") && !text.Contains("/>") && !text.Trim().Equals(string.Empty)) 
                                { 
                                    textTemp = Global.Root + "/" + Global.GetConfigKey("PresentationPath") + text.Replace("\"", "").Replace("\\", ""); 
 
                                    System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); 
                                    img.ImageUrl = textTemp
                                    img.Height = 128
                                    img.Width = 128
                                    if (!String.IsNullOrEmpty(img.ImageUrl)) 
                                    { 
                                        //expand the row height when the picture overflows 
                                        if ((img.Height.Value / 1.33333f) > 16) //assuming 75dpi 
                                            sheet.GetRow(rowNum).HeightInPoints = (float)img.Height.Value / 1.33333f; //assuming 75dpi 
                                        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, cellNum, rowNum, cellNum, rowNum); 
                                        anchor.AnchorType = 2
                                        string path = ctrl.Page.Server.MapPath(img.ImageUrl); 
                                        HSSFPicture picture = patriarch.CreatePicture(anchor, LoadImage(path, workbook)); 
                                        picture.Resize(); 
                                        break; 
                                     } 
                                } 
                            } 
                        } 
                    } 
                    //Handle Button controls (except ImageButton) 
                    if (ctrl is IButtonControl && !(ctrl is ImageButton)) 
                        sheet.GetRow(rowNum).CreateCell(cellNum).SetCellValue(StripText((ctrl as IButtonControl).Text)); 
 
                    //Handle Images (including ImageButton) 
                    if (ctrl is System.Web.UI.WebControls.Image) 
                    { 
                        if (!(item is GridCommandItem)) 
                            sheet.GetRow(rowNum).CreateCell(cellNum); 
 
                        System.Web.UI.WebControls.Image img = ctrl as System.Web.UI.WebControls.Image; 
 
                        if (!String.IsNullOrEmpty(img.ImageUrl)) 
                        { 
                            //expand the row height when the picture overflows 
                            if ((img.Height.Value / 1.33333f) > 16) //assuming 75dpi 
                                sheet.GetRow(rowNum).HeightInPoints = (float)img.Height.Value / 1.33333f; //assuming 75dpi 
 
                            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, cellNum, rowNum, cellNum, rowNum); 
                            anchor.AnchorType = 2
                            string path = ctrl.Page.Server.MapPath(img.ImageUrl); 
                            HSSFPicture picture = patriarch.CreatePicture(anchor, LoadImage(path, workbook)); 
                            picture.Resize(); 
                        } 
                    } 
                } 
            } 
            else 
            { 
                sheet.GetRow(rowNum).CreateCell(cellNum).SetCellValue(StripText(cell.Text)); 
            } 
 
            ////Set the global styles for each cell - ItemStyle, AlternatingItemStyle, etc 
            //if (item.ItemType == GridItemType.Item && globalStyles.ContainsKey("ItemStyle")) 
            //    sheet.GetRow(rowNum).GetCell(cellNum).CellStyle = workbook.GetCellStyleAt(globalStyles["ItemStyle"]); 
            //if (item.ItemType == GridItemType.AlternatingItem && globalStyles.ContainsKey("AlternatingItemStyle")) 
            //    sheet.GetRow(rowNum).GetCell(cellNum).CellStyle = workbook.GetCellStyleAt(globalStyles["AlternatingItemStyle"]); 
            //if (item.ItemType == GridItemType.Header && globalStyles.ContainsKey("HeaderStyle")) 
            //    sheet.GetRow(rowNum).GetCell(cellNum).CellStyle = workbook.GetCellStyleAt(globalStyles["HeaderStyle"]); 
            //if (item.ItemType == GridItemType.CommandItem && globalStyles.ContainsKey("CommandItemStyle")) 
            //    sheet.GetRow(rowNum).GetCell(cellNum).CellStyle = workbook.GetCellStyleAt(globalStyles["CommandItemStyle"]); 
            //if (item.ItemType == GridItemType.Footer && globalStyles.ContainsKey("FooterStyle")) 
            //    sheet.GetRow(rowNum).GetCell(cellNum).CellStyle = workbook.GetCellStyleAt(globalStyles["FooterStyle"]); 
        } 
    } 

OK. Who help me  this problem. Thanks all.

The programmer in Vietnam
Nguyen Trung Tin


0
Daniel
Telerik team
answered on 13 May 2010, 12:32 PM
Hello Nguyen,

It is unlikely to get wrong width/height since the Resize method resets the original dimensions of the image. Note that you can't change the original size by specifying different width/height.

Can you please attach the original picture to this post?

Best regards,
Daniel
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Bill Sting
Top achievements
Rank 1
answered on 14 May 2010, 09:37 AM
Hi Daniel,

Thanks for your reply.
This attach contain the original image not resize.

The programmer in Vietnam
Nguyen Trung Tin
0
Daniel
Telerik team
answered on 19 May 2010, 04:14 PM
Hello Nguyen,

Your image is 115 x 158 pixels - it should be stretched properly as long as you set the correct dimensions.

We modified RadGridExporter.cs to address an issue related to the image resizing - please download the updated version and let me know whether the problem still persists.

Regards,
Daniel
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Bill Sting
Top achievements
Rank 1
answered on 24 May 2010, 11:02 AM
Thank you very much, Daniel,

I will try,

Nguyen Trung Tin,
0
Bill Sting
Top achievements
Rank 1
answered on 26 May 2010, 05:02 AM
Hi Daniel,

Appear new mistake, it export only one page in radgrid (the page show), it can not export full data in radgrid :((
Please help me. Thank you

A programmer in Vietnam
Nguyen Trung Tin
0
Robert
Top achievements
Rank 1
answered on 26 May 2010, 01:51 PM
I think it's under Export Settings -> Ignore Paging.
0
swapnil jagtap
Top achievements
Rank 1
answered on 18 Jun 2010, 03:52 PM
Hi,
        This scenario fails in GridTemplateColumn with label.Is there any solution available for that.I want images and GridTemplateColumn
while exporting of grid.

Swapnil
0
swapnil jagtap
Top achievements
Rank 1
answered on 21 Sep 2010, 11:08 AM
Hi,
        This scenario fails in GridTemplateColumn with label.Is there any solution available for that.I want images and GridTemplateColumn
while exporting of grid.
        Please reply to my problem I am in trouble.
Swapnil
0
Daniel
Telerik team
answered on 24 Sep 2010, 11:05 AM
Hello Swapnil,

Please try the updated version of the code-library project and let me know whether it works for you.
Export to Excel with third-party library (NPOI)

Regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Csaba
Top achievements
Rank 1
answered on 09 Nov 2010, 01:31 PM
Hi everyone!


I needed a solution for exporting GridBinaryImageColumn's image, so i modified the RadGridExported class. I thought it could be useful for you guys and maybe telerik could insert it into the export code library:

//extend the createExcelRow method with the following lines (insert it after the '//Handle Images (including ImageButton)' code:
 
 
//Handle binary images
if (ctrl is Telerik.Web.UI.RadBinaryImage)
{                       
   if (!(item is GridCommandItem))
   {
      sheet.GetRow(rowNum).CreateCell(cellNum);
   }
 
   RadBinaryImage img = ctrl as RadBinaryImage;
 
   if (!String.IsNullOrEmpty(img.ImageUrl))
   {                           
      //240, 130 are hardcoded values (size of the image
      sheet.SetColumnWidth(cellNum, 8640); //8640 = 240 * 36 | 36: magic number
      sheet.GetRow(rowNum).HeightInPoints = 130;
                             
      HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, cellNum, rowNum, cellNum, rowNum);                           
      anchor.AnchorType = 2;
      HSSFPicture picture = patriarch.CreatePicture(anchor, LoadImage_Binary(img.DataValue, workbook));                           
      picture.Resize();
      picture.LineStyle = HSSFPicture.LINEWIDTH_ONE_PT; //single line border                           
   }
}
 
----------------------------------------
/// <summary>
/// Loads the binary imamge for the workbook | Returns the index of the picture added to the workbook
/// </summary>
/// <param name="buffer">Binary image</param>
/// <param name="wb"></param>
/// <returns>The index of the picture added</returns>
private static int LoadImage_Binary(byte[] buffer, HSSFWorkbook wb)
{  
        int pictureType = 0;
        pictureType = HSSFWorkbook.PICTURE_TYPE_JPEG;
        return wb.AddPicture(buffer, pictureType);
}


I had problems with resizing the image, that's why i have hardcoded values for the cell size.
Do you guys have any idea why it won't display the image in other then the original size?

Thanks for the answer!
All the best,
Sergio
0
Daniel
Telerik team
answered on 11 Nov 2010, 10:54 PM
Hello Sergio,

Thank you for your contribution. I will merge your code to the project files when updating the source code.

Best regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Snehal
Top achievements
Rank 2
answered on 30 Sep 2011, 11:37 AM
http://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx

C#

private void Excel_Export()

{

    Response.Clear();

    Response.Buffer = true;

    Response.AddHeader("content-disposition",

     "attachment;filename=GridViewExport.xls");

    Response.Charset = "";

    Response.ContentType = "application/vnd.ms-excel";

    StringWriter sw = new StringWriter();

    HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.AllowPaging = false;

    GridView1.DataBind();

    for (int i = 0; i < GridView1.Rows.Count; i++)

    {

        GridViewRow row = GridView1.Rows[i];

        //Apply text style to each Row

        row.Attributes.Add("class""textmode");

    }

    GridView1.RenderControl(hw);

 

    //style to format numbers to string

    string style = @"<style> .textmode { mso-number-format:\@; } </style>";

    Response.Write(style);

    Response.Output.Write(sw.ToString());

    Response.Flush();

    Response.End();

}

 

   

                  

VB.Net

 

Private Sub Excel_Export()

   Response.Clear()

   Response.Buffer = True

   Response.AddHeader("content-disposition", _

    "attachment;filename=GridViewExport.xls")

   Response.Charset = ""

   Response.ContentType = "application/vnd.ms-excel"

   Dim sw As New StringWriter()

   Dim hw As New HtmlTextWriter(sw)

   GridView1.AllowPaging = False

   GridView1.DataBind()

   For i As Integer = 0 To GridView1.Rows.Count - 1

     Dim row As GridViewRow = GridView1.Rows(i)

    'Apply text style to each Row

     row.Attributes.Add("class""textmode")

   Next

   GridView1.RenderControl(hw)

 

   'style to format numbers to string

   Dim style As String = "<style> .textmode " _

     "{ mso-number-format:\@; } </style>"

   Response.Write(style)

   Response.Output.Write(sw.ToString())

   Response.Flush()

   Response.End()

End Sub

 

Figure below displays the Excel Workbook which contains exported GridView with images.

0
Daniel
Telerik team
answered on 30 Sep 2011, 01:00 PM
Hello Snehal,

I'm afraid this is not a feasible solution because the files are not embedded in the Excel spreadsheet. If you send this file to someone he won't see the images if they are hosted on your computer.
Even if you make them publicly available through internet, they won't be displayed when the file is opened on computer that doesn't have an internet connection.

Kind regards,
Daniel
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
Tags
Grid
Asked by
Albert
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Albert
Top achievements
Rank 1
Robert
Top achievements
Rank 1
Vlad
Telerik team
Mike
Top achievements
Rank 1
Daniel
Telerik team
Paul Coakley
Top achievements
Rank 2
Bill Sting
Top achievements
Rank 1
swapnil jagtap
Top achievements
Rank 1
Csaba
Top achievements
Rank 1
Snehal
Top achievements
Rank 2
Share this question
or