Excel

Exports or imports Microsoft Excel data.

Not intended for exporting large amounts of data.

Component modes: Export | Export with Template | Import

Component mode: Import

Reads data from an Excel file and puts all the cell values in temp tables to be imported to the database. If the worksheet contains formulas then only their calculated value will be passed to the procedure.

File modes are not applicable to this component mode.

File modes: Download | Save | Save and download | Batch

File mode: Save and download

The file is saved in the database and the user downloads a copy.

Repeated worksheets (N/A for import mode)

By default each worksheet in an Excel template maps to one worksheet in the final document. Should you need more flexibility then you can use repeated worksheets, where a worksheet in the template maps to zero to many worksheets in the final document.

Each worksheet in the template whose name is wrapped in {} is treated as a repeated worksheet and the "Get repeated sheet" call (see below) is invoked for it. This call returns the id values and sheet names of the final sheets.

Restrictions

Repeated worksheets can not use any advanced features, including but not limited to charts, formulas and named cells.

SQL

SQL Call: Init (mandatory)

Retrieves the IDs of the documents to import. A Begin Document call will be made for each document.

May modify database: Yes

Parameters

@Action string
Will be set to "Init".

Resultset: Batch ID (optional)

The batch ID. Will be passed as @BatchId to all following calls.
Table count: repeated zero or one time
Row count: exactly one row
Columns
BatchId mandatory string
The batch ID.

Resultset: ID values

Which documents to import.
Table count: repeated exactly once
Row count: zero or more rows
Columns
<xxx> mandatory any
The ID for the document. The name of this column will be used as the ID parameter in the following calls.
ImportMode optional string
Determines the structure of the temp table passed to the "AppendRows" call. See "AppendRows" for more details.

Rows mode is recommended as it is easier to work with. Cells mode is best reserved for when the document contains dynamic columns or too many columns (SQL server supports at most 1024 columns in a table) and you can not persuade the party generating the document to output data in a form that is easier to import.
Default: Rows.
Possible value Description
Cells The temp table for "AppendRows" will contain one row per cell in the sheet.
Rows The temp table for "AppendRows" will contain one row per row in the sheet.

SQL Call: Begin document (mandatory)

SQL call to retrieve the excel document data to be imported.

May modify database: No

Parameters

@<xxx> string
The ID of the document.
@Action string
Will be set to "BeginDocument".
@BatchId string
Only used if a batch ID was given.

Resultset: Document data

Should contain either the binary data of the document or the file path to the document.
Table count: repeated exactly once
Row count: exactly one row
Columns
DocumentData optional binary
The binary data of the document. Only one of the columns "DocumentData" and "DocumentPath" may be used at once.
DocumentPath optional string
The file path to the excel document on disc. Only one of the columns "DocumentData" and "DocumentPath" may be used at once.

SQL Call: Begin worksheet (mandatory)

Will be called for once for each worksheet in the document.

May modify database: No

Parameters

@<xxx> string
The ID of the document.
@Action string
Will be set to "BeginWorkSheet".
@BatchId string
Only used if a batch ID was given.
@WorkSheetName string
The name of the worksheet.

Resultset: Extra parameters (optional)

Optional extra parameters that should be passed to the Append rows and End worksheet calls for this worksheet.
Table count: repeated zero or one time
Row count: exactly one row
Columns
<xxx> mandatory any
An extra parameter. The name of this column is used as the parameter name. The table should contain one column for each parameter.

SQL Call: Append rows (mandatory)

Called when rows have been parsed from the worksheet. Will be called multiple times (with a subset of the rows each time) if the worksheet contains many rows.

May modify database: Yes

Parameters

@<xxx> string
The ID of the document.
@<xxx> (extra parameter) string
Any extra parameters.
@Action string
Will be set to "AppendRows".
@BatchId string
Only used if a batch ID was given.
@WorkSheetName string
The name of the current worksheet.

Resultset: #Cells (Import mode "Cells")

Temp table that contains the cell values that have been parsed. This will be the structure of the table when using Import mode "Cells".
Table count: repeated exactly once
Row count: zero or more rows
Columns
RowNumber mandatory int
The row number of the cell.
ColumnName mandatory string
The column letter of the cell.
ColumnNumber mandatory int
The column number. A will be 1, E will be 5 etc.
CellValue mandatory string
The value of the cell. If the cell contained a date then this value will be in a format that is convertible to a datetime.

Resultset: #Cells (Import mode "Rows")

Temp table that contains the cell values that have been parsed. This will be the structure of the table when using Import mode "Rows".
Table count: repeated exactly once
Row count: zero or more rows
Columns
RowNumber mandatory int
The row number.
A mandatory string
The cell value for column A in the row.
... <X> mandatory string
The cell value for column <X> in the row. The table will contain one column for every column letter from A to <X> where <X> is the right most column in the worksheet that contains a value.

SQL Call: End worksheet (mandatory)

Called when a worksheet has finished processing.

May modify database: Yes

Parameters

@<xxx> string
The ID of the document.
@<xxx> (extra parameter) string
Any extra parameters.
@Action string
Will be set to "EndWorkSheet".
@BatchId string
Only used if a batch ID was given.
@WorkSheetName string
The name of the worksheet.

SQL Call: End document (mandatory)

Called when a document has finished processing.

May modify database: Yes

Parameters

@<xxx> string
The ID of the document.
@Action string
Will be set to "EndDocument".
@BatchId string
Only used if a batch ID was given.

SQL Call: Store error (mandatory)

Called when an error has occured while processing a document. If an error occurs then the document will be skipped and the next document will start processing.

May modify database: Yes

Parameters

@<xxx> string
The ID of the document.
@Action string
Will be set to "StoreError".
@ErrorMessage string
The cause of the error.

SQL Call: Finished (mandatory)

Will be called once every document has finished processing.

May modify database: Yes

Parameters

@Action string
Will be set to "Finished".
@BatchId string
Only used if a batch ID was given.

Resultset: Forwarding definitions (optional)

Table count: repeated zero or more times
Row count: exactly one row
Columns
ADMIN_ErrorMessage optional string

Displays a user friendly error message to the user. This blocks any forwarding for the user.

ADMIN_ConfirmDelete optional string

Displays a user friendly confirmation message to the user with a delete button as the confirm button. If the user chooses OK the SQL call will be rerun with the parameter @ButtonAction set to 'Delete'.

ADMIN_ConfirmQuestion optional string

Displays a user friendly confirmation message to the user with a question style. If the user chooses OK the SQL call will be rerun with the parameter @ButtonAction set to 'Confirm'.

ADMIN_ConfirmWarning optional string

Displays a user friendly confirmation message to the user with a warning style. If the user chooses OK the SQL call will be rerun with the parameter @ButtonAction set to 'Confirm'.

ADMIN_InfoMessage optional string

Displays a user friendly info message to the user. When the user clicks OK the user is forwarded.

ADMIN_SuccessMessage optional string

Displays a user friendly success message to the user. When the user clicks OK the user is forwarded.

ADMIN_Dialog optional string

The dialog alias of a predefined dialog to show the user. Must be the first column in the result set table. Use multiple result set tables to combine with other forwarding.

Use the menu item "Admin > Dialogs" to register new dialogs or find aliases for existing ones.

<xxx> (for ADMIN_Dialog) optional any

Any column without special meaning in the result set with the first column ADMIN_Dialog will be used to make replacements of placeholders in the message and title text.

ADMIN_DebugInfo optional string

Additional information to show the developer when using ADMIN_Dialog.

<passing_field> optional string
Any column with no other specific meaning will pe passed along to the menu item you are forwarding to.
ADMIN_CancelCidStepsBack optional int
Number of steps in the page history to jump back if the user clicks Cancel in an ADMIN_FORCE dialog (the default being none). This value overrides cancelcidstepsback specified in the query string.
ADMIN_CancelReturnToMenuItem optional string
Jumps back to the menu item with this alias if the user clicks Cancel in an ADMIN_FORCE dialog (the default being none). If no prior menu item is found with the given alias, then an error is thrown.
ADMIN_CancelMenuItem optional string
Alias of the menu item to execute if the user clicks Cancel in an ADMIN_FORCE dialog (the default being none). This value overrides cancelmenuitemid specified in the query string.
ADMIN_CancelMenuItemId optional int

Deprecated. Use ADMIN_CancelMenuItem instead.

Id of the menuitem to execute if the user clicks Cancel in an ADMIN_FORCE dialog (the default being none). This value overrides cancelmenuitemid specified in the query string.

ADMIN_CidStepsBack optional int
Number of steps in the page history to jump back after execution (the default being one step back). This value overrides any destination specified by the query string.
ADMIN_ReturnToMenuItem optional string
Jumps back to the menu item with this alias after execution. This value overrides any destination specified by the query string. If no prior menu item is found with the given alias, then an error is thrown.
ADMIN_Force optional string
Prompts the user with the specified text and the user may answer OK or cancel. If the user chooses OK the sql call will be rerun with the parameter @force set to 1.
ADMIN_Forward optional string
Displays a user friendly message and then forwards to the next menu item.
ADMIN_ForwardLink optional string
Alias of the link to forward to.
ADMIN_ForwardMenuGroup optional string

Alias of the menu group to show after execution (instead of former menu item). This value overrides any destination specified by the query string.

ADMIN_ForwardMenuGroupId optional int

Deprecated. Use ADMIN_ForwardMenuGroup instead.

Id of the menu group to show after execution (instead of former menu item). This value overrides any destination specified by the query string.

ADMIN_ForwardMenuItem optional string
Alias of the menu item to execute after execution (instead of former menu item). This value overrides any destination specified by the query string.
ADMIN_ForwardMenuItemId optional int

Deprecated. Use ADMIN_ForwardMenuItem instead.

Id of the menu item to execute after execution (instead of former menu item). This value overrides any destination specified by the query string.

ADMIN_Message optional string

Displays a user friendly error message to the user.

ADMIN_PasteHtmlFromPopup optional string
Pastes HTML into an HTML editor. See ADMIN_SetFieldValueFromPopup.
ADMIN_SetFieldValueFromPopup optional string
Sets the value of the field specified in the menuitempopup call. Only select this column if menu item is opened in a popup.
ADMIN_ClosePopup optional bit
If this column is anything but NULL the popup will be closed. Only select this column if the menu item is opened in a popup.
Default: The default behavior is to step back inside the popup window and close it if there is nothing to step back to.
ADMIN_ClosePopupAndReloadOpener optional bit

If this column is anything but NULL the popup will be closed and the parent will be reloaded. Only select this column if the menu item is opened in a popup. Avoid using this feature if the opener is a newEdit as that may interrupt the user's ongoing input.

ADMIN_ClearHistory optional any
When the value is not NULL all navigation history is cleared and the user can't navigate back. This is only supported when forwarding to another menu item.
ADMIN_RefreshMenu optional bit
Will trigger a reload of the sidebar if the column is anything but NULL.
Cache optional string
Cache key to be cleared. Supports wildcards.
CacheUserId optional string
Either a user id or '%'.

Clears all caches (e.g. access permissions) related to the specified user id.

Use '%' to clear caches for all users.
CancelButtonText optional string

Changes the text of the Cancel button when used with ADMIN_Force, ADMIN_ConfirmWarning, ADMIN_ConfirmQuestion, ADMIN_ConfirmDelete.

OkButtonText optional string

Changes the text of the OK button when used with ADMIN_ErrorMessage, ADMIN_ConfirmWarning, ADMIN_ConfirmQuestion, ADMIN_ConfirmDelete, ADMIN_InfoMessage, ADMIN_SuccessMessage, ADMIN_Message, ADMIN_Force, or ADMIN_Forward. ADMIN_Force,

SQL Call: Validate parameters

Allows you to validate the SQL parameters before any other SQL is run in the component. This call is only made if the SQL is a stored procedure and Validate parameters is checked.

May modify database: No

Parameters

@Force bit
Set to 1 if the last call to validate parameters used admin_force and the user clicked OK in the OK/Cancel dialog.
@ValidateParams bit
Set to 1 when this call is made.

Resultset: Messages (optional)

Table count: repeated zero or one time
Row count: zero or one row
Columns
ADMIN_Force optional string
Message asking the end user to confirm their parameters.
ADMIN_Message optional string
Message explaining why the parameters are rejected.

Custom access control and logging

SQL Call: Custom access control and logging

Use this call to restrict which entries a user is allowed to view and edit, and to log which entries a user views.

Access to a menu item is normally controlled through functions and roles alone but some entities need more fine grained control. For example, a user may have access to the View Member menu item for normal members but not for members with a protected identity.

The menu items a user visits are always logged (in ADMINLogMenuItem) but for sensitive data you may need to log exactly what entries are viewed. Do the logging in this call as the common ways of viewing data (grid and InfoSQL) are not allowed to modify the database.

If you bind a scalar function instead of a stored procedure to this call then its name must end with '_GrantAccess'.

May modify database: Yes

Resultset: Access permissions

Return whether the user is allowed to visit the menu item with the current parameters.
Table count: repeated exactly once
Row count: exactly one row
Columns
GrantAccess mandatory bit
1 if the user is allowed to view the menu item, 0 if the user should not be allowed to view the menu item.

If 0 then an error will be logged as the user should not have been able to reach the menu item with the given parameters in the first place.

Examples

Importing a document in cells mode

This example imports an Excel document where Date is the first column, the remaining columns represent branch offices, and the cells contain daily profit. In this case the columns are not known in advance.

Date Southwater Chicago Southwater Stockholm ...
2021-11-23 216132 415612 ...
2021-11-24 216535 415661 ...
2021-11-25 265139 412491 ...
... ... ... ...
CREATE OR ALTER PROCEDURE Example.ExcelImport
	@Action varchar(50),

	@ErrorMessage varchar(max) = NULL,
	@WorkSheetName nvarchar(300) = NULL,
	@ImportDocumentId int = NULL
AS
BEGIN
	IF @Action = 'Init'
	BEGIN
		SELECT TOP (100)
			ImportDocumentId,
			'Cells' AS ImportMode
		FROM
			Example.ImportDocument
		WHERE
			IsImported = 0
		ORDER BY
			ImportDocumentId; -- Prevent starvation by processing oldest document first.

		RETURN;
	END;

	IF @Action = 'BeginDocument'
	BEGIN
		SELECT
			Document AS DocumentData
		FROM
			Example.ImportDocument
		WHERE
			ImportDocumentId = @ImportDocumentId;

		RETURN;
	END;

	IF @Action = 'BeginWorksheet'
	BEGIN
		-- Example uses a document with a single worksheet and so needs no special processing.
		RETURN;
	END;

	IF @Action = 'AppendRows'
	BEGIN
		CREATE NONCLUSTERED INDEX IX_ExampleExcelImport_RowNumber ON #Cells (RowNumber);

		-- Import headers from the first row in the document.
		IF EXISTS (SELECT * FROM #Cells WHERE RowNumber = 1)
		BEGIN
			IF NOT EXISTS (SELECT * FROM #Cells WHERE RowNumber = 1 AND ColumnNumber = 1 AND CellValue = 'Date')
			BEGIN
				RAISERROR('The title of the first column should be "Date".', 16, 1);
				RETURN -1;
			END;

			-- Save the branch names.
			INSERT Example.ImportDocumentColumns
			(
				ImportDocumentId,
				ColumnNumber,
				BranchName
			)
			SELECT
				@ImportDocumentId,
				ColumnNumber,
				CellValue
			FROM
				#Cells
			WHERE
				RowNumber = 1 AND
				ColumnNumber > 1 AND
				CellValue IS NOT NULL;
		END;

		INSERT Example.ImportDocumentCells
		(
			ImportDocumentId,
			ReportDate,
			ColumnNumber,
			Profit
		)
		SELECT
			@ImportDocumentId,
			DateCol.CellValue,
			BranchCol.ColumnNumber,
			BranchCol.CellValue
		FROM
			#Cells DateCol
			JOIN #Cells BranchCol ON BranchCol.RowNumber = DateCol.RowNumber
		WHERE
			DateCol.RowNumber > 1 AND -- Ignore the header row.
			DateCol.ColumnNumber = 1 AND
			BranchCol.ColumnNumber > 1;

		RETURN;
	END;

	IF @Action = 'EndWorksheet'
	BEGIN
		-- Example uses a document with a single worksheet and so needs no special processing.
		RETURN;
	END;

	IF @Action = 'EndDocument'
	BEGIN
		UPDATE Example.ImportDocument SET
			ErrorMessage = NULL,
			IsImported   = 1
		WHERE
			ImportDocumentId = @ImportDocumentId;
		
		-- Import data from staging table into system here.

		RETURN;
	END;

	IF @Action = 'StoreError'
	BEGIN
		UPDATE Example.ImportDocument SET
			ErrorMessage = @ErrorMessage,
			IsImported   = 1
		WHERE
			ImportDocumentId = @ImportDocumentId;
		
		RETURN;
	END;

	IF @Action = 'Finished'
	BEGIN
		RETURN;
	END;

	RAISERROR('Unsupported action "%s.', 16, 1, @Action);
END;