当前位置: 首页 > 知识库问答 >
问题:

Excel XML到Java对象

邹昊
2023-03-14

我在使用Excel打开xml文件时遇到了一点问题。客户机向我发送了以下类型的XML:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <LastAuthor>Microsoft Office User</LastAuthor>
  <Created>1996-10-14T23:33:28Z</Created>
  <LastSaved>2021-03-10T13:02:44Z</LastSaved>
  <Version>16.00</Version>
 </DocumentProperties>
 <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
  <AllowPNG/>
 </OfficeDocumentSettings>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>15940</WindowHeight>
  <WindowWidth>28040</WindowWidth>
  <WindowTopX>380</WindowTopX>
  <WindowTopY>500</WindowTopY>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="12" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s62">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Arial"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s63">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Trebuchet MS" ss:Color="#000000" ss:Bold="1"/>
   <Interior ss:Color="#D3D3D3" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s64">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Trebuchet MS" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s65">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Trebuchet MS" ss:Color="#993300" ss:Bold="1"/>
   <Interior ss:Color="#FFFF00" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
 </Styles>
 <Worksheet ss:Name="SITES_INFO">
  <Table ss:ExpandedColumnCount="36" ss:ExpandedRowCount="2" x:FullColumns="1"
   x:FullRows="1" ss:StyleID="s62" ss:DefaultColumnWidth="65"
   ss:DefaultRowHeight="13">
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="75" ss:Span="4"/>
   <Column ss:Index="6" ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="175"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="90"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="225"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="200"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="225"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="100" ss:Span="1"/>
   <Column ss:Index="13" ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="125"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="100"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="175"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="125" ss:Span="2"/>
   <Column ss:Index="19" ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="175"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="160"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="150"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="160" ss:Span="1"/>
   <Column ss:Index="24" ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="250"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="200" ss:Span="1"/>
   <Column ss:Index="27" ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="160"
    ss:Span="1"/>
   <Column ss:Index="29" ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="175"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="160"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="215"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="175"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="160" ss:Span="1"/>
   <Column ss:Index="35" ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="200"/>
   <Column ss:StyleID="s62" ss:AutoFitWidth="0" ss:Width="100"/>
   <Row ss:AutoFitHeight="0" ss:Height="20">
    <Cell ss:StyleID="s63"><Data ss:Type="String">Protocol</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Site Number</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">PI Salutation</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">PI Last Name</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">PI First Name</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">PI Email</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">PI Phone Number</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Name</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Address 1</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Address 2</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment City</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment State</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Province</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Zip</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Country ISO Code</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Country</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Phone</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment FAX</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Contact First Name</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Contact Last Name</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Contact Email</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Contact Phone</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Drug Shipment Contact Role</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center Name</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center Address 1</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center Address 2</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center City</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center State</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center Province</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center Zip</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center Country ISO Code</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center Country</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center Phone</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center Fax</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Patient Treatment Center TimeZone</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Transaction Flag</Data></Cell>
   </Row>
   <Row>
    <Cell ss:StyleID="s64"><Data ss:Type="String">fgregre</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">0382</Data></Cell>
    <Cell ss:StyleID="s64"/>
    <Cell ss:StyleID="s64"><Data ss:Type="String">gregreger</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">gergre</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">vregregerge</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ergreger</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">regreger</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">8 Haaliya st</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">vervregerge</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">Haifa</Data></Cell>
    <Cell ss:StyleID="s64"/>
    <Cell ss:StyleID="s64"><Data ss:Type="String">N/A</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">gergere</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ISR</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ISRAEL</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">gergereg</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">gergreger</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">regerger</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">regerge</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">rgergerg</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ergerge</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ergerge</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">Rambam Medical Center</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">regergerge</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">Hematology Department</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ergergre</Data></Cell>
    <Cell ss:StyleID="s64"/>
    <Cell ss:StyleID="s65"><Data ss:Type="String">N/A</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ergreegre</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ISR</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ergerger</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">regergreger</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">regregerge</Data></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">(GMT+02:00) Jerusalem</Data></Cell>
    <Cell ss:StyleID="s65"><Data ss:Type="String">UPDATE</Data></Cell>
   </Row>
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <Selected/>
   <LeftColumnVisible>28</LeftColumnVisible>
   <Panes>
    <Pane>
     <Number>3</Number>
     <ActiveRow>5</ActiveRow>
     <ActiveCol>34</ActiveCol>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
 <Worksheet ss:Name="USER_INFO">
  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
   x:FullRows="1" ss:StyleID="s62" ss:DefaultColumnWidth="65"
   ss:DefaultRowHeight="13">
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <Panes>
    <Pane>
     <Number>3</Number>
     <ActiveRow>22</ActiveRow>
     <ActiveCol>7</ActiveCol>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>
   private void processFileData(String stringData, String filename, String extension)
    {
        InputStream inp = IOUtils.toInputStream(stringData, StandardCharsets.UTF_8);
        List<MULSite> siteList = new ArrayList<MULSite>();

        try {
            inp = FileMagic.prepareToCheckMagic(inp);
            Workbook workbook = WorkbookFactory.create(inp);
            Iterator<Row> iterator = workbook.getSheet("SITES_INFO").iterator();
            while (iterator.hasNext())
            {
                MULSite site = new MULSite();
                site.parse(iterator.next(),"xls", filename);
                DtoValidatorResult isValid = site.validate();
                if(isValid.getValid())
                {
                    splittableDtoMap.put(site.getSiteNumber(), site.toString());
                }
                else {
                    throw new RetrieverException("Issue encountered at " + filename);
                }
            }
        } catch (IOException | RetrieverException | JAXBException e) {
            e.printStackTrace();
        }
    }

但是,当它到达:workbook workbook=workbookfactory.create(inp);时,它会抛出以下错误:java.lang.IllegalArgumentException:您的InputStream既不是OLE2流,也不是OOXML流

我对如何处理这种类型的文件有点困惑,我尝试将扩展名从。xml更改为。xls,Excel可以用两种方式完美地打开它,但Apache POI不接受它,我不确定是文件的问题还是我如何用InputStream读取它。

共有1个答案

束福
2023-03-14

遗憾的是,这是不可能的,Apache POI不支持Office XML,它从来没有在库中实现过,因为微软很快就不赞成这样做。

我已经放弃了使用Apache POI来完成这一任务的尝试,并将使用XML解析器将其解析为DOM。

 类似资料:
  • 我想创建一个字符串,如下所示。 我们如何创建这个字符串使用

  • ..到这个Java category.class: 我搜索和阅读了不同的来源,从我理解的我应该使用地图,但事实是,我不知道如何做到这一点... 这是读取的代码,如果我调试它,我可以看到dataSnaphot有我需要的所有东西,但是类别c是null:

  • 在这个例子中,我们使用JsonNode创建了一个Tree,并将其写入json文件并读回树,然后将其转换为Student对象。 在C:\》Jackson_WORKSPACE创建名为C:\》Jackson_WORKSPACE的java类文件。 File: JacksonTester.java import java.io.File; import java.io.IOException; import

  • 问题内容: 我需要解析一个看起来像这样的json文件: 我想将这些X坐标和Y坐标放入JavaObject Click中,该类如下所示: 我看过gson是因为他们说这很容易,但是我不知道如何从文件中做到这一点。 问题答案: 假设您的json字符串数据存储在名为的变量中:

  • XML响应 我为电影和图像创建了pojo类。 Film.java 形象JAVA 请帮助我是新来的,我应该使用JAXB。 解决方案: 我已经加了 在Image的getter方法中,我添加了@xmlement(name=“Image”)。 在Image类中,我添加了一个注释@XmlRootElement(name=“Image”),它提供了我想要的东西。

  • 我试图从一个相当复杂的Java对象生成一个CSV文件。该对象是一个会话,具有一些属性,字符串和消息的列表,这些字符串和消息又具有一些属性,还有一个注释的列表,这些注释具有一些属性。 session类如下所示; 消息类如下所示; 事实上,开始认为(单一的)CSV可能不是解决这个问题的最佳方法。