Excel File

Reading an xls file stored in the specified path

Read up to 10000 rows x 1000 columns from the xls file stored in the specified path and get back the content of a specific folder

Syntax

var list = utils.getXlsContent(String dirId,String fileName,int sheetIndex,int fromRow,String... attributeNames);

Details

 dirId: path identifier
  fileName name of the xls file
 sheetIndex sheet index inside the spreadsheet, starting from 0
 fromRow the content will be read starting from the specified row index (the first row has index 0)
 attributeNames, list of attributes, assigned to each column, starting from leftmost column to the right
 return a list of js objects, where each object is expressed as a set of couples <attributename, related value>

Values stored in each js object can be accessed as: object.get("attributeName")

Export data from database in xlsx format

Starting from a SQL query and bind variables, generate an xlsx file.

It is possible to customize this file in a variety of different ways:

  • defining the sheet name

  • adding new sheets to an already existing xlsx (used as template)

  • adding a row at the beginning, reporting the column headers

  • customizing horizontal alignment, column width, background/foreground color for each cell (both for header and data rows), font (name, size, bold/italic/underline), data format (for dates and numbers)

  • breaking up rows in sections, according to the value of one column (grouping data) and adding an header row at the beginning of each section; optionally, a footer row can be included as well, where specifying Excel functions (es. SUM, COUNT, etc.)

Syntax

var exportedRows = utils.createXLSXFileFromSQLQuery(
        Long templateDirectoryId,
        String templateFileName,
        String sheetName,
        Long outDirectoryId,
        String outFileName,
        Map[] formatHeaderColumns,
        Map[] formatColumns, 
        Map grouping,
        Map additionalSettings,
        Long datastoreId,
        String sqlQuery,
        Object[] pars
);

Details

Structure of a javascript object contained in the list of objects composing the "formatHeaderColumns":

Structure of a javascript object contained in the list of objects composing the "formatColumns":

Structure of the javascript object for "grouping":

List of supported colors, in background/foreground colors:

AQUA , AUTOMATIC , BLACK , BLACK1 , BLUE , BLUE_GREY , BLUE1 , BRIGHT_GREEN , BRIGHT_GREEN1 , BROWN , CORAL , CORNFLOWER_BLUE , DARK_BLUE , DARK_GREEN , DARK_RED , DARK_TEAL , DARK_YELLOW , GOLD , GREEN , GREY_25_PERCENT , GREY_40_PERCENT , GREY_50_PERCENT , GREY_80_PERCENT , INDIGO , LAVENDER , LEMON_CHIFFON , LIGHT_BLUE , LIGHT_CORNFLOWER_BLUE , LIGHT_GREEN , LIGHT_ORANGE , LIGHT_TURQUOISE , LIGHT_TURQUOISE1 , LIGHT_YELLOW , LIME , MAROON , OLIVE_GREEN , ORANGE , ORCHID , PALE_BLUE , PINK , PINK1 , PLUM , RED , RED1 , ROSE , ROYAL_BLUE , SEA_GREEN , SKY_BLUE , TAN , TEAL , TURQUOISE , TURQUOISE1 , VIOLET , WHITE , WHITE1 , YELLOW , YELLOW1

For a preview of these colors, see the following link:

https://github.com/ClosedXML/ClosedXML/wiki/Excel-Indexed-Colors

Example

Grouping data with an header row for each section, containing a description spreading the whole row

var exportedRowsNumber = utils.createXLSXFileFromSQLQuery(
        null,null, // templateDirectoryId + templateFileName
        "My Sheet", // sheetName,
        9, // directory id for the destination xlsx file
        "myfile.xlsx", // outDirectoryId
        formatHeaderColumns: [
              { title: "Company", bold: true, backColor: "ORANGE" },
              { title: "User", bold: true, backColor: "ORANGE" },
              { title: "Desc", bold: true, backColor: "ORANGE" },
              { title: "Date", bold: true, backColor: "ORANGE" },
              { title: "Version", bold: true, backColor: "ORANGE" }
        ],
        formatColumns: [
              { width: 200 },
              { width: 300 },
              { width: 500 },
              { width: 300,format: "m/d/yy" },
              { width: 250 }
        ],
        grouping: {
              fieldName: "COMPANY_ID",
              showValue: false,
              headerRow: [
                    { title: "Company: <COMPANY_ID>", backColor: "ORANGE",colSpan: 5 },
                    { title: "", backColor: "ORANGE" },
                    { title: "", backColor: "ORANGE" },
                    { title: "", backColor: "ORANGE" },
                    { title: "", backColor: "ORANGE" }
              ]
        }, // grouping
        null, // additionalSettings
        null, // datastoreId
        "SELECT COMPANY_ID,USER_CODE_ID,DESCRIPTION,CREATE_DATE,ROW_VERSION FROM PRM01_USERS",
        [] // pars
);

Example

Grouping data with an header row for each section, containing a description spreading the whole row; at the end of each section there is also a footer row with the last cell containing the SUM of values for column "E"

var exportedRowsNumber = utils.createXLSXFileFromSQLQuery(
        null,null, // templateDirectoryId + templateFileName
        "My Sheet", // sheetName,
        9, // directory id for the destination xlsx file
        "myfile.xlsx", // outDirectoryId
        formatHeaderColumns: [
              { title: "Company", bold: true, backColor: "ORANGE" },
              { title: "User", bold: true, backColor: "ORANGE" },
              { title: "Desc", bold: true, backColor: "ORANGE" },
              { title: "Date", bold: true, backColor: "ORANGE" },
              { title: "Version", bold: true, backColor: "ORANGE" }
        ],
        formatColumns: [
              { width: 200 },
              { width: 300 },
              { width: 500 },
              { width: 300,format: "m/d/yy" },
              { width: 250 }
        ],
        grouping: {
              fieldName: "COMPANY_ID",
              showValue: false,
              headerRow: [
                    { title: "Company: <COMPANY_ID>", backColor: "ORANGE",colSpan: 5 },
                    { title: "", backColor: "ORANGE" },
                    { title: "", backColor: "ORANGE" },
                    { title: "", backColor: "ORANGE" },
                    { title: "", backColor: "ORANGE" }
              ],
              footerRow: [
                    { title: "", backColor: "ORANGE" },
                    { title: "", backColor: "ORANGE" },
                    { title: "", backColor: "ORANGE" },
                    { title: "Nr. of users", backColor: "ORANGE" },
                    { title: "=COUNT", backColor: "ORANGE" }
              ]
        }, // grouping
        null, // additionalSettings
        null, // datastoreId
        "SELECT COMPANY_ID,USER_CODE_ID,DESCRIPTION,CREATE_DATE,ROW_VERSION FROM PRM01_USERS",
        [] // pars
);

Reading a xls or xlsx file stored in the specified path

Read up to the specified number of rows, starting from the specified index (1..x) from the xls or xlsx file stored in the specified path and get back the content of a specific folder

dirId path identifier fileName name of the xls or xlsx file attributeNames, list of attributes, assigned to each column, starting from leftmost column to the right

Syntax

var list = utils.getExcelRowsBlock(Long dirId,String fileName,Long sheetIndex,
    Long fromRow,Long toRow,String... attributeNames);

startRow row index; if null it is the first row, i.e. 1

blockSize max number of rows to read, if available; if null it is set to 10000

attributeNames, list of attributes, assigned to each column, starting from leftmost column to the right

 return a list of js objects, where each object is expressed as a set of couples <attributename, related value>

A list of js objects, where each object is expressed as a set of couples <attributename, related value>; a 0 length list in case of no more rows available. It supports also nested objects and list of objects: it depends on the definition of the attribute lists. A few examples of attributes: [ "attr1", "subobject.attrsub1", "subobject.attrsub2", "sublist[0].subattr3","sublist[0].subattr4","sublist[1].subattr5" ]

Writing an image in a xls file stored in the server file system

Starting from an already existing Excel (xls), save one or more images in a series of cells, one for each image.

Syntax

utils.setImagesInXls(
      Long dirId,
      String srcFileName,
      String destFileName,
      String sheetName,
      List<Map> images
);

Details

Each javascript object contained in the "images" array must have the following content:

{ 
  x: 0, 
  y: 0, 
  w: 10, 
  h: 1000,  
  imagePath: "/opt/a.jpg" 
}

x is the column index (mandatory)

y is the row index (mandatory)

w is the image width (optional)

h is the image height (optional)

imagePath (mandatory) is the image absolute path (path+name) on the server file system.

Important note: do not save images too big or this would lead to a very large spreadsheet, difficult to save and later difficult to open by the end user.

Just to make some examples:

1. image having 80kb size with 1000x1000 resolution
   and excel having 1000 rows (images) -> final Excel size: 80MB size

2. image having 2kb size with 180x180 resolution
   and excel having 1000  rows  (images) -> final Excel size: 2MB size
   and excel having 10000  rows (images) -> final Excel size: 20MB size   
   and excel having 100000 rows (images) -> final Excel size: 200MB size
   
   1. image having 80kb size with 1000x1000 resolution
   and excel having 1000 rows (images) -> final Excel size: 80MB size

Therefore.. pay attention to the max number of rows allowed and the image size!

Last updated