Skip to main content Skip to footer

Import Images in C1FlexGrid from Excel Sheet/Workbook

ClFlexgrid can show images in a column, Bound or UnBound. We can also export the same grid to an excel file, with the images intact. But when the images are exported to an excel file they are not exported as an object within the cells, and that is not how Excel is designed to work. They are exported as objects on the sheet, which can be relocated, resized without any relation to their parent cell in the original grid. So this creates a hurdle when we try to Load an excel sheet, which has some images, in the C1Flexgrid control. What we expect to see is the grid being rendered with columns as shown in the excel file. But that is not the case. When ever we load an excel sheet with some images in a column, that column is rendered as empty in the grid, for the simple reason, that the grid cannot add/manipulate objects of the sheet on its own. Hence, we need to do it, for the controls. Fortunately, the C1 control, namely, C1Excel, lets you do that in quite a convenient way. So here is what we suggest to follow, if you want to load an excel sheet with images into a grid and also show the images in columns, 1. Create an object of C1Excel Control. Before loading the sheet in the grid, load it here, using the following code,

c1XLBook1.Load(Path and Name of File to be Loaded);  
XLSheet sheet1 = c1XLBook1.Sheets[0];

2. Second, we load all the "Shape Objects" stored within the sheet, to a "Shapes Collection, using the following code,

foreach (XLShape s1 in sheet1.Shapes)  
int j = 0;  
XLPictureShape xlpic1 = (XLPictureShape)s1;  
c1FlexGrid1.Cols[3][j] = xlpic1.Image;  

3. Now, its time to load these in a Grid column, for this we should have a column of Image type, within the grid, and in the sample that we are using, for ease we are adding all the objects one by one to subsequent rows, using the following code,

c1FlexGrid1.Cols[3][j] = xlpic1.Image;

Where Cols[3] can be customized to be any column of the user's choice. We have already used this code in the above "foreach" loop so that the shapes are read and loaded simultaneously in the grid. This approach does have some limitations though, one, we can cannot manipulate object locations until and unless we already know their existence order in the sheet, for all objects are stored in the "Shapes Collections" in the order of their appearance in the sheet. It is fairly easy if it is just a column of images, in the excel sheet, because then the are loaded in the same sequence. Secondly, the "Shapes Collection" stores only the unique objects, so if there is a repetition of same objects in the sheet, this logic would not work and to handle the repetition, we would have to manually map the object to another location on the grid. We have also attached a sample with this post in order to make the understanding a bit more easier. LoadExcelImages


comments powered by Disqus