将excel导出到带空白单元格的xml电子表格

我正在将一个Excel工作簿导出到XML电子表格中。 excel有说10列和10行。 有些单元格是空的(即没有值)。

当我将文件保存到xml电子表格并查看其中有空白单元格的行时,它只有单元格:具有空值的单元格不存在,并且xml显示空白之前的单元格和空白之后的单元格是一个又一个(空单元不存在)。

这里是一个xml的示例:

<Cell ss:StyleID="s36"><Data ss:Type="Number">cell1</Data><NamedCell ss:Name="Print_Area"/></Cell> <Cell><Data ss:Type="String">cell2</Data><NamedCell ss:Name="Print_Area"/></Cell> <Cell><Data ss:Type="String">cell4</Data><NamedCell ss:Name="Print_Area"/></Cell> 

缺less的单元格是cell3


有没有办法问问excel不是为了节省空间? 娱乐是不是很容易,因为它似乎使用xslt?

如果单元格是空的,这似乎是一个合理的优化,以节省空间 – 为什么不应该丢失。

您有足够的信息来重新创build原始电子表格

确切地说,存储的信息在哪里,让他重新创build电子表格? 如果这些行:

  • 数据,空,数据,空,数据
  • 数据,数据,数据,空,空
  • 数据,空,空,数据,数据

所有给予

  • 细胞数据/数据/细胞
  • 细胞数据/数据/细胞
  • 细胞数据/数据/细胞
  • /行

你可以build立你自己的VBAmacros。 像这个。 并添加对Microsoft.xml的引用。

 Sub makeXml() ActiveCell.SpecialCells(xlLastCell).Select Dim lastRow, lastCol As Long lastRow = ActiveCell.Row lastCol = ActiveCell.Column Dim iRow, iCol As Long Dim xDoc As New DOMDocument Dim rootNode As IXMLDOMNode Set rootNode = xDoc.createElement("Root") Dim rowNode As IXMLDOMNode Dim colNode As IXMLDOMNode 'loop over the rows For iRow = 2 To lastRow Set rowNode = xDoc.createElement("Row") 'loop over the columns For iCol = 1 To lastCol If (Len(ActiveSheet.Cells(1, iCol).Text) > 0) Then Set colNode = xDoc.createElement(GetXmlSafeColumnName(ActiveSheet.Cells(1, iCol).Text)) colNode.Text = ActiveSheet.Cells(iRow, iCol).Text rowNode.appendChild colNode End If Next iCol rootNode.appendChild rowNode Next iRow xDoc.appendChild rootNode fileSaveName = Application.GetSaveAsFilename( _ fileFilter:="XML Files (*.xml), *.xml") xDoc.Save (fileSaveName) set xDoc = Nothing End Sub Function GetXmlSafeColumnName(name As String) Dim ret As String ret = name ret = Replace(ret, " ", "_") ret = Replace(ret, ".", "") ret = Replace(ret, ",", "") ret = Replace(ret, "&", "") ret = Replace(ret, "!", "") ret = Replace(ret, "@", "") ret = Replace(ret, "$", "") ret = Replace(ret, "#", "") ret = Replace(ret, "%", "") ret = Replace(ret, "^", "") ret = Replace(ret, "*", "") ret = Replace(ret, "(", "") ret = Replace(ret, ")", "") ret = Replace(ret, "-", "") ret = Replace(ret, "+", "") GetXmlSafeColumnName = ret End Function 

我写了一些代码来处理遗漏的空单元格之前,我有同样的问题。 您只需要使用ss:Index Cell元素的ss:Index属性值(如果存在),并将Cell内容存储到适当的索引数组位置以重新创build原始单元格顺序。

 <?php $doc = new DOMDocument('1.0', 'utf-8'); if (!$doc->load('sample.xml')) die(); $root = $doc->documentElement; $root->removeAttributeNS($root->getAttributeNode('xmlns')->nodeValue, ''); $xpath = new DOMXPath($doc); foreach ($xpath->query('/Workbook/Worksheet/Table/Row') as $row) { $cells = array(); $cell_index = 0; foreach ($xpath->query('./Cell', $row) as $cell) { if ($cell->hasAttribute('ss:Index')) $cell_index = $cell->getAttribute('ss:Index'); else ++$cell_index; $cells[$cell_index - 1] = $cell->nodeValue; } // now process data print_r($cells); } 

请注意,空单元格不会被添加到数组,而其他所有的东西都在它的位置上。 如果需要,可以计算所有行中可能的最大单元索引(表列数)。