From: <b_c...@us...> - 2012-09-25 23:49:26
|
Revision: 6370 http://jaffa.svn.sourceforge.net/jaffa/?rev=6370&view=rev Author: b_calderwood Date: 2012-09-25 23:49:20 +0000 (Tue, 25 Sep 2012) Log Message: ----------- Modify simple export to excel to support grouped headers in the grid. Modified Paths: -------------- trunk/JaffaRIA/source/html/js/extjs/jaffa/grid/SimpleExportToExcelPlugin.js trunk/JaffaRIA/source/html/js/extjs/jaffa/grid/simpleExportToExcel.jsp Modified: trunk/JaffaRIA/source/html/js/extjs/jaffa/grid/SimpleExportToExcelPlugin.js =================================================================== --- trunk/JaffaRIA/source/html/js/extjs/jaffa/grid/SimpleExportToExcelPlugin.js 2012-09-25 21:50:07 UTC (rev 6369) +++ trunk/JaffaRIA/source/html/js/extjs/jaffa/grid/SimpleExportToExcelPlugin.js 2012-09-25 23:49:20 UTC (rev 6370) @@ -40,6 +40,41 @@ var exportColumns = []; var exportRecords = []; var columns = this.getColumnModel().config; + + var headers = []; + + var tables = this.getView().mainHd.query('.x-grid3-header-offset > table'), tw = this.getView().getTotalWidth(), rows = this.getView().cm.rows; + for(var row = 0; row < tables.length; row++){ + tables[row].style.width = tw; + header = []; + if(rows && row < rows.length){ + var cells = tables[row].firstChild.firstChild.childNodes; + for(var i = 0, gcol = 0; i < cells.length; i++){ + var group = rows[row][i]; + if((typeof col != 'number') || (col >= gcol && col < gcol + group.colspan)){ + + var width = 0, hidden = true, colCount = 0; + for(var j = gcol, len = gcol + group.colspan; j < len; j++){ + if(!this.getView().cm.isHidden(j)){ + var cw = this.getView().cm.getColumnWidth(j); + if(typeof cw == 'number'){ + width += cw; + } + hidden = false; + colCount=colCount+1; + } + } + if (colCount > 0) + header[header.length]={colspan:colCount,header:group.header||""}; + } + gcol += group.colspan; + } + headers[headers.length]=header; + } + } + + + for (var i = 0; i < columns.length; i++){ if (columns[i].dataIndex && columns[i].dataIndex!='' && !columns[i].hidden){ exportColumns[exportColumns.length]={header:columns[i].header,dataIndex:columns[i].dataIndex, width:columns[i].width}; @@ -54,6 +89,7 @@ }); var exportData = { + headers:headers, columns:exportColumns, records:exportRecords }; Modified: trunk/JaffaRIA/source/html/js/extjs/jaffa/grid/simpleExportToExcel.jsp =================================================================== --- trunk/JaffaRIA/source/html/js/extjs/jaffa/grid/simpleExportToExcel.jsp 2012-09-25 21:50:07 UTC (rev 6369) +++ trunk/JaffaRIA/source/html/js/extjs/jaffa/grid/simpleExportToExcel.jsp 2012-09-25 23:49:20 UTC (rev 6370) @@ -7,11 +7,13 @@ <%@ page import = "org.apache.poi.ss.usermodel.CellStyle" %> <%@ page import = "org.apache.poi.ss.usermodel.IndexedColors" %> <%@ page import = "org.apache.poi.ss.usermodel.CreationHelper" %> +<%@ page import = "org.apache.poi.ss.usermodel.Cell" %> <%@ page import = "org.apache.poi.ss.usermodel.Row" %> <%@ page import = "org.apache.poi.ss.usermodel.Sheet" %> <%@ page import = "org.apache.poi.ss.usermodel.Workbook" %> <%@ page import = "org.apache.poi.hssf.usermodel.HSSFWorkbook" %> <%@ page import = "org.apache.poi.ss.usermodel.Font" %> +<%@ page import = "org.apache.poi.ss.util.CellRangeAddress" %> <%! private static final Logger log = Logger.getLogger("jaffa.grid.simpleExportToExcel"); @@ -26,9 +28,6 @@ //Creating worksheet Sheet sheet = wb.createSheet(sheetName); - // Freeze header row - sheet.createFreezePane(0, 1, 0, 1); - //creating a custom palette for the workbook CellStyle style = wb.createCellStyle(); style = wb.createCellStyle(); @@ -44,7 +43,7 @@ style.setLeftBorderColor(IndexedColors.GREEN.getIndex()); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); - style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); + style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_CENTER); @@ -54,10 +53,44 @@ style.setFont(titleFont); JSONObject gridJson = (JSONObject) JSONSerializer.toJSON(jsonStr); - int rowCount = 1; + int headerRowCount = 0; + // Creating the group header rows. + JSONArray headerArray = new JSONArray(); + if (gridJson.optJSONArray("headers") != null) + headerArray = gridJson.optJSONArray("headers"); + + for (int i = 0; i < headerArray.size(); i++) { + Row row = sheet.createRow((short) headerRowCount); + headerRowCount = headerRowCount + 1; + JSONArray headerRecord = (JSONArray) headerArray.get(i); + int colIndex = 0; + for (int j = 0; j < headerRecord.size(); j++) { + JSONObject headerData = (JSONObject) headerRecord.get(j); + int span = 1; + if (headerData.getString("colspan")!=null && Integer.parseInt(headerData.getString("colspan"))>1){ + span = Integer.parseInt(headerData.getString("colspan")); + } + row.createCell(colIndex).setCellValue(headerData.getString("header")); + Cell cell = row.getCell(colIndex); + cell.setCellStyle(style); + sheet.autoSizeColumn(colIndex); + if (span>1){ + //merge cells in group header together + sheet.addMergedRegion(new CellRangeAddress(cell.getRowIndex(),cell.getRowIndex(),cell.getColumnIndex(),(cell.getColumnIndex() + span -1))); + //apply border to all cells in merged header + for (int k = 1; k < span; k++){ + row.createCell(colIndex+k); + row.getCell(colIndex+k).setCellStyle(style); + } + } + colIndex = colIndex + span; + } + } + // Creating the header row. - Row row = sheet.createRow((short) 0); + Row row = sheet.createRow((short) headerRowCount); + headerRowCount = headerRowCount + 1; JSONArray columns = gridJson.getJSONArray("columns"); @@ -70,6 +103,8 @@ JSONArray records = gridJson.getJSONArray("records"); + int rowCount = headerRowCount; + for (int j = 0; j < records.size(); j++) { row = sheet.createRow((short) rowCount); JSONObject recordData = (JSONObject) records.get(j); @@ -84,6 +119,9 @@ rowCount++; } + // Freeze header rows + sheet.createFreezePane(0, headerRowCount); + } catch (Exception e) { e.printStackTrace(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |