30 Answers, 1 is accepted
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.
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.
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.
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.
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
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.
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
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.
Best regards
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.
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.
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.
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
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.
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
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.
Thanks for your reply.
This attach contain the original image not resize.
The programmer in Vietnam
Nguyen Trung Tin
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.
I will try,
Nguyen Trung Tin,
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
This scenario fails in GridTemplateColumn with label.Is there any solution available for that.I want images and GridTemplateColumn
while exporting of grid.
Swapnil
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
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
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
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
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.
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