title: How to create xlsx file from XML, with using XSLT in java
body: <p>I am able to create just xls file with an old format, which is Microsoft Spreadsheet 97-2003, but I would rather go for something newer.</p>
  
  <p>How to create newer xlsx format?</p>
  
  <p>I use <code>javax.xml.transform</code> java package. </p>
  
  <p>Here is a chunk of java code</p>
  
  <pre><code>tFactory = TransformerFactory.newInstance();
  transformer = tFactory.newTransformer(new StreamSource("file.xslt"));
  transformer.transform(streamSource, new StreamResult(new FileOutputStream(filenameXls)));
  </code></pre>
  
  <p>I have tried using different xsl stylesheet definition, but it didn't help.</p>
  
  <p>This is my xlst file. </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  
  &lt;xsl:stylesheet version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet"
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"&gt;
  
    &lt;xsl:output method="html" encoding="utf-8" indent="yes" doctype-public="-//W3C//DTD XHTML 1.1 Strict//EN" version="1.0"
                doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" xml:lang="pl" /&gt;
    &lt;xsl:decimal-format name="euro" decimal-separator="," grouping-separator="."/&gt;
  
    &lt;xsl:template match="/"&gt;
  
      &lt;Worksheet ss:Protected="0" ss:Name="Name"&gt;
          &lt;html&gt;
              &lt;head&gt;
                  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
                  &lt;style&gt;
                      td {
                          margin:auto;
                          text-align:center;
                          vertical-align:middle;
                      }
                      table th h2 {
                          background-color:rgb(198,239,206);
                      }
                      th.important {
                          background-color:rgb(255,235,166);
                      }
                  &lt;/style&gt;
              &lt;/head&gt;
              &lt;body&gt;
                  &lt;table border="1"&gt;
                  &lt;tr&gt;&lt;th&gt;
                  &lt;h2&gt;Articles&lt;/h2&gt;
                  &lt;/th&gt;&lt;/tr&gt;
                  &lt;/table&gt;
                  &lt;table border="1"&gt;
                      &lt;tr&gt;
                          &lt;th&gt;Lp.&lt;/th&gt;
                          &lt;th class="important"&gt;Title&lt;/th&gt;
                          &lt;th class="important"&gt;Date&lt;/th&gt;
                          &lt;th class="important"&gt;Autors&lt;/th&gt;
                          &lt;th class="important"&gt;Afilliated&lt;/th&gt;
                          &lt;th class="important"&gt;Employed&lt;/th&gt;
  
                      &lt;/tr&gt;
                      &lt;xsl:for-each select="works/article"&gt;
                          &lt;tr&gt;
                              &lt;td&gt;&lt;xsl:value-of select="lp"/&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;xsl:value-of select="title"/&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;xsl:value-of select="publication-date"/&gt;&lt;/td&gt;
                              &lt;td&gt;
                                  &lt;ul&gt;
                                      &lt;xsl:for-each select="./author"&gt;
                                          &lt;li&gt;
                                              &lt;xsl:value-of select="given-names"/&gt;&amp;#160;&lt;xsl:value-of select="family-name"/&gt;
                                          &lt;/li&gt;
                                      &lt;/xsl:for-each&gt;
                                  &lt;/ul&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;ul&gt;
                                      &lt;xsl:for-each select="./author"&gt;
                                          &lt;li&gt;
                                              &lt;xsl:value-of select="affiliated-to-unit"/&gt;
                                          &lt;/li&gt;
                                      &lt;/xsl:for-each&gt;
                                  &lt;/ul&gt;
                              &lt;/td&gt;
                              &lt;td&gt;
                                  &lt;ul&gt;
                                      &lt;xsl:for-each select="./author"&gt;
                                          &lt;li&gt;
                                              &lt;xsl:value-of select="employed-in-unit"/&gt;
                                          &lt;/li&gt;
                                      &lt;/xsl:for-each&gt;
                                  &lt;/ul&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      &lt;/xsl:for-each&gt;
                  &lt;/table&gt;
              &lt;/body&gt;
          &lt;/html&gt;
      &lt;/Worksheet&gt;
  
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>