1.2 Excel file
The structured Excel table is relatively regular, and SPL reads and writes it with the xlsimport/xlsexport function.
For example: the business meanings of every column of the first sheet in ordersNT.xlsx sequentially are: order ID, customer NO, sales ID, order amount and order date. Part of the data is as follows:
The following is the partial result after the table is processed in a way that first sort the orders in ascending order alphabetically by customer NO, and then sort the orders with same customer NO in descending order by order amount, and finally save it to a new Excel in the original format:
A1, A3: read and write the first sheet of Excel. If you want to read the specified sheet, you can use:
xlsimport(;Sheet number or Sheet name)
If you want to write to the specified sheet, you can use:
xlsexport(A2; Sheet number or Sheet name)
SPL can also process the structured table with column name (title) in a similar way to process text files. For example, partial data of orders.xlsx is as follows:
Sort this file and write the result to a new file together with column name:
Sometimes the first few rows of the table are useless and need to be skipped, for example:
|1||orders from 2009 to 2010|
Reading from row 4:
Sometimes we need to append new data with the same structure to the original table. In this case, we can use the option @a:
If the appearance attribute is set for the last non-blank row of the original table, the appended data will inherit the style of this row. For example, the display format of column D of original table is #,##0.00, and the style of column E is mmm-dd-yyyy, as shown in the table below:
After appending the data, the result is as follows:
The style attribute has been set for the first blank row after the last non-blank row of original table, the appended data will inherit the style attribute of this row. Using this feature, we can achieve the data output from scratch in the specified format. For example, first create a blank Excel, and set the display format of column D in row 2 to #,##0.00, and that of column E to mmm-dd-yyyy.
Then append data to the blank table, and the result is as follows:
Some Excel tables are not regular in format and do not have clear column attributes. In this case, they can be processed as a two-dimensional string sequence.
For example, the following key-value data:
Now, we want to split the above data into 2 columns by key and value, and sort them by key and value, and finally write them to a new Excel. The result is as follows:
A1: read in the Excel, @w means reading as a two-dimensional string sequence. The whole is a large sequence, and each row is not only a member of the large sequence, but also a small sequence; each cell in the row is a member of the small sequence.
A2: Concatenate the 2D sequence into 1D sequence and remove possible blank cells such as A1, B1, E2.
A3: Split the string sequence into key and value.
A4: Sort by key and value.
A5: Write the result to a new Excel, @w means writing a sequence of sequences.
Sometimes we need to read and write the Excel cells accurately.
For example: the table below has the editor and edit date in row 1.
Now we want to copy the editor and edit date to the corresponding position on row 7, the result is as follows:
A2: Open the Excel file as an object.
A3: Read the cell A1 and assign the variable str. By default, it is to read from the first sheet. If you want to read the cell A1 in the specified sheet, you can use:
A2.xlscell("A1",Sheet number or Sheet name)
B3: Write contents of cell A1 to cell A7. Similarly, if you want to write to cell A7 of the specified sheet, you can use:
A2.xlscell("A7",Sheet number or Sheet name;str)
A4-B4: Read the contents of cell E1, and write to cell E7.
A5: Write Excel object to Excel file.
In the above example, A1-E1 that need to be read are consecutive cells, and A7-E7 that need to be written are also consecutive cells. For the reading and writing of such consecutive cells, SPL can implement with a more simplified code:
A3: Read consecutive cells in sequence format
A4: Write the sequence to consecutive cells, and each member of the sequence corresponds to one cell. You can either use a sequence to write data to consecutive cells or use a string separated by TAB (\t) or Enter (\r), where TAB means horizontal (column) separation and Enter means vertical (row) separation.
Using Excel objects, not only can read and write cells but also process multiple sheets.
A certain Excel uses multiple sheets to store the order tables. Each sheet has the same format, but the number and name are not the same. Now we want to sort these orders into a new Excel to make each sheet store one year’s data.
A1: Open the source Excel file as an object.
A2: Traverse each sheet, read the order of each sheet, and concatenate all the orders. A1.(stname) means taking out all sheet names from the Excel object A1.
A3: Group the orders by year.
A4: Open the target Excel file as an object. @w means write mode, and a new file will be created if the file does not exist.
A5: Traverse each group (yearly) orders of A3 and write them to the new sheet of A4 in turn.
A6: The Excel object opened in @w mode must be closed with the function xlsclose.