Results 1 to 3 of 3
  1. #1
    prutser is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default save XML file as .xlsx

    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?

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default Re: save XML file as .xlsx

    it looks like you are just passing Strings and not real files and file names,
    so once transformation get executes nothing happens.

    to make sure that everything is OK, simply run this method using real file names (for xsl and input xml)
    and make sure output.xlsx is created with proper result.

    Java Code:
    public void transform(String inXML, String inXSL, String outXML)
    	throws TransformerConfigurationException, TransformerException {
    		
    		File inXLSFile = new File("C:\\workspace\\MyProject\\input.xsl"); //provide proper xsl
    		
    		TransformerFactory factory = TransformerFactory.newInstance();
    		StreamSource xslStream = new StreamSource(inXLSFile);
    		
    		Transformer transformer = factory.newTransformer(xslStream);
    		transformer.setErrorListener(new MyErrorListener());
    		
    		
    		File inFile = new File("C:\\workspace\\MyProject\\input.xml"); //provide proper input xml
    		File outFile = new File("C:\\workspace\\MyProject\\output.xlsx"); //this file gets created after transformation
    		StreamSource in = new StreamSource(inFile);
    		StreamResult out = new StreamResult(outFile);
    		
    		transformer.transform(in, out);
    		
    	}

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: save XML file as .xlsx

    Those parameters are (I suspect) filenames.
    So just pass '<yourfilename>.xlsx' as the third parameter.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 11-17-2011, 09:33 AM
  2. Replies: 3
    Last Post: 06-21-2011, 04:51 PM
  3. how to convert .xlsx data to .txt
    By avitra in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 02-24-2011, 09:18 AM
  4. save file based on file extension
    By masa in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2010, 11:17 AM
  5. Replies: 0
    Last Post: 03-05-2010, 04:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •