빈 셀을 사용하여 Excel을 XML 스프레드 시트로 내 보냅니다
-
08-07-2019 - |
문제
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, "$", "")
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
존재하는 경우 요소 (읽기 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);
}
빈 셀은 어레이에 추가되지 않지만 다른 모든 것은 그 자리에 있습니다. 필요한 경우 모든 행을 통해 가능한 최대 셀 지수 (테이블 열 수)를 계산할 수 있습니다.