Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88835

Save Excel file from existing base64 encoding

$
0
0

I have an existing base64 encoding of an Excel file from this xml file and I want to save that data (fileContent) to a physical excel file, but I am stuck doing it. I've seen some tutorials on how to encode it, but I couldn't get it to work on saving that data into a file that can be opened with Microsoft Excel. The solutions I tried prints all the data as excel entries (see attached image). I have asked a related question here, but I did not know that the encoded data is an actual .xls file.

Here's the code that does that:

package parsing;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxSample {

    public static void main(String argv[]) {

        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();

            DefaultHandler handler = new DefaultHandler() {

                StringBuilder value;

                @Override
                public void startElement(String uri, String localName,
                        String qName, Attributes attributes)
                        throws SAXException {
                    value = new StringBuilder();
                }

                @Override
                public void endElement(String uri, String localName,
                        String qName) throws SAXException {
                    if ("fileContent".equalsIgnoreCase(qName)) {
                        FileInputStream fis = null;
                        try {
                            String decodedValue = new String(DatatypeConverter.parseBase64Binary(value.toString()));
                            value.append(decodedValue);
                            // The name of the file to create.
                            String fileName = "temp.xls";
                            File file = new File(fileName);
                            fis = new FileInputStream(file);
                            BufferedInputStream inputStream = new BufferedInputStream(fis);
                            byte[] fileBytes = new byte[(int) file.length()];
                            inputStream.read(fileBytes);
                            inputStream.close();
                            System.out.println(qName + " = " + decodedValue);
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(SaxSample.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(SaxSample.class.getName()).log(Level.SEVERE, null, ex);
                        } finally {
                            try {
                                fis.close();
                            } catch (IOException ex) {
                                Logger.getLogger(SaxSample.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    } else {
                        System.out.println(qName + " = " + value);
                    }
                    value = new StringBuilder();
                }

                @Override
                public void characters(char ch[], int start, int length)
                        throws SAXException {
                    value.append(new String(ch, start, length));
                }

            };

            saxParser.parse(new File("src/parsing/CON729.xml"), handler);
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }

    }

}

Viewing all articles
Browse latest Browse all 88835

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>