dortmundbvb0624 2017-01-04 15:03
浏览 115

如何在PHP中将.xml转换为.xlsx

I need the ability to convert a .xml file to a .xlsx file through a PHP script. I understand PHPExcel is likely the library that can achieve this, however I can find no documentation on this specific task. The closest I have come is with simply loading the xml file, and then attempting to save that load as a xlsx, but in doing so, breaks format and loses some data. The code shown below is what I am talking about.

$objReader = new PHPExcel_Reader_Excel2003XML();
$objPHPExcel = $objReader->load("xmlFile.xml");

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter -> save('/tmp/customPivotGrid.xlsx');

header('Content-disposition: attachment; filename='.str_replace(' ', '', $_GET['rptName']).'.xlsx');
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
readfile('/tmp/customPivotGrid.xlsx');

Like I said, this is working, but breaking structure and losing some data. I imagine a conversion needs to happen in between the load of the xml, and the resave as the xlsx. Does anyone have a solution, recommendation, or documentation regarding specifically only converting a XML to XLSX via PHP.

Thank you.

Edit in my current .xml styling that I am receiving.

<?xml version="1.0" encoding="utf-8"?>
<?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">
       <Title>Title</Title>
       <Author>Author</Author>
       <Created></Created>
   </DocumentProperties>
   <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
       <WindowHeight>9000</WindowHeight>
       <WindowWidth>50000</WindowWidth>
       <ProtectStructure>false</ProtectStructure>
       <ProtectWindows>false</ProtectWindows>
   </ExcelWorkbook>
   <Styles>
       <Style ss:ID="Default" ss:Name="Normal">
           <Alignment ss:Vertical="Top"/>
           <Font ss:FontName="Calibri" ss:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
       </Style>
       <Style ss:ID="ext-948" ss:Name="Heading 1">
           <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
           <Borders>
               <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#4F81BD"/>
           </Borders>
           <Font ss:FontName="Calibri" ss:Family="Swiss" ss:Size="11" ss:Color="#1F497D"/>
       </Style>
       <Style ss:ID="ext-949" ss:Name="Group Header">
           <Borders>
               <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#4F81BD"/>
           </Borders>
       </Style>
       <Style ss:ID="ext-950" ss:Name="Total Footer">
           <Borders>
               <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#4F81BD"/>
           </Borders>
       </Style>
       <Style ss:ID="ext-951" ss:Name="Title">
           <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
           <Font ss:FontName="Cambria" ss:Family="Swiss" ss:Size="18" ss:Color="#1F497D"/>
       </Style>
       <Style ss:ID="ext-967" ss:Parent="ext-949">
           <Alignment ss:Horizontal="Left" ss:Indent="0"/>
       </Style>
       <Style ss:ID="ext-968" ss:Parent="ext-950">
           <Alignment ss:Horizontal="Left" ss:Indent="0"/>
       </Style>
   </Styles>
   <Worksheet ss:Name="Grid">
       <Table x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="12.75" ss:DefaultColumnWidth="48" ss:LeftCell="1" ss:TopCell="1">
           <Row ss:AutoFitHeight="1" ss:Height="22.5" ss:StyleID="ext-951">
               <Cell ss:MergeAcross="6">
                   <Data ss:Type="String">Title</Data>
               </Cell>
           </Row>
           <Row ss:AutoFitHeight="1" ss:Height="20.25" ss:StyleID="ext-948">
               <Cell>
                   <Data ss:Type="String">Project Manager</Data>
               </Cell>
               <Cell ss:MergeAcross="1">
                   <Data ss:Type="String">Test</Data>
               </Cell>
               <Cell ss:MergeAcross="1">
                   <Data ss:Type="String">Test</Data>
               </Cell>
               <Cell ss:MergeAcross="1">
                   <Data ss:Type="String">Grand total</Data>
               </Cell>
           </Row>
           <Row ss:AutoFitHeight="1" ss:Height="20.25" ss:StyleID="ext-948">
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String">Total Cost</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String">Total Sell</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String">Total Cost</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String">Total Sell</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String">Total Cost</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String">Total Sell</Data>
               </Cell>
           </Row>
           <Row ss:AutoFitHeight="0">
               <Cell>
                   <Data ss:Type="String">John Doe</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">0</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">0</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">0</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">0</Data>
               </Cell>
           </Row>
           <Row ss:AutoFitHeight="0">
               <Cell>
                   <Data ss:Type="String">John Doe</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">35308.75</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">95719.42000000001</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">35308.75</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">95719.42000000001</Data>
               </Cell>
           </Row>
           <Row ss:AutoFitHeight="0">
               <Cell>
                   <Data ss:Type="String">John Doe</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">3078.61</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">6265.33</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">3078.61</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">6265.33</Data>
               </Cell>
           </Row>
           <Row ss:AutoFitHeight="0">
               <Cell>
                   <Data ss:Type="String">John Doe</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">139911.83</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">272964.89</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">139911.83</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">272964.89</Data>
               </Cell>
           </Row>
           <Row ss:AutoFitHeight="0">
               <Cell>
                   <Data ss:Type="String">John Doe</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">40045.579999999994</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">113359.39</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">18961.879999999997</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">52636.520000000004</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">59007.45999999999</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">165995.90999999997</Data>
               </Cell>
           </Row>
           <Row ss:AutoFitHeight="0">
               <Cell>
                   <Data ss:Type="String">John Doe</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">700</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">1050</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">700</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">1050</Data>
               </Cell>
           </Row>
           <Row ss:AutoFitHeight="0">
               <Cell>
                   <Data ss:Type="String">John Doe</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">67090.46</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">184097.89999999994</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">77105.98000000001</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">428772.8300000001</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">144196.44</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">612870.7300000008</Data>
               </Cell>
           </Row>
           <Row ss:AutoFitHeight="0">
               <Cell>
                   <Data ss:Type="String">Training Three</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="String"></Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">225</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">476</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">225</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">476</Data>
               </Cell>
           </Row>
           <Row ss:AutoFitHeight="0" ss:StyleID="ext-950">
               <Cell ss:StyleID="ext-968">
                   <Data ss:Type="String">Grand total</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">110914.65000000002</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">304772.6199999999</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">271513.44</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">850569.66</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">382428.0899999999</Data>
               </Cell>
               <Cell>
                   <Data ss:Type="Number">1155342.279999999</Data>
               </Cell>
           </Row>
       </Table>
       <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
          <PageSetup>
              <Layout x:CenterHorizontal="1" x:Orientation="Portrait" />
              <Header x:Margin="0.3" />
              <Footer x:Margin="0.3" x:Data="Page &amp;P of &amp;N" />
              <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75" />
          </PageSetup>
          <FitToPage />
          <Print>
              <PrintErrors>Blank</PrintErrors>
              <FitWidth>1</FitWidth>
              <FitHeight>32767</FitHeight>
              <ValidPrinterInfo />
              <VerticalResolution>600</VerticalResolution>
          </Print>
          <Selected />
          <ProtectObjects>False</ProtectObjects>
          <ProtectScenarios>False</ProtectScenarios>
      </WorksheetOptions>
   </Worksheet>
</Workbook>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)