Jump to content

EXCEL

From Commander4j
Revision as of 21:47, 9 November 2026 by Dgarratt (talk | contribs) (Updated by push_wiki.py)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Middleware4j can read and write Excel spreadsheets. Unlike the other connectors, the type name is different for each direction:

Direction Type in config.xml File formats
Input XLS .xls and .xlsx
Output EXCEL .xlsx

Reading a spreadsheet

<input>
   <id>in1</id>
   <description>Read Excel File</description>
   <type>XLS</type>
   <url>
      <path>./interface/input/Stock Count</path>
      <mask/>
      <prefix/>
      <pollingInterval>1000</pollingInterval>
   </url>
   <xsl>
      <XSLTPath/>
      <XSLT>EXCEL_TO_CSV.xsl</XSLT>
   </xsl>
</input>

There are no settings particular to this connector. The first sheet of the workbook is read and converted to rows and columns, the same layout as the CSV and ASCII input connectors produce.

<data type="XLS" filename="Stock Count.xlsx" rows="2" cols="3">
   <row id="1">
      <col id="1">Material</col>
      <col id="2">Quantity</col>
      <col id="3">Counted</col>
   </row>
   <row id="2">
      <col id="1">43170672</col>
      <col id="2">120.0</col>
      <col id="3">2026-08-31</col>
   </row>
</data>
Cell contains Value in the XML
Text The text.
A number The number with a decimal point, so 120 becomes 120.0. Use format-number() or floor() in the stylesheet if a whole number is needed.
A date The date as yyyy-MM-dd. Any time of day is dropped.
A formula The result of the formula.
True or false true or false.
Nothing The <col> element is left out.

Because empty cells are left out, select columns by their id, for example col[@id='3'], and not by position.

Two things to check in the spreadsheet itself:

  • The number of columns read is taken from the row with the most filled cells. Give the sheet a heading row, or at least one row, in which every column is filled in, otherwise columns on the right can be missed.
  • A cell that is empty but has formatting applied, such as a border or a fill colour, stops the file from being read. Clear the formatting from empty cells, or put a value in them.

A heading row in the spreadsheet arrives as row 1. To skip it, process row[@id > 1].

Writing a spreadsheet

<output>
   <id>out1</id>
   <enabled>true</enabled>
   <description>Write Excel File</description>
   <type>EXCEL</type>
   <url>
      <path>./interface/output/Despatch</path>
      <prefix/>
      <fileExtension>xlsx</fileExtension>
      <use83GUID>false</use83GUID>
      <retainOriginalFilename>false</retainOriginalFilename>
   </url>
</output>

There are no settings particular to this connector either. The content of the workbook is described entirely by the XML that the stylesheet produces.

<excel sheets="1" filename="Despatch_12345">
   <sheet id="1" name="Sheet 1" cols="3" rows="2">
      <header>
         <col id="1" type="string">SSCC</col>
         <col id="2" type="double">Quantity</col>
         <col id="3" type="isodate">Production Date</col>
      </header>
      <row id="1">
         <col id="1">000000000000000123</col>
         <col id="2">5616</col>
         <col id="3">2016-08-01T14:58:03</col>
      </row>
      <row id="2">
         <col id="1">000000000000000124</col>
         <col id="2">5616</col>
         <col id="3">2016-08-01T15:10:41</col>
      </row>
   </sheet>
</excel>
Element or attribute Description
excel / @sheets Number of sheets in the workbook. Required.
excel / @filename Optional. Name for the output file, without the extension. When given it replaces the name that would otherwise be built from the input file name.
sheet / @id Sheet number, starting at 1.
sheet / @name The name shown on the sheet tab.
sheet / @cols Number of columns. Required.
sheet / @rows Number of data rows, not counting the heading. Required.
header / col The heading for each column. The type attribute sets how the values in that column are written.
row / col The values. Rows and columns are found by their id attributes, numbered from 1.

Column types:

type Written as The value must be
string Text, left aligned. Use this for codes such as an SSCC that must keep leading zeros. Anything.
double A number, right aligned. A number in every row. An empty value causes the file to fail.
isodate A date and time. In the form 2016-08-01T14:58:03.

The heading row is written in bold on a shaded background, all cells have a border, and the column widths are adjusted to fit their content.

The sample stylesheet C4J_DESPATCH_PREADVICE_XML_to_Excel.xsl in samples/advanced/xslt builds a spreadsheet of pallets from a Commander4j despatch pre advice message.

See also: Connectors, Maps, Creating a Map