Skip to main content

Exporting Kendo UI GRID to Excel with Java

This is a simple method of creating an Excel export function on your Kendo UI grid with a Java EE server.

JS function to trigger Excel export

In your web-page with the grid - insert this JS function. You should create a button or similar that calls this when you want to perform the export from the UI.
 function exportGridToExcel() {  
     var grid = $("#grid").data("kendoGrid");    
     var currentPage = grid.dataSource.page();  
     var allPages = new Array();  
     for(var n=1;n<=grid.dataSource.totalPages();n++) {  
       grid.dataSource.page(n);  
       var view = grid.dataSource.view();  
       for(var x=0;x<view.length;x++) {  
         allPages.push(view[x]);  
       }  
     }  
     $("#excelExportGridData").val(JSON.stringify({"rows": allPages, "cols": grid.columns}));  
     $("#exportToExcelForm").submit();  
     grid.dataSource.page(currentPage);  
   }  

Hidden form and iframe

You also need a hidden form and iframe on your page. This is posted to the servlet below - and the hidden iframe acts as a target for the form.
 <form id="exportToExcelForm" action="KendoGridExcelServlet" method="POST" target="exportToExcelHiddenFrame">  
       <input type="hidden" id="excelExportGridData" name="griddata" />  
     </form>  
     <iframe name="exportToExcelHiddenFrame" style="display: none;">  </iframe>  

Java servlet

The Java servlet is using Apache POI and Java JSON libraries from json.org. You should name it KendoGridExcelServlet or what you've set for the action for the hidden form above. The code below is only the method for processing the request, you should insert this into your servlet code template.
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)  
       throws ServletException, IOException {  
     request.setCharacterEncoding("UTF-8");      
     OutputStream out = response.getOutputStream();  
     try {  
       JSONObject gridobj = new JSONObject((String)request.getParameter("griddata"));  
       JSONArray rowsArr = gridobj.getJSONArray("rows");      
       JSONArray columnsArr = gridobj.getJSONArray("cols");     
       ArrayList<String> shownFieldsList = new ArrayList<String>();  
       ByteArrayOutputStream baos = new ByteArrayOutputStream();  
       Workbook wb = new XSSFWorkbook();  
       CellStyle headerStyle = wb.createCellStyle();  
       Font font = wb.createFont();  
       font.setBoldweight(Font.BOLDWEIGHT_BOLD);  
       headerStyle.setFont(font);  
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
       CreationHelper createHelper = wb.getCreationHelper();  
       CellStyle dateCellStyle = wb.createCellStyle();  
       dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm"));  
       Sheet sh = wb.createSheet();  
       Row headerRow = sh.createRow(0);  
       headerRow.setHeight((short)(headerRow.getHeight()*2));  
       int colIndex = 0;  
       for(int n=0;n<columnsArr.length();n++) {  
         JSONObject o = columnsArr.getJSONObject(n);  
         if(!o.has("hidden") || o.getBoolean("hidden")==Boolean.FALSE) {  
           shownFieldsList.add(o.getString("field"));       
           Cell cell = headerRow.createCell(colIndex);  
           cell.setCellValue(o.getString("title"));  
           cell.setCellStyle(headerStyle);  
            if(o.has("width") && !"null".equals(o.getString("width"))) {  
             sh.setColumnWidth(colIndex,32*Integer.parseInt(o.getString("width")));  
           } else {  
             sh.setColumnWidth(colIndex,256*15);  
           }  
           colIndex++;  
         }  
       }  
       Pattern datePattern = Pattern.compile("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9]Z");  
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
       int rowIndex = 1;  
       for(int n=0;n<rowsArr.length();n++) {  
         JSONObject rowObj = rowsArr.getJSONObject(n);  
         Row row = sh.createRow(rowIndex++);  
         colIndex = 0;  
         for(String field : shownFieldsList) {  
           Cell cell = row.createCell(colIndex++);  
           if(rowObj.has(field)) {  
             Object obj = rowObj.get(field);  
             if(String.class.isInstance(obj)) {  
               if(datePattern.matcher((String)obj).find()) {  
                 cell.setCellValue(dateFormat.parse(((String)obj).replace("T", " ").substring(0,"yyyy-MM-dd HH:mm:ss".length())));  
                 cell.setCellStyle(dateCellStyle);  
               } else {  
                 cell.setCellValue((String)obj);  
               }  
             } else if(Number.class.isInstance(obj)) {  
               cell.setCellValue(((Number)obj).doubleValue());  
             } else if(Date.class.isInstance(obj)) {  
               cell.setCellValue((Date)obj);  
               cell.setCellStyle(dateCellStyle);  
             }  
           }  
         }  
       }  
       wb.write(baos);  
       String filename = "KendoGrid.xlsx";  
       response.setHeader("Content-Disposition", "attachment; filename=\""+filename+"\"");  
       response.setContentType(URLConnection.guessContentTypeFromName(filename));  
       response.setDateHeader("Last-Modified",new Date().getTime());  
       response.setContentLength((int) baos.size());  
       out.write(baos.toByteArray());    
     } catch(Exception e) {  
       throw new ServletException(e);  
     } finally {  
       out.close();  
     }  
   }  

Comments

Popular posts from this blog

Angular components not reloading on route change

Spent a long time wondering why route changes caused strange effects on my component, and found out that it was because my component wasn't reloading at all. I had this assumption that when a route parameter changed (e.g. /projects/1 changed to /projects/2 ) the component for the route would be reloaded. But this is not the default behaviour of the Angular router. The default behaviour of the Angular router is to reuse the route if the configuration is the same (and not reload the component). But we can override this by providing a RouteReuseStrategy to our @NgModule: providers: [ { provide: RouteReuseStrategy, useClass: AARouteReuseStrategy } ] The full custom implementation of the RouteReuseStrategy will then be like this (and it's the shouldReuseRoute method that changes the behaviour so that the component is reloaded on route parameter change): export class AARouteReuseStrategy extends RouteReuseStrategy { shouldDetach(route: ActivatedRou...

My VNC based development environment with Visual Studio Code running on Ubuntu

I've used this setup for my development environment for several years - giving me a developer desktop I can access anywhere. Even from my mobile phone. I've been fixing bugs, writing code and deployed emergency fixes from the bus, train and mountain tops. The setup is based on a lightweight desktop environment. There are lot of alternatives, but I've chosen fluxbox. From a plain AWS Amazon Ubuntu 16.04 ec2 instance I've started like this: sudo apt-get update sudo apt-get install fluxbox Download and extract tigervnc from https://github.com/TigerVNC/tigervnc/releases (I downloaded the binary file from https://bintray.com/tigervnc/stable/tigervnc/1.7.0 named tigervnc-1.7.0.x86_64.tar.gz ) then extract: tar -xvzf tigervnc-1.7.0.x86_64.tar.gz You need to install: sudo apt-get install x11-xkb-utils You need to edit .vnc/xstartup: nano .vnc/xstartup last line replace "twm &" with: fluxbox & Then you can start the...

Webassembly and audioworklet

Have been playing a lot recently with webassembly and the upcoming audioworklet web standard. This is exciting and powerful technology, opening up for serious use of the web in production of audio and music. Follow the links from my homepage petersalomonsen.com for live demo and more resources.