为Excel创建OData提供程序-电源查询显示$ metadata xml

时间:2018-12-03 21:57:35

标签: php excel odata

我正在创建一个OData提供程序以与具有PHP库的https://github.com/Algo-Web/POData

一起用于Excel。

我设法通过以下方式在本地网络的浏览器中提供了示例数据集:

http://dss.example.com/proto/odata.svc/Samples?$ format = json

{
  "value":[
    {
      "id":"1","firstName":"Rebbecca","lastName":"Didio","companyName":"Brandt, Jonathan F Esq","address":"171 E 24th St","city":"Leith","state":"TA","post":"7315","phone1":"03-8174-9123","phone2":"0458-665-290","email":"rebbecca.didio@didio.com.au","web":"http://www.brandtjonathanfesq.com.au"
    },{
      "id":"2","firstName":"Stevie","lastName":"Hallo","companyName":"Landrum Temporary Services","address":"22222 Acoma St","city":"Proston","state":"QL","post":"4613","phone1":"07-9997-3366","phone2":"0497-622-620","email":"stevie.hallo@hotmail.com","web":"http://www.landrumtemporaryservices.com.au"
    },{
      "id":"3","firstName":"Mariko","lastName":"Stayer","companyName":"Inabinet, Macre Esq","address":"534 Schoenborn St #51","city":"Hamel","state":"WA","post":"6215","phone1":"08-5558-9019","phone2":"0427-885-282","email":"mariko_stayer@hotmail.com","web":"http://www.inabinetmacreesq.com.au"
    },{
      "id":"4","firstName":"Gerardo","lastName":"Woodka","companyName":"Morris Downing & Sherred","address":"69206 Jackson Ave","city":"Talmalmo","state":"NS","post":"2640","phone1":"02-6044-4682","phone2":"0443-795-912","email":"gerardo_woodka@hotmail.com","web":"http://www.morrisdowningsherred.com.au"
    },{
      "id":"5","firstName":"Mayra","lastName":"Bena","companyName":"Buelt, David L Esq","address":"808 Glen Cove Ave","city":"Lane Cove","state":"NS","post":"1595","phone1":"02-1455-6085","phone2":"0453-666-885","email":"mayra.bena@gmail.com","web":"http://www.bueltdavidlesq.com.au"
    },{
      "id":"6","firstName":"Idella","lastName":"Scotland","companyName":"Artesian Ice & Cold Storage Co","address":"373 Lafayette St","city":"Cartmeticup","state":"WA","post":"6316","phone1":"08-7868-1355","phone2":"0451-966-921","email":"idella@hotmail.com","web":"http://www.artesianicecoldstorageco.com.au"
    },
    ...
}

http://dss.example.com/proto/odata.svc/ $ metadata

上的相应元数据
<?xml version="1.0" encoding="UTF-8"?>
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns="http://schemas.microsoft.com/ado/2009/11/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" Version="3.0">
    <edmx:DataServices m:MaxDataServiceVersion="3.0" m:DataServiceVersion="3.0">
        <Schema Namespace="Proto">
            <EntityType OpenType="false" Abstract="false" Name="Sample">
                <Key>
                    <PropertyRef Name="id"/>
                </Key>
                <Property Name="id" Type="Edm.Int64" Nullable="false"/>
                <Property Name="firstName" Type="Edm.String" Nullable="false"/>
                <Property Name="lastName" Type="Edm.String" Nullable="false"/>
                <Property Name="companyName" Type="Edm.String" Nullable="false"/>
                <Property Name="address" Type="Edm.String" Nullable="false"/>
                <Property Name="city" Type="Edm.String" Nullable="false"/>
                <Property Name="state" Type="Edm.String" Nullable="false"/>
                <Property Name="post" Type="Edm.String" Nullable="false"/>
                <Property Name="phone1" Type="Edm.String" Nullable="false"/>
                <Property Name="phone2" Type="Edm.String" Nullable="false"/>
                <Property Name="email" Type="Edm.String" Nullable="false"/>
                <Property Name="web" Type="Edm.String" Nullable="false"/>
            </EntityType>
            <EntityContainer Name="Proto" m:IsDefaultEntityContainer="true">
                <EntitySet Name="Proto" EntityType="Proto.Sample" cg:GetterAccess="Public"/>
            </EntityContainer>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

通过记录标题,我已经确认Excel提出了以下请求:

请求1似乎是所有数据。该库使用JSON对此进行响应

User-Agent: Microsoft.Data.Mashup (https://go.microsoft.com/fwlink/?LinkID=304225)
MaxDataServiceVersion: 3.0
OData-MaxVersion: 4.0
Accept: application/json;odata.metadata=minimal;q=1.0,application/json;odata=minimalmetadata;q=0.9,application/atomsvc+xml;q=0.8,application/atom+xml;q=0.8,application/xml;q=0.7,text/plain;q=0.7
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

仅针对JSON的请求2

User-Agent: Microsoft.Data.Mashup (https://go.microsoft.com/fwlink/?LinkID=304225)
OData-MaxVersion: 4.0
Accept: application/json;odata.metadata=minimal
Accept-Encoding: gzip, deflate
Host: ds1.example.com

该库对此进行JSON响应

atom / xml的请求3

通过反复试验,我决定在此处返回元数据

User-Agent: Microsoft.Data.Mashup (https://go.microsoft.com/fwlink/?LinkID=304225)
MaxDataServiceVersion: 3.0
Accept: application/json;odata=minimalmetadata;q=1.0,application/atomsvc+xml;q=0.8,application/atom+xml;q=0.8,application/xml;q=0.7,text/plain;q=0.7
Accept-Encoding: gzip, deflate
Host: ds1.example.com
---Request Start: /proto/odata.svc/Samples---
User-Agent: Microsoft.Data.Mashup (https://go.microsoft.com/fwlink/?LinkID=304225)
MaxDataServiceVersion: 3.0
Accept: application/atomsvc+xml;q=0.8,application/atom+xml;q=0.8,application/xml;q=0.7,text/plain;q=0.7
Accept-Encoding: gzip, deflate

但是,这会在PowerQuery中显示XML元数据作为查询的结果。

我还修改了OData后端,以返回通过浏览器请求时返回的xml:http:/dss.example.com/proto/odata.svc/Samples

对于此变体,Excel显示错误:

  

“ Odata:给定的URL既不指向OData服务也不是供稿:”

我是OData的新手,目的是通过Web门户传递Table数据,而无需在运行Excel的计算机上安装任何东西,否则我将使用ODBC

1 个答案:

答案 0 :(得分:0)

通过documentation

  

OData支持两种表示资源的格式,基于XML   Atom格式和JSON格式。如HTTP中所述   规范[RFC2616],客户可以指明其偏好   通过包含带有一个   可以处理的MIME类型列表。

这意味着您只能使用两个formats ATOM和JSON。 但是,默认情况下,metadata返回xml格式。这意味着您可以更改格式,并且默认情况下可以更改ResourcePath。

Algo-Web / POData具有BaseService.php。 有TargetKind枚举。 TargetKind有资源。 资源是实体(例如项目http://dss.example.com/proto/odata.svc/Samples中的Samples)。

在这种情况下,我认为您需要更改BaseService.php

case TargetKind::RESOURCE():
                return HttpProcessUtility::selectMimeType(
                    $requestAcceptText,
                    array_merge(
                        [MimeTypes::MIME_APPLICATION_ATOM],
                        $baseMimeTypes
                    )
                );

,然后将MimeTypes::MIME_APPLICATION_ATOM更改为MimeTypes.MIME_APPLICATION_XML

例如

return HttpProcessUtility::selectMimeType(
                    $requestAcceptText,
                    array_merge(
                        [MimeTypes::MIME_APPLICATION_XML],
                        $baseMimeTypes
                    )
  );