Skip to main content Skip to footer

Exporting WijGrid data to Excel

This blog extends the Excel exporting functionality of WijGrid as described in the following blog. We would discuss two different scenarios here i.e. exporting all the WijGrid data to Excel along with the gridlines and exporting specific WijGrid columns to Excel. So, lets take it one by one. Exporting WijGrid data to Excel with Gridlines: You would need to save the wijgrid data into a hidden variable. Later get the hidden variable on the server side using the Response object and save it in an Excel file. The panes tag must be included when writing the data from the Response object to the Excel file so as to export the WijGrid data along with the gridlines. Here is the code for the same:


protected void btnExportServer_Click(object sender, EventArgs e)  
{  
Response.ContentType = "application/ms-excel";  
Response.AddHeader("content-disposition", "attachment; filename=test.xls");  
Response.Write("");  
Response.Write("");  
Response.Write("");  
Response.Write("< ! --[ if gte mso 9 ] >");  
Response.Write("<  xml>");  
Response.Write("< x:ExcelWorkbook >");  
Response.Write("< x:ExcelWorksheets >");  
Response.Write("< x:ExcelWorksheet >");  
//this line names the worksheet  
Response.Write("< x:Name >gridlineTest< /x:Name >");  
Response.Write("< x:WorksheetOptions >");  
//these 2 lines are what works the magic  
Response.Write("< x:Panes>");  
Response.Write("< /x:Panes>");  
Response.Write("< /x:WorksheetOptions>");  
Response.Write("< /x:ExcelWorksheet>");  
Response.Write("< /x:ExcelWorksheets>");  
Response.Write("< /x:ExcelWorkbook>");  
Response.Write("< /xml>");  
Response.Write("< ! [endif]-->");  
Response.Write((string)Request.Params["hiddenHtml"]);  
Response.End();  
}  

Exporting specific columns in WijGrid to Excel: The code above already describes a way to export the complete WijGrid data to Excel. A simple trick to export the specific columns is to hide the columns, which you do not wish to be exported to Excel. Later save the data of the visible columns to a hidden variable. Here is the code for the same:

function prepareHtml()    
{   
   var cols = $("#demo").wijgrid("option", "columns");   
   cols[0].visible = false;   
   cols[1].visible = false;   
   $("#demo").wijgrid("doRefresh");   
   var cols = $("#demo").wijgrid("option", "columns");   
   $("#hiddenHtml").val($("#demo").parent().html());   
}  

The same code as above can be later used to export this subset of data to Excel. Here is the sample with the complete implementation of the above code.

MESCIUS inc.

comments powered by Disqus