Generate Excel file (xls or xlsx) based on script

Gustavo Olivei1
Tera Contributor

Hello All,

 

I am looking for some help to generate an Excel file based on Script. I read a lot of content based on CSV file, but the request is to have a XLS or XLXS file. So, I have the script results working fine for csv, so I just want to "improve" the result.

 

The request is:

I want an Excel file to show in the header the name of the field (ex: Assignment Group) and the number of records assigned to them (ex: 5). There is more complexity to obtain the result (filtering and using Glideaggregate), but the final output into the file should be like this:

 

Assignment GroupQuantity
Group A5
Group B2

 

The current code that I am using you can find below:

var gr = new GlideRecord('table');
gr.get('sys_id of record');
var grAttachment = new GlideSysAttachment();
grAttachment.write(gr, filename, contenttype, excelData);
14 REPLIES 14

You need to create a new style for it.

<Style ss:ID='BlueCell'>

<Font ss:Size='16'/>

<Interior ss:Color='#a5d8ff' ss:Pattern='Solid'/> // Add blue background

<Alignment ss:Horizontal='Center' ss:Vertical='Center' ss:WrapText='1'/>

</Style>


and use it like this

<Cell ss:StyleID='BlueCell'><Data ss:Type='String'>text...</Data></Cell>

 

Thanks George. It worked.

 

Also can you help me with what other tags I can use for more formatting. Like we used interior for background color, what tag we can use for to increase size of excel cell as wrap text true is not looking good.

Leandro Lopes
Tera Contributor

Do you provide how you assemble the data into the excelData variable?

Glen Moore
Giga Contributor

Well, if your goal is to group data, like showing Assignment Group and Quantity, you can tweak the script to use GlideAggregate to get those grouped results and then build the rows dynamically based on the output.
If you’re looking to learn more about how Excel works with scripts or want to refine your approach, Microsoft Excel Books has some great resources. It’s a good place to explore ideas and pick up tips for handling Excel files programmatically.