This blog will let you know to export excel using apache poi hwf and customize the excel like adding more information like the filters used in the report, heading.
We should create an object in HTML file which contains the map-id from an efwd file, all the required input parameters for executing the query and more information such as filters used or the date of exporting the excel
var dataToSend = { dir: window.DashboardGlobals.folderpath, //directory of main folder map_id: 1, //map id in efwd file FIlter_1: Dashboard.getVariable('weekValue'), Filter_2: Dashboard.getVariable('monthValue'), Filter_3:Dashboard.getVariable('yearValue') //all the required parameters for executing query }; openHWF('POST',encodeURI(url), {query: JSON.stringify(dataToSend), dataCountPerSheet: '100'} ,'_self' );
Where URL is the path of the hwf file from the main folder, dataToSend is the map created above to pass the data to the hwf file and the parameters for query execution, dataCountPerSheet is to specify the number of records per sheet and the function is called with all the required data as arguments.
In HWF file we can get all the data like the output of the query executed, all the extra information we sent through the object created earlier through post request like
JSONObject queryData = req.dataToQuery; JSONArray data = queryResult.data;
Start by creating an excel workbook and create a sheet in that workbook as follows
HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet worksheet = wb.createSheet("Sheet");
We can create as many sheets we require based on the number of records per sheet and should create every row we needed, every cell in that row. Set a cell value, apply styles and we can even merge cells. Should iterate through all the values in object and display as follows
HSSFRow paramrowH = worksheet.createRow( row number ); HSSFCell cellTitle = paramrowH.createCell( cell number ); cellTitle.setCellValue(" excel title "); cellTitle.setCellStyle(cellTitlStyle); worksheet.addMergedRegion(new CellRangeAddress( row start, row end, col start, col end));
In the above code, we have used cellStyle ‘cellTitlStyle’, we can create different cell styles, up to 4000. In each cell style, we can have different font styles which we can customize its color, family, size, italic or bold etc…. The created font can be used while creating cell style. While creating cell style we can customize its color, background color, borders, border colors, text alignment, vertical alignment and many more and for all these customizations there are predefined functions and colors which we should use.
HSSFFont titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short) 16); titleFont.setFontName("Arial"); titleFont.setColor(IndexedColors.BLACK.getIndex()); titleFont.setBold(true); titleFont.setItalic(false); HSSFCellStyle cellTitlStyle = wb.createCellStyle(); cellTitlStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex()); cellTitlStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cellTitlStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cellTitlStyle.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER); cellTitlStyle.setFont(titleFont);
Different styles for table heading and table data can be achieved by creating different styles and using them appropriately. We can create a separate function to create a heading so that we can call this function to create a heading in every sheet. We will keep a count of all the data rows so that we can create a new sheet and put the load into a new sheet. Should iterate through the data object from the request and create a row for every record and cell for every value in the record.
responseJson.put("reportName","EXCEL EXPORT"); // name of the excel exported
For further queries please get back to us at support@helicalinsight.com