Import a spreadsheet into APPX

Export the sheet to a delimited text file before uploading it. The example reads rows of text. It cannot read Excel workbook files directly.

On this page

APPX 5.1 and later equivalents

The example below uses older routines. When maintaining it on APPX 5.1 or later, use the following replacements.

GET CONFIGURATION INFO
.ENV GET APPXPATH
GET FILE FROM CLIENT
.CLIENT UPLOAD FILE
STREAM OPEN
.STREAM OPEN
STREAM READ
.STREAM READ
STREAM CLOSE
.STREAM CLOSE
Parsing routines
.TEXT PARSE LINE

The DMO application has a more recent example of importing data, see the Input IMPORT PROSPECTS.

Export the Data

In Excel or another spreadsheet program, choose Save As or Export and select tab-delimited text. Check the header row, empty cells and embedded line breaks. A tab within a cell splits that cell into two fields.

Upload the File to the Server

Use a file-chooser field in the INPUT process to select the file on the desktop. Choose a writable import folder on the server. The older example below uses the tmp folder under APPXPATH.

In the -Start of Process event point, include this:

      SUBR     --- GET CONFIGURATION INFO           SUBPROCESS   END? N  FAIL 0

The screen includes a file-chooser field, which we will call WORK DESKTOP FILE NAME. We are going to use configuration info to build a temporary storage path for the file on the server, with a date time stamp to ensure that we don't overwrite an existing file, in a field called WORK SERVER FILE NAME. When the user enters a path into the field on the screen and hits the proper option key, the following code is triggered in Option Intercept:

      SET      --- CONFIG TYPE                =      APPXPATH
      READ     --- CONFIG                 HOLD 0 FT 0 BY CONFIG TYPE
F     CANCEL   Unexpected Error Reading CONFIG
      SET      XXX WORK SERVER FILE NAME      =  --- CONFIG DATA
      APPEND   XXX WORK SERVER FILE NAME      0      /tmp/TransferFile-
      SET DATE XXX WORK DATE TIME STAMP
      SET      --- TEMP 32                    =  XXX WORK DATE TIME STAMP
      APPEND   XXX WORK SERVER FILE NAME      0  --- TEMP 32
      *
      PASS     XXX WORK DESKTOP FILE NAME     FIELD            SHARE? Y
      PASS     XXX WORK SERVER FILE NAME      FIELD            SHARE? N
      PASS     --- AI                         FIELD            SHARE? Y
      *
      SUBR     --- GET FILE FROM CLIENT             DETACHED     END? N  FAIL 0
      *
      IF       --- AI                         EQ     1
F     ERROR    Could not upload the file.

Note: The APPEND statement that includes /tmp/TransferFile- is formatted for a Linux server. Windows servers will require a backslash separator and probably a different target folder.

Barring any unexpected situations, we now have the tab-delimited file on the server, located at WORK SERVER FILE NAME.

Parse the Uploaded File for the Desired Data

For quoted CSV containing commas inside a field, use a CSV reader. .TEXT PARSE LINE splits delimiters inside quoted text.

The next step is to use the STREAM commands to read each row in the uploaded file. We know that the first row contains column headings, so we will skip past that row. If you prefer, though, and you want to get elegant, you can import them into a separate file, and use them to decide which data goes into which field. But for our example, we'll assume that the columns are preset to always have the right data in the right place.

By the way, you want to be careful about your column headings - make sure they don't have any line feeds in them, or it can throw the whole thing off. You can see that if you look at the tab-delimited file on your desktop via WordPad or NotePad. With Word Wrap off, if the second line doesn't start with data, check and edit those column headings.

So, first we have to define a couple of fields and open the file for reading:

      SET      XXX WORK FIRST TIME THRU       =      Y
      CNV BIN  XXX WORK TAB CHARACTER         =      9
      SET      --- STREAM OPEN TYPE           =      r
      SET      --- STREAM FILE PATHNAME       =  XXX WORK SERVER FILE NAME
      GOSUB    --- STREAM OPEN
      IF       --- STREAM RETURN CODE         NE     0
T     ERROR    Something went wrong, don't continue
      *
      LABEL    READ STREAM FILE
      SET      --- STREAM READ LENGTH         =
      GOSUB    --- STREAM READ
      *
      *        Return Code will be 0 if some data was returned.
      *        Return Code will be 1 if EOF reached or other error occurs.
      *        --- STREAM BUFFER contains the data that was read
      *        --- STREAM READ LENGTH = number of bytes actually returned
      *
      IF       --- STREAM RETURN CODE         EQ     0
T     IF       XXX WORK FIRST TIME THRU       EQ     Y
TT    SET      XXX WORK FIRST TIME THRU       =      N
TF    GOSUB    XXX FILE PARSE
T     GOTO     READ STREAM FILE
      *
      GOSUB    --- STREAM CLOSE

Each time we found a line of data, we called a subroutine called XXX FILE PARSE. Here's what that subroutine might contain:

      SET      XXX WORK TAB POSITION      001 =      0
      RESTORE  XXX DATAFILE                   DEFAULT RECORD
      *
      GOSUB    XXX FILE PARSE SUB GET NEXT FIELD
      SET      XXX DATAFILE LAST NAME         =  --- TEMP 32K
      GOSUB    XXX FILE PARSE SUB GET NEXT FIELD
      SET      XXX DATAFILE FIRST NAME        =  --- TEMP 32K
      GOSUB    XXX FILE PARSE SUB GET NEXT FIELD
      SET      XXX DATAFILE EMAIL ADDRESS     =  --- TEMP 32K
      GOSUB    XXX FILE PARSE SUB GET NEXT FIELD
      *        Keep setting fields like that as needed
      WRITE    XXX DATAFILE               FAIL 0

The last piece of the puzzle is the FILE PARSE SUB GET NEXT FIELD subroutine. And that looks like this:
      IF       --- STREAM BUFFER              IN XXX WORK TAB CHARACTER
T     SET      XXX WORK TAB POSITION      002 =  --- TEXT AT POSITION
F     SET      XXX WORK TAB POSITION      002 =  --- STREAM READ LENGTH
F     COMPUTE  XXX WORK TAB POSITION      002 +      1
      *
      *        --- LI is field length
      SET      --- LI                         =  XXX WORK TAB POSITION      002
      COMPUTE  --- LI                         -  XXX WORK TAB POSITION      001
      COMPUTE  --- LI                         -      1
      *
      *        --- PI is location of first importable character
      SET      --- PI                         =  XXX WORK TAB POSITION      001
      COMPUTE  --- PI                         +      1
      SET      --- TEMP 32K                   =
      SET TEMP 32K   AT 001 FOR LI  FROM PI  OF  --- STREAM BUFFER
      *
      *        Replace second tab with blank, find next tab
      *
      SET      --- TEMP 1                     =
      SET      --- PI                         =  XXX WORK TAB POSITION      002
      SET TEMP 1     AT 001 FOR 001 INTO PI  OF  --- STREAM BUFFER
      SET      XXX WORK TAB POSITION      001 =  XXX WORK TAB POSITION      002
      *

And that's it. You may need to do some tweaking in the XXX FILE PARSE subroutine to handle numeric fields, date fields, and so on.

Please note that APPX 5.1 has an excellent new subroutine called .TEXT PARSE LINE, which can be called instead of the FILE PARSE SUB GET NEXT FIELD subroutine above. I highly recommend using that if you're on APPX 5.1 or later.

-- AlKalter - 2012-06-19

Topic revision: r7 - 03 Dec 2026, AdminUser - This page was cached on 03 Dec 2026 - 19:41.

Powered by Foswiki. Wiki content belongs to its contributing authors.