我需要能够通过PHP脚本将.xml文件转换为.xlsx文件。我理解PHPExcel可能是可以实现此目的的库,但是我找不到关于这个特定任务的文档。我最接近的是简单地加载xml文件,然后尝试将该加载保存为xlsx,但这样做会破坏格式并丢失一些数据。下面显示的代码就是我所说的。
$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');
就像我说的,这是有效的,但是破坏了结构并丢失了一些数据。我想在xml的加载和xlsx的重新保存之间需要进行转换。有没有人有关于仅通过PHP将XML转换为XLSX的解决方案,建议或文档。
谢谢。
使用我当前收到的.xml样式编辑。
<?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 &P of &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>