MobyDog
03/29/2024, 5:14 PMMobyDog
03/29/2024, 5:14 PMhtml
<div class="welcome-dashboard" ng-controller="GatedDataExportController as vm">
...
<div class="form-group">
<label for="startDate">Start Date:</label>
<input type="date" id="startDate" ng-model="startDate">
</div>
<div class="form-group">
<label for="endDate">End Date:</label>
<input type="date" id="endDate" ng-model="endDate">
</div>
<button class="btn btn-primary" ng-click="exportData()">Export</button>
..
</div>
js
angular.module("umbraco").controller("GatedDataExportController", function ($scope, $http) {
var vm = this;
alert("hello world");
$scope.exportData = function () {
var startDate = $scope.startDate.toISOString();
var endDate = $scope.endDate.toISOString();
// Call backend API to trigger export
$http.get("/umbraco/backoffice/Website/DataExport/GetGatedExport?from=" + startDate + "&to=" + endDate)
.then(function (response) {
console.log("Export successful");
})
.catch(function (error) {
console.error("Export failed:", error);
});
};
});
MobyDog
03/29/2024, 5:15 PMcs
public IActionResult GetGatedExport(string from, string to)
{
// get dates ...
using (var scope = scopeProvider.CreateScope(autoComplete: true))
{
var db = scope.Database;
string strSProc = String.Format(";EXEC sp_gac_export @@gac_startDate='{0}', @@gac_endDate='{1}'", fromDate, toDate);
var results = db.FetchMultiple<ExportRegistrations, ExportDownloads, ExportDownloadByRegistration, ExportDownloadByEmail>(strSProc);
List<ExportRegistrations> exportRegistrations = results.Item1;
//....
if (exportRegistrations.Count > 0 || exportDownloads.Count > 0 || exportDownloadByRegistrations.Count > 0 || exportDownloadByEmails.Count > 0)
{
var workbook = new XLWorkbook();
// Export Registrations
if (exportRegistrations.Count > 0)
{
var worksheet = workbook.Worksheets.Add("ExportRegistrations");
PopulateWorksheet(worksheet, exportRegistrations);
}
//...continued
// Create a memory stream to save the workbook
using (var stream = new MemoryStream())
{
Console.WriteLine("using var stream");
workbook.SaveAs(stream);
var content = stream.ToArray();
Console.WriteLine("return file");
return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "dataexport.xlsx");
}
}
else
{
// If there are no records to export, return an empty file
return File(new byte[0], "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "empty.xlsx");
}
}
}
MobyDog
03/29/2024, 5:30 PMMobyDog
03/29/2024, 8:23 PMA hub and casual space for you to interact with fellow community members and learn more about Umbraco!
Powered by