While Cognos reports offer various export options directly, I have specific criteria that necessitate the creation of a custom "Export to Excel" button within the report.
Although I have the option to use cogbox, embedding the report in an iframe causes the entire menu bar to be hidden. Consequently, I was tasked with implementing a button to handle the export functionality within the report.
Through research, I discovered the potential use of Custom Controls, allowing the external embedding of JavaScript files through extensions. I successfully wrote code by referencing examples from various sources to read the report and execute the export option. However, I find that a deeper understanding of the OControlHost object is necessary for further refinement.
Could you please provide some insights or guidance on shaping this implementation?
I have Simple list Report , That I'm trying to Export to Excel Format.
Here is the Code I'm Using in JS file.
define(function() {
"use strict";
function DownloadReportToExcel() {
}
DownloadReportToExcel.prototype.draw = function(oControlHost) {
// Customize the appearance if needed
var el = oControlHost.container;
el.innerHTML = '<button id="downloadButton">Download Excel</button>';
var downloadButton = document.getElementById('downloadButton');
downloadButton.addEventListener('click', function() {
this.downloadExcel(oControlHost);
}.bind(this));
};
DownloadReportToExcel.prototype.downloadExcel = function(oControlHost) {
// Fetch all data from the report
var allData = this.getAllReportData(oControlHost);
// Create a Blob with the Excel content
var blob = new Blob([this.generateExcelContent(allData)], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
// Create a link element to trigger the download
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'report.xlsx';
// Append the link to the document and trigger the download
document.body.appendChild(link);
link.click();
// Remove the link from the document
document.body.removeChild(link);
// Clean up
URL.revokeObjectURL(link.href);
};
DownloadReportToExcel.prototype.getAllReportData = function(oControlHost) {
var allData = [];
console.log('oControlHost:', oControlHost);
// Use oControlHost directly to access methods and properties
var dataSet = oControlHost._191;
// Check if dataSet is defined
if (dataSet) {
// Iterate through rows and columns to retrieve data
for (var iRow = 0; iRow < dataSet.rowCount; iRow++) {
var rowData = [];
for (var iCol = 0; iCol < dataSet.columnCount; iCol++) {
var cellValue = dataSet.getCell(iRow, iCol).getFormattedValue();
rowData.push(cellValue);
}
allData.push(rowData);
}
} else {
console.error('Data set not found in the oControlHost instance.');
}
return allData;
};
DownloadReportToExcel.prototype.generateExcelContent = function(data) {
// Implement the logic to generate Excel content
// For simplicity, you can use a library like SheetJS (xlsx) or manually create Excel content
var excelContent = '';
console.log('data:', data);
// Add headers
var headers = data[0];
excelContent += headers.join('\t') + '\n';
// Add data rows
for (var i = 1; i < data.length; i++) {
excelContent += data[i].join('\t') + '\n';
}
return excelContent;
};
DownloadReportToExcel.prototype.setData = function(oControlHost, oDataStore) {
// If data is needed, handle it here
};
DownloadReportToExcel.prototype.getParameters = function() {
// If parameters are needed, handle them here
return [];
};
DownloadReportToExcel.prototype.isInValidState = function() {
return true;
};
return DownloadReportToExcel;
});
------------------------------
Loyapally Ranjith
------------------------------