从现有的base64编码保存Excel文件

我有一个从这个xml文件的Excel文件现有的base64编码,我想保存该数据(fileContent)到一个物理的Excel文件,但我坚持这样做。 我已经看过一些关于如何对它进行编码的教程,但是我不能将它保存到可以用Microsoft Excel打开的文件中。 我尝试的解决scheme打印所有的数据作为excel条目(见附图)。 我在这里问了一个相关的问题 ,但我不知道编码的数据是一个实际的.xls文件。

这是这样的代码:

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(); } } } 

我不知道你要在你的方法endElement(...)做什么,但是如果你想得到一个可读的excel文件,只需要将编码的base64转换为一个字节数组(不另一个string) endElement(...)是这样的:

 @Override public void endElement(String uri, String localName, String qName) throws SAXException { if ("fileContent".equalsIgnoreCase(qName)) { try { Files.write(Paths.get("temp.xls"), DatatypeConverter.parseBase64Binary(value.toString())); } catch (IOException ex) { Logger.getLogger(SaxSample.class.getName()).log(Level.SEVERE, null, ex); } } else { // I'm still not sure why you're printing this ... debugging? System.out.println(qName + " = " + value); } } 

你唯一需要做的是:

  • 获取元素fileContent的内容(你已经这样做了)
  • 将这个内容解码成一个字节数组
  • 将这个字节数组保存在一个文件中(不需要做进一步的转换)