Devika M

Devika M

  • NA
  • 39
  • 3.5k

Convert Excel to HTML by XSLT transformation of XML

Jul 17 2020 12:28 AM
I have a requirement to preview excel files in browser. I could generate an XML for the worksheet. I am trying to convert the XML to HTML by XSLT transformation. I am able to get the data in table format. I could not get the style part in xsl. Below is the XML for a sample spreadsheet.
  1. <Data>  
  2. <DataProps>  
  3. <sheetFormatPr defaultColWidth="9.25" defaultRowHeight="15" dyDescent="0.25" />  
  4. </DataProps>  
  5. <Row RowNumber="1">  
  6. <Cell Ref="A1" ColumnId="A" ColumnNumber="0" Type="s">  
  7. <CellProps />  
  8. <Value>Make </Value>  
  9. <DisplayValue>Make </DisplayValue>  
  10. </Cell>  
  11. <Cell Ref="B1" ColumnId="B" ColumnNumber="1" Type="s">  
  12. <CellProps />  
  13. <Value>Miles </Value>  
  14. <DisplayValue>Miles </DisplayValue>  
  15. </Cell>  
  16. <Cell Ref="C1" ColumnId="C" ColumnNumber="2" Type="s">  
  17. <CellProps />  
  18. <Value>Cost</Value>  
  19. <DisplayValue>Cost</DisplayValue>  
  20. </Cell>  
  21. </Row>  
  22. <Row RowNumber="2">  
  23. <Cell Ref="A2" ColumnId="A" ColumnNumber="0" Type="s">  
  24. <CellProps />  
  25. <Value>Ford</Value>  
  26. <DisplayValue>Ford</DisplayValue>  
  27. </Cell>  
  28. <Cell Ref="B2" ColumnId="B" ColumnNumber="1">  
  29. <CellProps />  
  30. <Value>40000</Value>  
  31. <DisplayValue>40000</DisplayValue>  
  32. </Cell>  
  33. <Cell Ref="C2" ColumnId="C" ColumnNumber="2" Style="1">  
  34. <CellProps numFmtId="6" formatCode=""$"#,##0_);[Red]\("$"#,##0\)" applyNumberFormat="1">  
  35. <font>  
  36. <sz val="11" />  
  37. <color theme="1" />  
  38. <name val="Calibri" />  
  39. <family val="Swiss" />  
  40. <scheme val="minor" />  
  41. </font>  
  42. </CellProps>  
  43. <Value>3500</Value>  
  44. <DisplayValue>$3,500</DisplayValue>  
  45. </Cell>  
  46. </Row>  
  47. <Row RowNumber="3">  
  48. <Cell Ref="A3" ColumnId="A" ColumnNumber="0" Type="s">  
  49. <CellProps />  
  50. <Value>Chevi</Value>  
  51. <DisplayValue>Chevi</DisplayValue>  
  52. </Cell>  
  53. <Cell Ref="B3" ColumnId="B" ColumnNumber="1">  
  54. <CellProps />  
  55. <Value>55000</Value>  
  56. <DisplayValue>55000</DisplayValue>  
  57. </Cell>  
  58. <Cell Ref="C3" ColumnId="C" ColumnNumber="2" Style="1">  
  59. <CellProps numFmtId="6" formatCode=""$"#,##0_);[Red]\("$"#,##0\)" applyNumberFormat="1">  
  60. <font>  
  61. <sz val="11" />  
  62. <color theme="1" />  
  63. <name val="Calibri" />  
  64. <family val="Swiss" />  
  65. <scheme val="minor" />  
  66. </font>  
  67. </CellProps>  
  68. <Value>4200</Value>  
  69. <DisplayValue>$4,200</DisplayValue>  
  70. </Cell>  
  71. </Row>  
  72. <Row RowNumber="4">  
  73. <Cell Ref="A4" ColumnId="A" ColumnNumber="0" Type="s">  
  74. <CellProps />  
  75. <Value>Tata</Value>  
  76. <DisplayValue>Tata</DisplayValue>  
  77. </Cell>  
  78. <Cell Ref="B4" ColumnId="B" ColumnNumber="1">  
  79. <CellProps />  
  80. <Value>51000</Value>  
  81. <DisplayValue>51000</DisplayValue>  
  82. </Cell>  
  83. <Cell Ref="C4" ColumnId="C" ColumnNumber="2" Style="1">  
  84. <CellProps numFmtId="6" formatCode=""$"#,##0_);[Red]\("$"#,##0\)" applyNumberFormat="1">  
  85. <font>  
  86. <sz val="11" />  
  87. <color theme="1" />  
  88. <name val="Calibri" />  
  89. <family val="Swiss" />  
  90. <scheme val="minor" />  
  91. </font>  
  92. </CellProps>  
  93. <Value>3800</Value>  
  94. <DisplayValue>$3,800</DisplayValue>  
  95. </Cell>  
  96. </Row>  
  97. </Data>  
XSL Sheet
  1. <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  2. <!-- This is the identity transformation, it is the default action -->  
  3. <xsl:template match="@*|node()">  
  4. <xsl:copy>  
  5. <xsl:apply-templates select="@*|node()"/>  
  6. </xsl:copy>  
  7. </xsl:template>  
  8. <!-- This transforms Data to table-->  
  9. <xsl:template match="Data">  
  10. <table>  
  11. <xsl:apply-templates select="@*|node()"/>  
  12. </table>  
  13. </xsl:template>  
  14. <!-- This transforms Row to tr -->  
  15. <xsl:template match="Row">  
  16. <tr>  
  17. <xsl:apply-templates select="@*|node()"/>  
  18. </tr>  
  19. </xsl:template>  
  20. <!-- This transforms Cell to td -->  
  21. <xsl:template match="Cell">  
  22. <td>  
  23. <xsl:value-of select="DisplayValue"/>  
  24. </td>  
  25. </xsl:template>  
  26. </xsl:stylesheet>  

Answers (1)