The Excel package contains a high level function to simplify using Excel as a database export utility for discrete sizes.

1 Using dbexport

The dbexport function in Ax.ms.Excel library provides a built in database export mechanism. It allows to create a sheet from a database table using an optional filter condtion (all rows if not specified).

The generated sheet takes as name the table name and includes a header with column names and data validation rules according column data types.

Return Method
Sheet createSheet(JSConnection conn, Consumer<DBExportOptions> consumer)
Sheet createSheet(JSConnection conn, String tabname)
Sheet createSheet(JSConnection conn, String sheetname, String sql)
Sheet createSheet(JSConnection conn, String tabname, String colnames, String where, String orderBy)
createSheet will create the sheet with the tableName if it does not exists. It will append data to it if it already exists.

2 Naming cells

During dbexport, the sheet for data will have a heading column with named cells. Named cells and have the form tableName.columnName and they should be unique across the workbook.

Notice that prefixing the name of cell with table of sheet is required to generate uniquess for columns across multiple tables. Excel will no admit duplicated names in a workdbook.

3 Data types

Data types are set on Excel according database column type including data validation constrataints.

Source data type Constraint
String Data length constraint
Short Integer constraint
Integer Integer constraint
Long Integer constraint
Float Decimal constraint
Double Decimal constraint
BigDecimal No constraint as it's stored as String to keep BigDecimal precision. Style is right aligned in concordance with numbers. Even it is stored as string it will be properly converted to BigDecimal when using Excel dbimport
Date Date constraint
Datetime Date constraint

4 Export examples

Let's see how to export a simple Informix database named stores_demo. You can create the stores database by executing dbaccessdemo7 on the Informix database server. To run the samples, you must be connected to stores_demo database.

4.1 Export a table

The following example shows how to use an export a single table

Copy
<script>
    
    // 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, "stores7.xlsx");
    
    var dbexport = wb.dbexport();
    
    var table = "state";
    var sheet = dbexport.createSheet(Ax.db, table);
    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));
    sheet.setAutoSizeColumns();

    return wb.toBlob();
</script>

4.2 Export a set of tables

The following example shows how to use an export a set of tables

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, "stores7.xlsx");
    
    var dbexport = wb.dbexport();
    for (table of tables) {
        var sheet = dbexport.createSheet(Ax.db, table);
        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 wb.toBlob();
</script>

The operation will return an Excel workbook with a sheet for every table with it's data.

4.3 Export using a configurator

If nothing specified all table columns and all table rows are exported. But you can configure many options using an Export configurator consumer.

Copy
<script>
    
    // Wire db logger to console
    Ax.db.setLogger(console.getLogger());
    
    // Create XLSX workbook using Streaming API
    var wb = new Ax.ms.Excel(Ax.ms.Excel.Type.XSSF, "stores7.xlsx");
    
    var dbexport = wb.dbexport();
    
    var sheet = dbexport.createSheet(Ax.db, config => {
        config.setTableName("customer");
        config.setWhere("state ='CA'");
        config.setColumnNames("*");
        config.setOrderBy("1");
        // To change the tableStyle (set to null to avoid a creating a table for data)
        // Default table style is TableStyleMedium2
        config.setTableStyle("TableStyleMedium1");
    });
    var table = sheet.getName();
    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));
    sheet.setAutoSizeColumns();
    
    return wb.toBlob();

</script>

5 Export an SQL statement

You can also export an SQL statement on stores_demo (informix) database. In that case, the name of the sheet will

Copy
<script>
    
    // 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, "stores7.xlsx");

    // Create a dbexport object
    var dbexport = wb.dbexport();

var sql = `
select c.customer_num, c.lname, c.company, c.phone, u.call_dtime, u.call_descr
  from customer c, cust_calls u
       where c.customer_num = u.customer_num
       order by 1
`;
    var sheet = dbexport.createSheet(Ax.db, "sample1", sql);
    var rows = sheet.getLastRowNum();
    var cols = sheet.getLastColNum();
    console.log(Ax.text.String.format("Sheet has %d row(s) x %d cols", rows, cols));
    // Warning: If large number of rows and cols do not auto resize columns as it's time consuming method.
    sheet.setAutoSizeColumns();
    
    return wb.toBlob();
</script>