Combine Same-structure Worksheets into One
Problem description & analysis
There are multiple Excel files (worksheets) of same structure in an xlsx directory. Each worksheet has multiple sheets, as shown below:
book1.xlsx—sheet1:
id |
name |
1 |
Jack |
2 |
Tom |
3 |
Jerry |
book1.xlsx—sheet2:
id1 |
name1 |
10 |
Frank |
11 |
Lucy |
12 |
Alice |
book2.xlsx—sheet1:
id |
name |
4 |
James |
5 |
Danny |
6 |
Lion |
book2.xlsx—sheet2:
id1 |
name1 |
13 |
Terry |
14 |
May |
15 |
test |
We are trying to combine these worksheets into one, as shown below:
result.xlsx—sheet1
id |
name |
1 |
Jack |
2 |
Tom |
3 |
Jerry |
4 |
James |
5 |
Danny |
6 |
Lion |
result.xlsx—sheet2
id1 |
name1 |
10 |
Frank |
11 |
Lucy |
12 |
Alice |
13 |
Terry |
14 |
May |
15 |
test |
Solution
Write the following script p1.dfx in esProc:
A |
B |
C |
D |
|
1 |
=directory@p("xlsx/") |
|||
2 |
for A1 |
=file(A2).xlsopen() |
||
3 |
for B2 |
=file(A2).xlsimport@t(;B3.stname) |
||
4 |
if #A2==1 |
=file("result.xlsx").xlsexport@at(C3;B3.stname) |
||
5 |
else |
=file("result.xlsx").xlsexport@a(C3;B3.stname) |
Explanation:
A1 Read names of all files in xlsx path.
A2 Loop through A1.
B2 Return the three attributes of sheet name (stname), number of rows (nrows), and number of columns (ncols) of the worksheet corresponding to the current A2’s file name.
B3 Loop through B2.
C3 Retrieve the content of sheet in B3.stname corresponding to A2’s file name.
C4, D4 If A2’s loop number is 1, output C3’s content to the sheet of B3.stname in result.xlsx. @t option enables outputting column headers, and @a option enables an append type output.
C5, D5 If A2’s loop number isn’t 1, output C3’s content to the sheet of B3.stname in result.xlsx. @a option enables an append type output.
Read How to Call an SPL Script in Java to learn about the method of integrating a SPL script with a Java program.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_Desktop/
SPL Learning Material 👉 https://c.scudata.com
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProcDesktop
Linkedin Group 👉 https://www.linkedin.com/groups/14419406/