Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
1 reply Latest Post - ‏2012-12-12T12:26:06Z by SystemAdmin
PRUTSER
PRUTSER
1 Post
ACCEPTED ANSWER

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>");
System.exit(1);
}

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");
System.err.println(e);
} catch (TransformerException e) {
System.err.println("Error during transformation");
System.err.println(e);
}
}

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