Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将excel导出为包含空白单元格的xml电子表格_Xml_Excel_Excel 2007 - Fatal编程技术网

将excel导出为包含空白单元格的xml电子表格

将excel导出为包含空白单元格的xml电子表格,xml,excel,excel-2007,Xml,Excel,Excel 2007,我正在将excel工作簿导出到xml电子表格中。excel有10列10行。某些单元格为空(即没有值) 当我将文件保存到XML电子表格中并查看其中包含空白单元格的行时,它只有单元格:空值的单元格不在那里,XML显示空白前的单元格,空白后的单元格是一个接一个的(空单元格不存在)。 以下是xml的一个示例: <Cell ss:StyleID="s36"><Data ss:Type="Number">cell1</Data><NamedCell ss:Nam

我正在将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>
cell1
细胞2
第四单元
缺少的单元格是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, "$", "")
    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
属性值(如果存在)(阅读了解详细信息),并将
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);
}
documentElement;
$root->removeAttributeNS($root->getAttributeNode('xmlns')->nodeValue',);
$xpath=新的DOMXPath($doc);
foreach($xpath->query('/Workbook/Worksheet/Table/Row')作为$Row)
{
$cells=array();
$cell_index=0;
foreach($xpath->query('./Cell',$row)作为$Cell)
{
if($cell->hasAttribute('ss:Index'))
$cell_index=$cell->getAttribute('ss:index');
其他的
++$cell_指数;
$cells[$cell_index-1]=$cell->nodeValue;
}
//现在处理数据
打印(单元格);
}

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

我正在使用xsl将此xml转换为我自己的xml模式。如果我将一个值连接到一个标题(放在第一行),我现在无法知道单元格是否丢失当您的架构设计不好时,由于存在间隙,我的值没有连接到正确的标头。我没有使用任何架构,我只想将excel保存为xml电子表格,保留空单元格。这会保留空单元格-A1处缺少的单元格与ZZ16000Excel 2007是xml电子表格一样处理。我目前不在开发的计算机附近,但我记得在导出时,每个元素都有属性,其中一个允许您使用确切地知道是哪一行(当他跳行时,他会将这个属性添加到下一行数据中),我只是用属性循环了这些元素,并创建了缺少的行,因为我需要XML中的所有内容。有一个索引:在行或单元格开始标记中,似乎说“我是行这个”或“我是单元格那个”。。如果您期望第8行,但在一个新的行标记中找到Index:10,那么您将添加一个空的第8行和第9行,这对于迭代是有效的,但这似乎意味着,如果不首先检查所有单元格的“Index”属性,就无法实现对单元格列表的随机访问。对吗?