The Excel package contains a high level function to simplify using Excel as a database import utility.

1 Using dbimport

You can use a generated Excel workbook as a data set to perform a database import. The database import uses the workbook as data source.

  • For each sheet name is taken as table name (if no table matching sheet name exists, it's excluded).
  • On a sheet (table), the first row is taken as table column names.
  • Column names are mached with table columns (non matching are excluded).
  • For each row starting after header, an update or insert is performed on target table.

The process returns a new workbook inlcuding a table of contens (TOC) with a summary of all operations.

2 dbimport example

To follow this dbimport example you'll require to download two files:

Stores7.sql is a sample schema for informix database.

2.1 Create stores7 tables

Create a new database or connect to an existing database and create tables using downloaded SQL file:

2.2 Run a dbimport script

Download Excel file containing data to import into created tables in a path well known. Then, change Excel file path in JS Script importer and execute it.

Copy
<script>
    var wb = Ax.ms.Excel.load("file:/tmp/stores7.xlsx");

    // Use the wb as a data source to import on given database
    // Return a new wb with results of dbimport process
    return wb.dbimport().load(Ax.db).toBlob();
</script>

2.3 Inspecting results

The operation will return a new Excel with the results of the operations as a sheet containing a summary of operations on each database table.

3 Error results

Excel dbimport method automatically traps errors produced in row loading. In Excel returned, you'll get a status excell showing number of errors produced, and a tab for each table with errors.

Each row not integrated into the database are returned with all columns and five additional columns at the end:

  1. Row: Identifies original row number of data
  2. Statement: Identifiesif error has been produced in insert or update of data
  3. SQLCode: SQL Error Code
  4. SQLState: SQL ISAM Error Code
  5. SQLCode: Message describing the error motive

4 Fixing errors

A nice features is that you can use the Excel resutls worksheet to fix errors and re-submit it again as a input for loading process. Extra columns with error information will not affect the loading process.

5 Generating a dbimport template

You can generate a dbimport template to be filled by users by using dbexport utility methods. For example, to generate an empty dbimport template for previous example:

Copy
<script>
    // Tables to export to excel
    var tables = [
        "customer",
        "cust_calls",
        "catalog",
        "call_type",
        "items",
        "manufact",
        "orders",
        "state",
        "stock",
    ];
    
    // Wire db logger to console
    Ax.db.setLogger(console.getLogger());
    
    // Create a new XLSX workbook
    var wb = new Ax.ms.Excel(Ax.ms.Excel.Type.XSSF, "stores7Sample");
    
    var dbexport = wb.dbexport();
    for (table of tables) {
        // Fill sheet with all columns (*) from table and where condition matching no rows (1=0)
        var sheet = dbexport.createSheet(Ax.db, table, "*", "1=0", "1");
        var rows = sheet.getLastRowNum();
    	var cols = sheet.getLastColNum();
      	console.log(Ax.text.String.format("Table %s has %d row(s) x %d cols", table, rows, cols));
        // Warning: If large number of rows and cols do not auto resize columns as it's time consuming method.
        sheet.setAutoSizeColumns();
    }
    
    // Return the blob 
    // can be saved to file: 
    // new Ax.io.File("/tmp/stores7.xlsx").write(wb.toBlob());
    return wb.toBlob();
</script>