You might consider using Office Add-ins, which you can code in Java Script.
This is the MS recommended way of programming Office 365.
Use the Office Add-ins platform to build solutions that extend Office applications and interact with content in Office documents. With Office Add-ins, you can use familiar web technologies such as HTML, CSS, and ...
The hierarchical relationship in your crosstabbed columns (PropName/ValName) isn't really supported by the PIVOT command in SQL Server. An alternative approach is to write the normalized data out to CSV and then use a third-party tool that does support crosstabbing column groups like your PropName and ValName.
One such tool is xtab.py (https://pypi.org/...
While I haven't done this exact sort of thing myself I would recommend installing Python and Pandas.
This combination can:
Directly query your database(s)
Perform pivots with a little care on millions of data entries, see also
Output to CSV or Excel formats
Better yet it is all free, gratis & open source.