Page 1 of 1

save XML file as .xlsx

PostPosted: Thu Mar 15, 2012 2:37 am
by prutser
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?

Re: save XML file as .xlsx

PostPosted: Fri Mar 16, 2012 12:57 am
by jason
You might find it easiest to transform to a "Flat OPC XML" file, which xlsx4j can then convert to a .xlsx file.