将Excel电子表格转换为XML

时间:2018-07-03 16:52:52

标签: excel excel-vba excel-formula vba

我有一个Excel电子表格。当我尝试将Excel电子表格另存为XML时,所有前导零都丢失了。我正在使用Excel2016。Excel电子表格中有63138行。以下是我的Excel电子表格的示例数据。

Col1  col2     col3  col4    col5
32    000001    000    001    1
32    000032    000    032    22
32    000111    000    111    032

当我将文件另存为XML时,如何防止excel掉掉前导零。最后的所有列均已格式化为文本。下面是图像:

enter image description here

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以将XlRangeValueDataType枚举用于Range对象的Value属性。所有零均保留。对于包含五列的示例,您将获得:

Sub GGG()

    Dim rng As Range
    Dim xml$

    xml = Range("A1").CurrentRegion.Value(XlRangeValueDataType.xlRangeValueXMLSpreadsheet)

    '// Save string here somewhere

End Sub

输出:

<?xml version="1.0"?>
<?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">
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:CharSet="204" x:Family="Swiss" ss:Size="11"
    ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s63">
   <Alignment ss:Horizontal="Left" ss:Vertical="Center"/>
   <Font ss:FontName="Inherit" x:CharSet="204" ss:Color="#303336"/>
  </Style>
  <Style ss:ID="s68">
   <NumberFormat ss:Format="@"/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="5" ss:ExpandedRowCount="4" ss:StyleID="s68"
   ss:DefaultRowHeight="15">
   <Row>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Col1</Data></Cell>
    <Cell><Data ss:Type="String">col2</Data></Cell>
    <Cell><Data ss:Type="String">col3</Data></Cell>
    <Cell><Data ss:Type="String">col4</Data></Cell>
    <Cell><Data ss:Type="String">col5</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">32</Data></Cell>
    <Cell><Data ss:Type="String">000001</Data></Cell>
    <Cell><Data ss:Type="String">000</Data></Cell>
    <Cell><Data ss:Type="String">001</Data></Cell>
    <Cell><Data ss:Type="String">1</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">32</Data></Cell>
    <Cell><Data ss:Type="String">000032</Data></Cell>
    <Cell><Data ss:Type="String">000</Data></Cell>
    <Cell><Data ss:Type="String">032</Data></Cell>
    <Cell><Data ss:Type="String">22</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="String">32</Data></Cell>
    <Cell><Data ss:Type="String">000111</Data></Cell>
    <Cell><Data ss:Type="String">000</Data></Cell>
    <Cell><Data ss:Type="String">111</Data></Cell>
    <Cell><Data ss:Type="String">032</Data></Cell>
   </Row>
  </Table>
 </Worksheet>
</Workbook>