diff --git a/HRM.BO/VirtualPaging/VirtualPaging.cs b/HRM.BO/VirtualPaging/VirtualPaging.cs index d4cd09b..4aef283 100644 --- a/HRM.BO/VirtualPaging/VirtualPaging.cs +++ b/HRM.BO/VirtualPaging/VirtualPaging.cs @@ -34,6 +34,12 @@ namespace HRM.BO /// public bool CountTotal { get; set; } + /// + /// True for Export to Excel: every row matching the search, in the grid's sort, comes back as an .xlsx + /// file (VirtualPageResult.ExcelFile) instead of JSON. + /// + public bool ExportExcel { get; set; } + /// A filter as text, or null when it was not sent. public string GetString(string name) { @@ -69,6 +75,12 @@ namespace HRM.BO /// "text" (default), "number", "money", "date" or "datetime". public string Type { get; set; } + + /// Header text in the Excel file. + public string Title { get; set; } + + /// Column width in the grid (px); the Excel column gets a matching width. + public int Width { get; set; } } /// One window of rows for the grid. @@ -82,5 +94,10 @@ namespace HRM.BO /// The rows of this window, holding only the requested columns. public List> Data { get; set; } = new List>(); + + /// The .xlsx file, set only for ExportExcel requests. The controller returns it as a file. + public byte[] ExcelFile { get; set; } + + public const string ExcelContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; } } diff --git a/HRM.DA/DA/VirtualPaging/README.md b/HRM.DA/DA/VirtualPaging/README.md index 439cd70..bb54fd8 100644 --- a/HRM.DA/DA/VirtualPaging/README.md +++ b/HRM.DA/DA/VirtualPaging/README.md @@ -8,6 +8,7 @@ memory, so it works the same for 1,000 or 1,000,000 rows and always shows the la |---|---|---| | Request / result | `HRM.BO/VirtualPaging/VirtualPaging.cs` | `VirtualPageRequest` (filters, columns, skip/take, search, sort) and `VirtualPageResult`, shared by every page | | Paging SQL | `HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs` | Wraps a page's query with search, sort, `OFFSET/FETCH` and the totals | +| Excel | `HRM.DA/DA/VirtualPaging/VirtualPagingExcel.cs` | Streams the rows from SQL Server straight into an `.xlsx` (Open XML SAX writer) | | Grid | `ClientApp/src/app/custom-components/virtual-grid/` (`VirtualGridModule`) | ``: virtual scrolling, search box, total, Export to Excel | ## A new page: 4 small steps (worked example: Discontinue Employee Details) @@ -69,7 +70,10 @@ public ActionResult GetDiscontinueEmployeePage(VirtualPageRequest request) DateTime fromDate = request.GetDate("fromDate", "From Date"); // also GetInt / GetString DateTime toDate = request.GetDate("toDate", "To Date"); - return Ok(_EmployeeService.GetDiscontinuedEmployeePage(request, fromDate, toDate, currentUser.PayrollTypeID ?? 0)); + VirtualPageResult result = _EmployeeService.GetDiscontinuedEmployeePage(request, fromDate, toDate, currentUser.PayrollTypeID ?? 0); + if (result.ExcelFile != null) // Export to Excel + return File(result.ExcelFile, VirtualPageResult.ExcelContentType, "DiscontinueEmployeeDetails.xlsx"); + return Ok(result); } catch (ArgumentException ex) { return BadRequest(ex.Message); } catch (Exception ex) { return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); } @@ -105,7 +109,7 @@ preview() { | Scroll | `skip 100, 200, …` | next 100 rows only (no `COUNT`) | | Sort | `sortField`, `sortDir` | `ORDER BY` that column, then the unique field | | Search (after 400 ms) | `search`, `countTotal true` | `WHERE` every column `LIKE @vpSearch`, new `COUNT` | -| Export to Excel | `take 0` | every matching row, same search and sort; Kendo builds the `.xlsx` | +| Export to Excel | `exportExcel true` | every matching row, same search and sort, written row by row into an `.xlsx` on the server; the browser only downloads the file | ## Rules diff --git a/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs b/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs index 0b56d90..decd485 100644 --- a/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs +++ b/HRM.DA/DA/VirtualPaging/VirtualPagingDA.cs @@ -47,12 +47,14 @@ namespace HRM.DA string source = " FROM (" + query + ") AS vp"; string searchCondition = search.Length > 0 ? SearchCondition(columns) : null; - bool paged = request.Take > 0; + bool export = request.ExportExcel; // every matching row, written straight into an .xlsx + bool paged = !export && request.Take > 0; + bool countTotal = !export && request.CountTotal; var sql = new StringBuilder(); // 1. Totals, only on Preview / new search (scroll and sort do not change them). One scan for both. - if (request.CountTotal) + if (countTotal) { sql.Append("SELECT COUNT(*) AS TotalWithoutSearch, ") .Append(searchCondition == null ? "COUNT(*)" : "ISNULL(SUM(CASE WHEN " + searchCondition + " THEN 1 ELSE 0 END), 0)") @@ -80,7 +82,14 @@ namespace HRM.DA var result = new VirtualPageResult(); using (IDataReader reader = tc.ExecuteReader(CommandType.Text, sql.ToString(), parameters.ToArray())) { - if (request.CountTotal) + // Export to Excel: rows go from SQL Server into the file one by one, never all held in memory. + if (export) + { + result.ExcelFile = VirtualPagingExcel.Create(reader, columns); + return result; + } + + if (countTotal) { if (reader.Read()) { diff --git a/HRM.DA/DA/VirtualPaging/VirtualPagingExcel.cs b/HRM.DA/DA/VirtualPaging/VirtualPagingExcel.cs new file mode 100644 index 0000000..0191b18 --- /dev/null +++ b/HRM.DA/DA/VirtualPaging/VirtualPagingExcel.cs @@ -0,0 +1,211 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Globalization; +using System.IO; +using System.Text; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Spreadsheet; +using HRM.BO; + +namespace HRM.DA +{ + /// + /// Export to Excel for <app-virtual-grid>: writes the rows of a data reader straight into an .xlsx + /// with the Open XML SAX writer (OpenXmlWriter), one row at a time. Fast and light on memory even for + /// very long lists, unlike building the workbook in the browser or as a full object model. + /// + /// The file: a bold, frozen header row with filters, the grid's column titles and widths, dates as real + /// Excel dates ("dd mmm yyyy") and money as numbers ("#,##0.00"). Text is written as text, never as a formula. + /// + internal static class VirtualPagingExcel + { + private const int MaxRows = 1000000; // Excel's limit is 1,048,576 + + // Style indexes in the stylesheet below. + private const uint StyleHeader = 1, StyleDate = 2, StyleDateTime = 3, StyleMoney = 4; + + internal static byte[] Create(IDataReader reader, List columns) + { + // Columns to write: the grid's, or every field of the query when none were sent. + var fields = new List(); + if (columns.Count == 0) + { + for (int i = 0; i < reader.FieldCount; i++) + fields.Add(new ExcelColumn { Ordinal = i, Title = reader.GetName(i), Type = "text", Width = 15 }); + } + else + { + foreach (VirtualPageColumn c in columns) + { + fields.Add(new ExcelColumn + { + Ordinal = reader.GetOrdinal(c.Field), + Title = string.IsNullOrWhiteSpace(c.Title) ? c.Field : c.Title, + Type = (c.Type ?? "text").ToLowerInvariant(), + Width = Math.Max(10, Math.Min(60, (c.Width > 0 ? c.Width : 130) / 7.0)), + }); + } + } + + using (var stream = new MemoryStream()) + { + using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook)) + { + WorkbookPart workbookPart = document.AddWorkbookPart(); + workbookPart.Workbook = new Workbook(); + + WorkbookStylesPart stylesPart = workbookPart.AddNewPart(); + stylesPart.Stylesheet = CreateStylesheet(); + stylesPart.Stylesheet.Save(); + + WorksheetPart sheetPart = workbookPart.AddNewPart(); + int lastRow = WriteSheet(sheetPart, reader, fields); + + string range = "$A$1:$" + ColumnName(fields.Count) + "$" + lastRow; + workbookPart.Workbook.AppendChild(new Sheets( + new Sheet { Id = workbookPart.GetIdOfPart(sheetPart), SheetId = 1U, Name = "Sheet1" })); + workbookPart.Workbook.AppendChild(new DefinedNames( + new DefinedName("'Sheet1'!" + range) { Name = "_xlnm._FilterDatabase", LocalSheetId = 0U, Hidden = true })); + workbookPart.Workbook.Save(); + } + return stream.ToArray(); + } + } + + /// Header + every row, streamed. Returns the last row number. + private static int WriteSheet(WorksheetPart sheetPart, IDataReader reader, List fields) + { + int rowIndex = 1; + using (OpenXmlWriter writer = OpenXmlWriter.Create(sheetPart)) + { + writer.WriteStartElement(new Worksheet()); + + // Header row stays visible while scrolling. + writer.WriteElement(new SheetViews(new SheetView( + new Pane { VerticalSplit = 1D, TopLeftCell = "A2", ActivePane = PaneValues.BottomLeft, State = PaneStateValues.Frozen }) + { WorkbookViewId = 0U })); + + writer.WriteStartElement(new Columns()); + for (int i = 0; i < fields.Count; i++) + writer.WriteElement(new Column { Min = (uint)(i + 1), Max = (uint)(i + 1), Width = fields[i].Width, CustomWidth = true }); + writer.WriteEndElement(); + + writer.WriteStartElement(new SheetData()); + + writer.WriteStartElement(new Row { RowIndex = 1U }); + foreach (ExcelColumn f in fields) + writer.WriteElement(TextCell(f.Title, StyleHeader)); + writer.WriteEndElement(); + + while (rowIndex <= MaxRows && reader.Read()) + { + rowIndex++; + writer.WriteStartElement(new Row { RowIndex = (uint)rowIndex }); + foreach (ExcelColumn f in fields) + writer.WriteElement(ValueCell(reader.IsDBNull(f.Ordinal) ? null : reader.GetValue(f.Ordinal), f.Type)); + writer.WriteEndElement(); + } + + writer.WriteEndElement(); // SheetData + writer.WriteElement(new AutoFilter { Reference = "A1:" + ColumnName(fields.Count) + rowIndex }); + writer.WriteEndElement(); // Worksheet + } + return rowIndex; + } + + private static Cell ValueCell(object value, string type) + { + if (value == null) + return new Cell(); + + if (value is DateTime date && (type == "date" || type == "datetime")) + return new Cell { CellValue = new CellValue(date.ToOADate()), StyleIndex = type == "date" ? StyleDate : StyleDateTime }; + + if (IsNumber(value) && (type == "money" || type == "number")) + { + var cell = new Cell { CellValue = new CellValue(Convert.ToDecimal(value, CultureInfo.InvariantCulture)) }; + if (type == "money") + cell.StyleIndex = StyleMoney; + return cell; + } + + string text = value is DateTime d ? d.ToString("dd MMM yyyy", CultureInfo.InvariantCulture) + : Convert.ToString(value, CultureInfo.InvariantCulture); + return TextCell(text, 0); + } + + /// An inline text cell (never a formula). Characters XML cannot hold are dropped. + private static Cell TextCell(string text, uint style) + { + var cell = new Cell + { + DataType = CellValues.InlineString, + InlineString = new InlineString(new Text(CleanText(text)) { Space = SpaceProcessingModeValues.Preserve }), + }; + if (style != 0) + cell.StyleIndex = style; + return cell; + } + + private static Stylesheet CreateStylesheet() + { + return new Stylesheet( + new NumberingFormats( + new NumberingFormat { NumberFormatId = 164U, FormatCode = "dd mmm yyyy" }, + new NumberingFormat { NumberFormatId = 165U, FormatCode = "dd mmm yyyy hh:mm" }) { Count = 2U }, + new Fonts( + new Font(new FontSize { Val = 11D }, new FontName { Val = "Calibri" }), + new Font(new Bold(), new FontSize { Val = 11D }, new FontName { Val = "Calibri" })) { Count = 2U }, + new Fills( + new Fill(new PatternFill { PatternType = PatternValues.None }), + new Fill(new PatternFill { PatternType = PatternValues.Gray125 }), + new Fill(new PatternFill(new ForegroundColor { Rgb = "FFE8ECF2" }) { PatternType = PatternValues.Solid })) { Count = 3U }, + new Borders(new Border()) { Count = 1U }, + new CellFormats( + new CellFormat(), // 0 default + new CellFormat { FontId = 1U, FillId = 2U, ApplyFont = true, ApplyFill = true }, // 1 header + new CellFormat { NumberFormatId = 164U, ApplyNumberFormat = true }, // 2 date + new CellFormat { NumberFormatId = 165U, ApplyNumberFormat = true }, // 3 date + time + new CellFormat { NumberFormatId = 4U, ApplyNumberFormat = true }) { Count = 5U }); // 4 money #,##0.00 + } + + private static bool IsNumber(object v) => + v is decimal || v is double || v is float || v is int || v is long || v is short || v is byte; + + private static string CleanText(string text) + { + if (string.IsNullOrEmpty(text)) + return string.Empty; + var sb = new StringBuilder(text.Length); + foreach (char ch in text) + { + if (ch == '\t' || ch == '\n' || ch == '\r' || ch >= 0x20 && ch != 0xFFFE && ch != 0xFFFF) + sb.Append(ch); + } + return sb.ToString(); + } + + /// 1 → A, 27 → AA. + private static string ColumnName(int number) + { + string name = string.Empty; + while (number > 0) + { + int m = (number - 1) % 26; + name = (char)('A' + m) + name; + number = (number - 1) / 26; + } + return name; + } + + private sealed class ExcelColumn + { + public int Ordinal; + public string Title; + public string Type; + public double Width; + } + } +} diff --git a/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts b/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts index e4528f0..3b5c1f2 100644 --- a/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts +++ b/HRM.UI/ClientApp/src/app/_models/common/virtual-grid.ts @@ -18,13 +18,14 @@ export interface VirtualGridColumn { /** What the grid posts to the page endpoint (VirtualPageRequest on the server). */ export interface VirtualPageRequest { filters: { [name: string]: string }; - columns: { field: string; type: VirtualGridColumnType }[]; + columns: { field: string; type: VirtualGridColumnType; title: string; width: number }[]; skip: number; take: number; // 0 = all rows (Export to Excel) search?: string; sortField?: string; sortDir?: 'asc' | 'desc'; countTotal: boolean; // true on Preview and on a new search; scroll/sort keep the totals + exportExcel?: boolean; // true: the server returns every matching row as an .xlsx file } /** What the page endpoint returns (VirtualPageResult on the server). */ diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html index 5609f05..ee2de8e 100644 --- a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.html @@ -39,15 +39,6 @@ } - - - @for (column of columns; track column.field) { - - - } - - {{ noRecordsText }} diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts index 781d4e8..24d80e1 100644 --- a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.component.ts @@ -1,12 +1,12 @@ import { - ChangeDetectionStrategy, Component, EventEmitter, Input, OnDestroy, OnInit, Optional, Output, ViewChild + ChangeDetectionStrategy, Component, EventEmitter, Input, OnDestroy, OnInit, Optional, Output } from '@angular/core'; import { formatDate } from '@angular/common'; import { of, Subject, Subscription } from 'rxjs'; import { catchError, debounceTime, distinctUntilChanged, finalize, switchMap } from 'rxjs/operators'; -import { ExcelExportData } from '@progress/kendo-angular-excel-export'; -import { GridComponent, GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid'; +import { GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid'; import { SortDescriptor } from '@progress/kendo-data-query'; +import { saveAs } from 'file-saver'; import { ApiService } from 'src/app/app.api.service'; import { HRMNotificationService } from 'src/app/app.notification.service'; import { loadingPanelService } from 'src/app/hrm-loding panel/loding.panel.service'; @@ -51,8 +51,6 @@ export class VirtualGridComponent implements OnInit, OnDestroy { /** After every loaded window. */ @Output() loaded = new EventEmitter(); - @ViewChild(GridComponent) kendoGrid: GridComponent; - started = false; gridView: GridDataResult = { data: [], total: 0 }; totalWithoutSearch = 0; @@ -62,7 +60,6 @@ export class VirtualGridComponent implements OnInit, OnDestroy { searchText = ''; appliedSearch = ''; exporting = false; - exportRows: any[] = []; private filters: { [name: string]: string } = {}; private firstLoad = false; @@ -143,32 +140,24 @@ export class VirtualGridComponent implements OnInit, OnDestroy { this.search$.next((text || '').trim()); } - /** Loads every matching row (same search and sort), then lets Kendo build the Excel file. */ + /** + * The server builds the .xlsx (every row matching the search, in the grid's sort) and the browser + * only downloads it, so a long list never has to be loaded or processed in the page. + */ exportExcel(): void { if (!this.started || this.gridView.total === 0 || this.exporting) { return; } this.setExporting(true); - this.apiService.httpPost(this.url, this.buildRequest(0, 0, false)).subscribe( - (result) => { - this.exportRows = (result.data || []).map(row => this.prepareRow(row)); - setTimeout(() => this.kendoGrid.saveAsExcel()); - }, - (err) => { - this.setExporting(false); - this.showError(this.errorText(err, 'Failed to export to Excel.')); - } + const request: VirtualPageRequest = { ...this.buildRequest(0, 0, false), exportExcel: true }; + this.apiService.httpDownloadFile(this.url, request).pipe( + finalize(() => this.setExporting(false)) + ).subscribe( + (file: Blob) => saveAs(file, this.exportFileName), + (err) => this.showDownloadError(err, 'Failed to export to Excel.') ); } - /** Kendo asks for the rows to put in the Excel file. */ - excelData = (): ExcelExportData => ({ data: this.exportRows }); - - onExcelCreated(): void { - this.exportRows = []; - this.setExporting(false); - } - /** Grid display format by column type. */ displayFormat(column: VirtualGridColumn): string { switch (column.type) { @@ -179,17 +168,6 @@ export class VirtualGridComponent implements OnInit, OnDestroy { } } - /** Excel cell format by column type. */ - excelOptions(column: VirtualGridColumn): any { - switch (column.type) { - case 'date': return { format: 'dd MMM yyyy' }; - case 'datetime': return { format: 'dd MMM yyyy HH:mm' }; - case 'money': return { format: '#,##0.00', textAlign: 'right' }; - case 'number': return { textAlign: 'right' }; - default: return {}; - } - } - isNumber(column: VirtualGridColumn): boolean { return column.type === 'money' || column.type === 'number'; } @@ -212,7 +190,7 @@ export class VirtualGridComponent implements OnInit, OnDestroy { const sort = this.sort && this.sort.length > 0 && this.sort[0].dir ? this.sort[0] : null; return { filters: this.filters, - columns: this.columns.map(c => ({ field: c.field, type: c.type || 'text' })), + columns: this.columns.map(c => ({ field: c.field, type: c.type || 'text', title: c.title, width: this.columnWidth(c) })), skip: skip, take: take, search: this.appliedSearch || undefined, @@ -267,6 +245,16 @@ export class VirtualGridComponent implements OnInit, OnDestroy { return fallback; } + /** A failed download returns its error message as a Blob; read it before showing it. */ + private showDownloadError(err: any, fallback: string): void { + const blob: Blob = err instanceof Blob ? err : (err && err.error instanceof Blob ? err.error : null); + if (blob) { + blob.text().then(text => this.showError(text || fallback), () => this.showError(fallback)); + } else { + this.showError(this.errorText(err, fallback)); + } + } + private showError(message: string): void { if (this.notificationService) { this.notificationService.showError(message); diff --git a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts index 8ba6e01..3479021 100644 --- a/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts +++ b/HRM.UI/ClientApp/src/app/custom-components/virtual-grid/virtual-grid.module.ts @@ -1,8 +1,7 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; -import { ExcelModule, GridModule } from '@progress/kendo-angular-grid'; -import { ExcelExportModule } from '@progress/kendo-angular-excel-export'; +import { GridModule } from '@progress/kendo-angular-grid'; import { ButtonsModule } from '@progress/kendo-angular-buttons'; import { TextBoxModule } from '@progress/kendo-angular-inputs'; import { VirtualGridComponent } from './virtual-grid.component'; @@ -10,7 +9,7 @@ import { VirtualGridComponent } from './virtual-grid.component'; /** Import this module to use (common virtual-scrolling grid). */ @NgModule({ declarations: [VirtualGridComponent], - imports: [CommonModule, FormsModule, GridModule, ExcelModule, ExcelExportModule, ButtonsModule, TextBoxModule], + imports: [CommonModule, FormsModule, GridModule, ButtonsModule, TextBoxModule], exports: [VirtualGridComponent] }) export class VirtualGridModule {} diff --git a/HRM.UI/Controllers/Employee/EmployeeController.cs b/HRM.UI/Controllers/Employee/EmployeeController.cs index 34e9e6b..6bc47af 100644 --- a/HRM.UI/Controllers/Employee/EmployeeController.cs +++ b/HRM.UI/Controllers/Employee/EmployeeController.cs @@ -3428,7 +3428,10 @@ namespace HRM.UI.Controllers if (fromDate > toDate) return BadRequest("From Date cannot be after To Date."); - return Ok(_EmployeeService.GetDiscontinuedEmployeePage(request, fromDate, toDate, currentUser.PayrollTypeID ?? 0)); + VirtualPageResult result = _EmployeeService.GetDiscontinuedEmployeePage(request, fromDate, toDate, currentUser.PayrollTypeID ?? 0); + if (result.ExcelFile != null) // Export to Excel + return File(result.ExcelFile, VirtualPageResult.ExcelContentType, "DiscontinueEmployeeDetails.xlsx"); + return Ok(result); } catch (ArgumentException ex) {