我正在将 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>

缺失的单元格是 cell3


有没有办法让excel不节省空间?使用 xslt 进行游戏并不像看起来那么容易?

有帮助吗?

解决方案

如果单元格为空,这似乎是一种合理的优化以节省空间 - 为什么它不会丢失。

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

其他提示

让他重新创建电子表格的信息到底存储在哪里?如果这些行:

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

全部给予

  • 单元格数据/数据/单元格
  • 单元格数据/数据/单元格
  • 单元格数据/数据/单元格
  • /排

您可以构建自己的VBA宏。像这个。并添加对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, "<*>quot;, "")
    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

在我编写一些代码来处理省略的空单元格之前,我遇到了同样的问题。您只需使用 Cell 元素的 ss:Index 属性值(如果存在)(read XML电子表格参考以获取详细信息)并将 Cell 内容存储到正确的索引数组位置重新创建原始单元格顺序。

<?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);
}

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top