Formatting exported grid to look just like displayed grid.

When exporting a grid, to Word, Excel or PDF, I would want the exported grid to display in Excel .... as it does with the source formatting. Color and font properties. All.

Can I achieve this?

Assuming that the answer is 'Yes', can you provide some code to copy the formatting of the source grid to export grid. Like looping through each displayed cell and transferring the format to the exported cell? (Font size, backcolor and forcolor.)

AND. Can you give some sample code for some text which will appear on each printed page. Under the header and to left aligned. Above the grid. Like:

Syncfusion

ejGrid promotion.

Thank you in advance.



5 Replies

MS Mani Sankar Durai Syncfusion Team October 30, 2017 11:48 AM UTC

Hi Felix, 

Thanks for contacting Syncfusion support. 

Query 1: can you provide some code to copy the formatting of the source grid to export grid. Like looping through each displayed cell and transferring the format to the exported cell? (Font size, backcolor and forcolor.) 
     We have achieved your requirement using ServerExcelQueryCellInfo event in Grid. In this event, we can get the each cell range, cell value and columns. Based on that we can set the background color for specific cell, change the font size and fonts while exporting the Grid. Please refer to the following code example and Help documentation 
@(Html.EJ().Grid<OrdersView>("FlatGrid") 
        .Datasource((IEnumerable<object>)ViewBag.datasource) 
                .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items => 
                { 
                    items.AddTool(ToolBarItems.ExcelExport); 
                    items.AddTool(ToolBarItems.PrintGrid); 
                })) 
        .AllowPaging() 
        .Columns(col => 
        { 
... 
        }) 
        ) 
 
 
    public void ExportToExcel(string GridModel) 
        { 
            ExcelExport exp = new ExcelExport();            
            var DataSource = new NorthwindDataContext().OrdersViews.Take(100).ToList(); 
            GridProperties obj = ConvertGridObject(GridModel); 
            obj.ServerExcelQueryCellInfo = QueryCellInfo; 
            exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false,"none"); 
        } 
        public void QueryCellInfo(object currentCell) 
        { 
            IRange range = (IRange)currentCell; 
            if (range.Column == 3) 
            { 
                
                range.CellStyle.Color = Color.Red; //pass string value to assign the color 
            } 
            if (range.Column == 5) 
            { 
                range.CellStyle.Font.Bold = true; 
                range.CellStyle.Font.Italic = true; 
            } 
        } 
 

Query 2: Can you give some sample code for some text which will appear on each printed page. Under the header and to left aligned. Above the grid.  
    To add the header while printing the grid we sugeest you to use Beforeprint event in Grid control.  
Refer the code example 
 
<label id="lblHeader" class="header" style="display:none;"><b>Syncfusion</b></label> 
<img src="../../Images/download.png" class="image" style="display:none;" /> 
 
<br /> 
<div id = "ControlRegion"> 
    @(Html.EJ().Grid<OrdersView>("FlatGrid") 
        .Datasource((IEnumerable<object>)ViewBag.datasource) 
                .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items => 
                { 
                    items.AddTool(ToolBarItems.ExcelExport); 
                    items.AddTool(ToolBarItems.PrintGrid); 
                })) 
        .AllowPaging() 
        .Columns(col => 
        { 
... 
 
        }) 
                        .ClientSideEvents(eve => eve.BeforePrint("beforePrint")) 
        ) 
 
 
         </div > 
 
 
        <script type="text/javascript" >  
function beforePrint(args) { 
            $("#lblHeader").css('display', 'block'); 
            $(".image").css('display', 'block'); 
            args .element.prepend($(".header")); 
            args .element.prepend($(".image")); 
        } 
 
     </script> 
         
 
 
Refer the help documentation.  


We have also prepared a sample based on the above two requirements and that can be downloaded from the below link 

Please let us know if you need further assistance. 

Regards, 
Manisankar Durai. 



FE Felix October 30, 2017 03:18 PM UTC

Hi
Manisankar. Thanks for the prompt reply. I had seen that code in your/Syncfusion's various replies to other queries.

It's sort of OK for a specific case. My question was for something more generic. Like one parameter is the ejGrid and the second parameter is the ExcelGrid object. And I see now that I would probably need the data list as well.

Yeah! Well. That was my idea. But its sort of based on C#/VB6 'thinking'.

Perhaps something in Javascript?

The problem is not new. It existed well before MVC. In VB6 and grid printing for instance.

Making it a parameter, 'ExportWithFormatting=true/false', on the GridModel, would be very much a value added feature.



MS Mani Sankar Durai Syncfusion Team October 31, 2017 11:13 AM UTC

Hi Felix, 

We have checked your query and found that you need to have one additional property in gridModel by passing as parameter with those details that contains color changing, font size etc.., while exporting in grid. It is not feasible because when exporting the large data of records in grid it contains large cell details with font size, color that need to be change etc.., and cause a performance issue while exporting.  

So it is not feasible to achieve your requirement while exporting in grid and we suggest you to use the solution provided in the previous update. 

Regards, 
Manisankar Durai. 



FE Felix November 2, 2017 08:18 AM UTC

OK Manisankar. I can see that we are at a dead end using inbuilt public void ExportToExcel(string GridModel). Which is a pity. I have only small reports from tables with no more than 90 rows and less than 20 columns. So performance is not something that I would suffer from.

But. I still want a generic way to generate formatted reports as they appear on-screen to the client, With/without multiple headers etc.

The code that you supplied below is not feasible. It just doesn't compile.


obj.ServerExcelQueryCellInfo = QueryCellInfo;   doesn't 'mix' with public void QueryCellInfo(object currentCell)


How do I 'supply' the function with 'object currentCell'. There has to be a loop somewhere. Or that the 'event' has its 'row' and 'column' parameter and is activated whenever a cell is filled with a value.

Anyway, too many hour spent puzzling it out with no online help to resolve the issue.

Now. What about Syncfusion ReportWriter. Reading about it looks good. But then so did ' public void ExportToExcel(string GridModel)'

If I build the required reports using c# code, and including full formatting, into an RDL formated document using Syncfusion ReportWriter, (a generic function), can they be easily converted to .docx, .xlsx and .pdf format without me writing complex code. (I write with many bugs). Do you have ready prepared functions to do this?

Before I start to invest a lot of work in it only to find that only the data is exported without the visually attractive formatting.



YD Yuvaraj Devarajan Syncfusion Team November 3, 2017 11:04 AM UTC

Hi Felix, 
 
 I can see that we are at a dead end using inbuilt public void ExportToExcel(string GridModel). Which is a pity. I have only small reports from tables with no more than 90 rows and less than 20 columns. So performance is not something that I would suffer from. 
But. I still want a generic way to generate formatted reports as they appear on-screen to the client, With/without multiple headers etc. 
The code that you supplied below is not feasible. It just doesn't compile. 

obj.ServerExcelQueryCellInfo = QueryCellInfo;   doesn't 'mix' with public void QueryCellInfo(object currentCell) 
 
 
How do I 'supply' the function with 'object currentCell'. There has to be a loop somewhere. Or that the 'event' has its 'row' and 'column' parameter and is activated whenever a cell is filled with a value.

 
Anyway, too many hour spent puzzling it out with no online help to resolve the issue. 
 
By default, the ServerExcelQueryCellInfo which is trigger from our export source while creating each cell and it is passed the current cell details as a parameter. Please refer to the below documentation link.  
 
Now. What about Syncfusion ReportWriter. Reading about it looks good. But then so did ' public void ExportToExcel(string GridModel)' 
If I build the required reports using c# code, and including full formatting, into an RDL formated document using Syncfusion ReportWriter, (a generic function), can they be easily converted to .docx, .xlsx and .pdf format without me writing complex code. (I write with many bugs). Do you have ready prepared functions to do this? 
Before I start to invest a lot of work in it only to find that only the data is exported without the visually attractive formatting. 
 
We can achieve your requirement by using ReportWriter control to export the report into PDF, Excel, Word, PPT and HTML directly. We have prepared a sample and it can be downloaded from below location, 
 
Please find the below UG documents for more detail, 
 
You can obtain the complete ASP.NET MVC ReportWriter sample in below build installed location, 
%userprofile%\AppData\Local\Syncfusion\EssentialStudio\version\ MVC\Samples\web 
 
 
Regards, 
Yuvaraj D. 


Loader.
Up arrow icon