Below are some practical using examples of the excel tags.
1 Ejemplo 1
Example
In this example it is showed how to generate an EXCEL document taking the data from the database demo_foodmart using a SELECT.
It also shows how to send this document by email.
Copy
<xsql-script> <body> <!-- Create the workbook --> <set name='wb'> <excel.Workbook /> </set> <!-- Create the workbook sheet with name Hoja_1 --> <set name="sheet"> <excel.Workbook.createSheet name="Hoja_1"> <wb/> </excel.Workbook.createSheet> </set> <!-- SELECT the first 30 rows of employee table and load them in a workbook --> <excel.Sheet.fromResultSet > <sheet/> <sql.toResultSet> <select first='30'> <columns> employee.employee_id Identidicador, employee.first_name Nombre, employee.last_name Apellido, employee.gender Genero, employee.education_level Estudios, employee.position_title Cargo, employee.department_id Departamento </columns> <from table='employee'> </from> <order>employee_id</order> </select> </sql.toResultSet> </excel.Sheet.fromResultSet> <!-- Select cell 1:0 to workbook sheet Hoja_1 --> <set name='my_cell'> <excel.Row.getCell col='0'> <excel.Sheet.getRow row='1'> <excel.Workbook.getSheet name='Hoja_1'> <wb /> </excel.Workbook.getSheet> </excel.Sheet.getRow> </excel.Row.getCell> </set> <!-- Check if the cell is null in case the SELECT does not return rows. --> <if> <expr><isnull><my_cell/></isnull></expr> <then> <exception>Cell at row 1 col 0 not found.</exception> </then> </if> <!-- Set color red for the cell at 1:0 --> <excel.Cell.setCellStyle css="font-color: red;"> <wb/> <my_cell /> </excel.Cell.setCellStyle> <!-- Creating a temporary EXCEL file with the workbook data --> <excel.Workbook.write> <wb/> <file name="test.xls" type="temp"/> </excel.Workbook.write> <println> <!-- Return the font color to cell indicated --> Font color row='1' col='0': <excel.Cell.getFontColor> <wb/> <my_cell /> </excel.Cell.getFontColor> <!-- Return the sheet Name to workbook index indicated --> Name to first workbook sheet: <excel.Workbook.getSheetName index='0'> <wb /> </excel.Workbook.getSheetName> </println> <!-- Prints the ResultSet --> <println> <!-- Load workbook into ResultSet --> <excel.Workbook.toResultSet start-header-row='1' start-column='A'> <wb/> </excel.Workbook.toResultSet> </println> <!-- Send e-mail with the excel.--> <mail smtphost='localhost' from='no-reply@deister.es' to='mymail@mydomain.com' subject='Excel test'> <content> Attached file </content> <attachs> <mail.attach type='file/xls'> <file type='temp' name='test.xls' /> </mail.attach> </attachs> </mail> </body> </xsql-script>
Font color row='1' col='0': #ff0000
Name to first workbook sheet: Hoja_1
+-------------+----------+---------+------+------------------+----------------------+------------+
|identificator|name |surname |gender|studies |position |departament |
+-------------+----------+---------+------+------------------+----------------------+------------+
| 1|Sheri |Nowmer |F |Graduate Degree |President | 1|
| 2|Derrick |Whelply |M |Graduate Degree |VP Country Manager | 1|
| 4|Michael |Spence |M |Graduate Degree |VP Country Manager | 1|
| 5|Maya |Gutierrez|F |Bachelors Degree |VP Country Manager | 1|
| 6|Roberta |Damstra |F |Bachelors Degree |VP Information Systems| 2|
| 7|Rebecca |Kanagaki |F |Bachelors Degree |VP Human Resources | 3|
| 8|Kim |Brunner |F |Bachelors Degree |Store Manager | 11|
| 9|Brenda |Blumberg |F |Graduate Degree |Store Manager | 11|
| 10|Darren |Stanz |M |Partial College |VP Finance | 5|
| 11|Jonathan |Murraiin |M |Graduate Degree |Store Manager | 11|
| 12|Jewel |Creek |F |Graduate Degree |Store Manager | 11|
| 13|Peggy |Medina |F |Bachelors Degree |Store Manager | 11|
| 14|Bryan |Rutledge |M |Bachelors Degree |Store Manager | 11|
| 15|Walter |Cavestany|M |Bachelors Degree |Store Manager | 11|
| 16|Peggy |Planck |F |Bachelors Degree |Store Manager | 11|
| 17|Brenda |Marshall |F |Partial College |Store Manager | 11|
| 18|Daniel |Wolter |M |Partial College |Store Manager | 11|
| 19|Dianne |Collins |F |Bachelors Degree |Store Manager | 11|
| 20|Beverly |Baker |F |Bachelors Degree |VP Country Manager | 1|
| 21|Pedro |Castillo |M |Bachelors Degree |VP Country Manager | 1|
| 22|Laurie |Borges |F |Bachelors Degree |VP Country Manager | 1|
| 23|Shauna |Wyro |F |Bachelors Degree |Store Manager | 11|
| 24|Jacqueline|Wyllie |F |Partial College |Store Manager | 11|
| 25|Lin |Conley |F |Graduate Degree |Store Manager | 11|
| 26|Jose |Bernard |M |High School Degree|Store Manager | 11|
| 27|Charles |Macaluso |M |Bachelors Degree |Store Manager | 11|
| 28|Lois |Wood |F |Partial College |Store Manager | 11|
| 29|Kevin |Armstrong|M |Graduate Degree |Store Manager | 11|
| 30|Cody |Goldey |M |Partial College |Store Manager | 11|
| 31|Paula |Nickell |F |Partial College |Store Manager | 11|
+-------------+----------+---------+------+------------------+----------------------+------------+
2 Ejemplo 2
Example
In this example it is showed how to generate an EXCEL document from a virtual table to which the data will be inserted with the function matrix.
Copy
<xsql-script> <body> <!-- Create virtual table with matrix data--> <vtable name='v_music'> <column name='artist' type='string' /> <column name='title_album' type='string' /> <column name='tracks' type='integer' /> <column name='genre' type='string' /> </vtable> <vtable.insert name='v_music'> <matrix> { "ACDC" , "TNT" , "9" , "Hard rock" }, { "Accept" , "Balls to the Wall" , "10", "Heavy metal" }, { "Aerosmith" , "Big Ones" , "16", "Hard rock" }, { "Alice in Chains" , "Facelift" , "12", "Grunge" }, { "Apocalyptica" , "Plays Metallica By Four Cellos" , "8" , "Cello rock" }, { "Black Label Society" , "Sonic Brew" , "15", "Hard rock" }, { "Black Sabbath" , "Black Sabbath" , "7" , "Heavy metal" }, { "Led Zeppelin" , "Led Zeppelin II" , "9" , "Hard rock" }, { "Queen" , "A Kind of Magic" , "9" , "Hard rock" }, { "Motörhead" , "Motorizer" , "11", "Heavy metal" }, </matrix> </vtable.insert> <!-- Create the workbook --> <set name='wb'> <excel.Workbook /> </set> <!-- Create the workbook sheet with name Hoja_1 --> <set name="sheet"> <excel.Workbook.createSheet name="Hoja_1"> <wb/> </excel.Workbook.createSheet> </set> <!-- Create a workbook sheet from ResultSet --> <excel.Sheet.fromResultSet > <sheet/> <!-- ResultSet to virtual table --> <vtable.toResultSet> <v_music /> </vtable.toResultSet> </excel.Sheet.fromResultSet> <!-- Select cell 1:0 to workbook sheet Hoja_1 --> <set name='my_cell'> <excel.Row.getCell col='0'> <excel.Sheet.getRow row='1'> <excel.Workbook.getSheet name='Hoja_1'> <wb /> </excel.Workbook.getSheet> </excel.Sheet.getRow> </excel.Row.getCell> </set> <!-- Check if the cell is null in case the SELECT does not return rows. --> <if> <expr><isnull><my_cell/></isnull></expr> <then> <exception>Cell at row 1 col 0 not found.</exception> </then> </if> <!-- Set color red for the cell at 1:0 --> <excel.Cell.setCellStyle css="font-color: red;"> <wb/> <my_cell /> </excel.Cell.setCellStyle> <!-- Creating a temporary EXCEL file with the workbook data --> <excel.Workbook.write> <wb/> <file name="test.xls" type="temp"/> </excel.Workbook.write> <println> <!-- Return the font color to cell indicated --> Font color row='1' col='0': <excel.Cell.getFontColor> <wb/> <my_cell /> </excel.Cell.getFontColor> <!-- Return the sheet Name to workbook index indicated --> Name to first workbook sheet: <excel.Workbook.getSheetName index='0'> <wb /> </excel.Workbook.getSheetName> </println> <!-- Prints the ResultSet --> <println> <!-- Load workbook into ResultSet --> <excel.Workbook.toResultSet start-header-row='1' start-column='A'> <wb/> </excel.Workbook.toResultSet> </println> <!-- Send e-mail with the excel.--> <mail smtphost='localhost' from='no-reply@deister.es' to='mymail@mydomain.com' subject='Excel test'> <content> Informe adjunto </content> <attachs> <mail.attach type='file/xls'> <file type='temp' name='test.xls' /> </mail.attach> </attachs> </mail> </body> </xsql-script>
Font color row='1' col='0': #ff0000
Name to first workbook sheet: Hoja_1
+-------------------+------------------------------+------+-----------+
|artist |title_album |tracks|genre |
+-------------------+------------------------------+------+-----------+
|ACDC |TNT | 9 |Hard rock |
|Accept |Balls to the Wall | 10 |Heavy metal|
|Aerosmith |Big Ones | 16 |Hard rock |
|Alice in Chains |Facelift | 12 |Grunge |
|Apocalyptica |Plays Metallica By Four Cellos| 8 |Cello rock |
|Black Label Society|Sonic Brew | 15 |Hard rock |
|Black Sabbath |Black Sabbath | 7 |Heavy metal|
|Led Zeppelin |Led Zeppelin II | 9 |Hard rock |
|Queen |A Kind of Magic | 9 |Hard rock |
|Motörhead |Motorizer | 11 |Heavy metal|
+-------------------+------------------------------+------+-----------+
3 Ejemplo 3
Example
In this example it is showed how to generate a EXCEL document using a virtual table.
Copy
<xsql-script> <body> <!-- Create empty virtual table --> <vtable name='v_random'> <column name='random_1' type='integer' /> <column name='random_2' type='integer' /> <column name='random_3' type='integer' /> </vtable> <!-- Insert data into vtable --> <for name='m_degrees' start='0' end='30' > <do> <!-- Calculate random numbers --> <set name='random_1' type='decimal'> <math.trunc><mul><math.random/>100</mul></math.trunc> </set> <set name='random_2' type='decimal'> <math.trunc><mul><math.random/>1000</mul></math.trunc> </set> <set name='random_3' type='decimal'> <math.trunc><mul><math.random/>25</mul></math.trunc> </set> <vtable.insert name='v_random'> <number><random_1 /></number> <number><random_2 /></number> <number><random_3 /></number> </vtable.insert> </do> </for> <!-- Create the workbook --> <set name='wb'> <excel.Workbook /> </set> <!-- Create the workbook sheet with name Hoja_1 --> <set name="sheet"> <excel.Workbook.createSheet name="Hoja_1"> <wb/> </excel.Workbook.createSheet> </set> <!-- Create a workbook sheet from ResultSet --> <excel.Sheet.fromResultSet > <sheet/> <!-- ResultSet to virtual table --> <vtable.toResultSet> <v_random /> </vtable.toResultSet> </excel.Sheet.fromResultSet> <!-- Creating a temporary EXCEL file with the workbook data --> <excel.Workbook.write> <wb/> <file name="test.xls" type="temp"/> </excel.Workbook.write> <!-- Prints the ResultSet --> <println> <!-- Load workbook into ResultSet --> <excel.Workbook.toResultSet start-header-row='1' start-column='A'> <wb/> </excel.Workbook.toResultSet> </println> </body> </xsql-script>
+--------+--------+--------+
|random_1|random_2|random_3|
+--------+--------+--------+
| 49 | 403 | 5 |
| 1 | 169 | 12 |
| 33 | 943 | 24 |
| 38 | 961 | 19 |
| 41 | 25 | 24 |
| 60 | 512 | 16 |
| 54 | 893 | 12 |
| 77 | 670 | 3 |
| 59 | 30 | 7 |
| 75 | 222 | 17 |
| 68 | 29 | 15 |
| 83 | 400 | 16 |
| 30 | 87 | 22 |
| 77 | 133 | 13 |
| 47 | 668 | 20 |
| 12 | 604 | 6 |
| 79 | 525 | 10 |
| 45 | 91 | 12 |
| 64 | 502 | 0 |
| 17 | 923 | 17 |
| 38 | 225 | 4 |
| 90 | 322 | 21 |
| 23 | 216 | 6 |
| 63 | 140 | 21 |
| 54 | 585 | 8 |
| 96 | 576 | 2 |
| 94 | 697 | 3 |
| 44 | 422 | 24 |
| 17 | 28 | 14 |
| 6 | 604 | 22 |
| 50 | 445 | 5 |
+--------+--------+--------+