▦K2 Advanced GridsDeveloper Portal / 0.24
Documentation / Data export

CSV export

Lesson 14 · Part A — Export the Purchase Orders register as a portable CSV file without changing the underlying K2 ListView.

Before you begin

Continue from Lesson 13 — Card View. Use the 250-record Purchase Orders demo. The engine is unchanged.

Download basic export configuration ↓
Required after saving a configuration file

Replace REPLACE_WITH_YOUR_AREAITEMID with your runtime Area Item ID. Save the chosen configuration as purchase-orders.config.js in the server config folder, increment configVersion in the Data Label loader and reload the form. Console changes are temporary.

1. Enable export

K2AdvancedGridsConfig.defaults.export = {
  "enabled": true,
  "showToolbarButton": true,
  "allowUserOptions": true,
  "defaultFormat": "xlsx",
  "defaultScope": "filtered",
  "defaultColumns": "visible",
  "preserveColumnOrder": true,
  "fileName": "PurchaseOrders",
  "appendTimestamp": true,
  "includeSummary": true,
  "includeGrouping": false,
  "includeFormatting": false,
  "csv": {
    "delimiter": ",",
    "includeBOM": true
  },
  "excel": {
    "sheetName": "Data",
    "freezeHeader": true,
    "freezePinned": true,
    "autoFitColumns": true,
    "autoFilter": true,
    "nativeConditionalFormatting": false
  }
};
K2AdvancedGrids.instance.refresh();
Export toolbar button
Figure 125 · Export toolbar button · Actual test capture
Export options dialog
Figure 126 · Export options dialog · Actual test capture

2. Download CSV

Select CSV, All loaded records and All columns. Choose comma as delimiter and download. CSV contains the header and raw record values, not Excel styles, summary sheets or group sheets. UTF-8 BOM is enabled by default for multilingual data; delimiters supported by the engine are comma, semicolon, tab and pipe. Excel may interpret dates or numbers using local settings; widen columns if dates appear as ####.

CSV export settings: 250 records and 17 columns
Figure 127 · CSV export settings: 250 records and 17 columns · Actual test capture
CSV opened in Excel
Figure 128 · CSV opened in Excel · Actual test capture

3. Record scope

Loaded exports all records currently loaded into the grid, not unseen server records. Filtered exports all matching loaded records, across pages. Page exports the current page dataset. Apply Status = Approved to reproduce the 50-record filtered scenario, then export the current page of 10 records. See Lesson 2 and Lesson 3.

All 250 loaded records (Excel example)
Figure 129 · All 250 loaded records (Excel example) · Actual test capture
Filtered CSV: 50 Approved records
Figure 130 · Filtered CSV: 50 Approved records · Actual test capture
Current page CSV: 10 records
Figure 131 · Current page CSV: 10 records · Actual test capture

4. Column selection and order

Visible columns only excludes hidden columns; All columns includes the underlying columns. The configured preserveColumnOrder option retains the current column order, including pinned zones. See Lesson 6 — Column management. Hidden columns are not a security boundary.

Visible columns only (Excel example)
Figure 132 · Visible columns only (Excel example) · Actual test capture
All columns (Excel example)
Figure 133 · All columns (Excel example) · Actual test capture
Reordered columns and sorted records (Excel example)
Figure 134 · Reordered columns and sorted records (Excel example) · Actual test capture

5. Diagnostics

With Status = Overdue and page size 10, run this read-only diagnostic in the browser console. It uses the CSV builder without downloading a file.

{
  const ag = K2AdvancedGrids.instance;
  const id = ag.inspect()[0].id; // choose the target grid on multi-grid forms
  console.table(["loaded", "filtered", "page"].map(scope => {
    const data = ag.buildCSV(id, {scope, columns: "visible"});
    return {scope, records: data.records, columns: data.columns};
  }));
}
Diagnostic: loaded 250, filtered 50, page 10; nine columns
Figure 151 · Diagnostic: loaded 250, filtered 50, page 10; nine columns · Actual test capture

Knowledge check

Can you export all matching records while showing only ten rows? Can you export hidden columns? Does CSV retain worksheet formatting? Expected answers: yes, yes if All columns is selected, and no.

Enlarged screenshot