Java:将XML内容转换为Excel文件的StringWriter

时间:2016-10-10 06:51:59

标签: java xml excel jaxb apache-poi

我是Java的新手。我有一个java方法,通过使用JAXB生成具有XML内容的StringWriter:

PrepareXMLService.java

public StringWriter prepare(Report report) throws JAXBException {
        StringWriter xmlStringWriter = new StringWriter();

        JAXBContext jaxbContext = JAXBContext.newInstance(Report.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        jaxbMarshaller.marshal(report, xmlStringWriter);

        return xmlStringWriter;
    }

xmlStringWriter的XML内容

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<report>
    <reportObject>
        <object>CLIENT_ID</object>
        <objectValue>1111111</objectValue>
    </reportObject>
    <reportContent>
        <reportInterval>
            <startDate>2015-07-01T00:00:00+03:00</startDate>
            <endDate>2016-06-30T23:59:59+03:00</endDate>
        </reportInterval>
        <client>
            <clientId>1111111</clientId>
            <clientName>test</clientName>
            <clientTown>test</clientTown>
            <clientAddress>test</clientAddress>
        </client>
        <transaction>
            <transactionDate>2015-10-23T00:00:00+03:00</transactionDate>
            <transactionAmount>23</transactionAmount>
            <transactionCurrency>7777</transactionCurrency>
            <transactionClientId>8798768789</transactionClientId>
            <transactionNetAmount>22</transactionNetAmount>
        </transaction>
    </reportContent>
</report>

我需要从这个StringWriter创建一个Excel文件。我必须使用Apache POI,但我真的不明白如何使用我的StringWriter来实现它。我不能用数据写一个物理XML文件以便以后读取它,因为这是业务方禁止的,我认为这不是很有效。

1 个答案:

答案 0 :(得分:2)