Exports or imports Microsoft Excel data.
Not intended for exporting large amounts of data.
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.
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.
Repeated worksheets can not use any advanced features, including but not limited to charts, formulas and named cells.
Retrieves the IDs of the documents to import. A Begin Document call will be made for each document.
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 to retrieve the excel document data to be imported.
Will be called for once for each worksheet in the document.
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.
Called when a worksheet has finished processing.
Called when a document has finished processing.
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.
Will be called once every document has finished processing.
Displays a user friendly error message to the user. This blocks any forwarding for the user.
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'.
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'.
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'.
Displays a user friendly info message to the user. When the user clicks OK the user is forwarded.
Displays a user friendly success message to the user. When the user clicks OK the user is forwarded.
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.
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.
Additional information to show the developer when using ADMIN_Dialog.
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.
Alias of the menu group to show after execution (instead of former menu item). This value overrides any destination specified by the query string.
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.
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.
Displays a user friendly error message to the user.
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.
Changes the text of the Cancel button when used with ADMIN_Force, ADMIN_ConfirmWarning, ADMIN_ConfirmQuestion, ADMIN_ConfirmDelete.
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,
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.
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'.
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;