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
将Excel电子表格转换为XML_Excel_Excel Formula_Vba - Fatal编程技术网

将Excel电子表格转换为XML

将Excel电子表格转换为XML,excel,excel-formula,vba,Excel,Excel Formula,Vba,我有一个excel电子表格。当我试图将Excel电子表格保存为XML时,所有前导零都丢失了。我正在使用Excel 2016。我在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电子表格。当我试图将Excel电子表格保存为XML时,所有前导零都丢失了。我正在使用Excel 2016。我在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删除前导零。最后的所有列都已格式化为文本。下图:


任何帮助都将不胜感激。

您可以使用
XlRangeValueDataType
枚举来获取范围对象的值属性。保留所有零。对于包含五列的示例,您将获得:

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>

可乐
可乐
可乐
可乐
可乐
32
000001
000
001
1.
32
000032
000
032
22
32
000111
000
111
032

您似乎依靠自定义数字格式来实现前导零。您需要将值更改为range.text值,并将列的格式设置为text。最后的所有列的格式都设置为text。我也附上了照片。