• 1 reply
  • Latest Post - ‏2012-12-12T12:26:06Z by SystemAdmin
1 Post

Pinned topic save XML file as .xlsx

‏2012-03-14T13:49:22Z |
I am doing an XSLT transformation of an XML file, resulting in a new XML file with another layout. I use for this transformation the following Java code:
if (args.length != 3) {
System.err.println("Usage: XML to Excel 2007 or higher "
+ "<input.xml> <input.xsl> <output.xml>");

String inXML = args[0];
String inXSL = args[1];
String outXML = args[2];

XMLtoXLSX st = new XMLtoXLSX();
try {
st.transform(inXML, inXSL, outXML);
} catch (TransformerConfigurationException e) {
System.err.println("Invalid factory configuration");
} catch (TransformerException e) {
System.err.println("Error during transformation");

public void transform(String inXML, String inXSL, String outXML)
throws TransformerConfigurationException, TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xslStream = new StreamSource(inXSL);
Transformer transformer = factory.newTransformer(xslStream);
transformer.setErrorListener(new MyErrorListener());
StreamSource in = new StreamSource(inXML);
StreamResult out = new StreamResult(outXML);
transformer.transform(in, out);
Now I want to save this transformed XML file (StreamResult out) programmatically as .xlsx via Java. Opening/importing an XML file in Excel 2007 or higher manually is easy, but how to write Java code for this? Can someone please give a reference to an tutorial or a code snippet to do this?
Updated on 2012-12-12T12:26:06Z at 2012-12-12T12:26:06Z by SystemAdmin
  • SystemAdmin
    1830 Posts

    Re: save XML file as .xlsx

    A Java library to save as .xlsx would definitely make your life easy.