如果用户自定义小数分隔符,TransferSpreadsheet 会生成无效的 XLSX 文件

TransferSpreadsheet builds invalid XLSX file if user customized decimal separator

在 Microsoft Access 2016(内部版本 16.0.8201.2200)中,自定义 Windows 10 中的数字格式时,VBA TransferSpreadsheet method 无法正常工作,特别是在计算机上选择美国地区后,如果您将 "decimal symbol" 和 "digit grouping symbol" 交换为德国的习惯格式:

当我使用 TransferSpreadsheet 保存查询时,当我随后尝试在 Excel 中打开该工作簿时,它说:

We have found some problem in some content in '...'. Do you want us to try to recover as much as we can?

当我这样做时,我收到以下警告:

Excel was able to open the file by repairing or removing the unreadable content.

当我查看 XLSX 内容的内容时,我并不惊讶它有问题,因为内部 XML 格式不正确。因为我已将 Windows 中的小数点分隔符替换为“,”,它在 XML 中用逗号而不是小数位创建数字。但是 XML 标准规定,无论您的区域偏好如何,XML 中的数字都应使用“.”。作为小数点。

<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  <dimension ref="A1:K20"/>
  <sheetViews>...</sheetViews>
  <sheetFormatPr defaultRowHeight="15"/>
  <sheetData>
    <row outlineLevel="0" r="1">...</row>
    <row outlineLevel="0" r="2">
      ...
      <c r="D2" s="0">
        <v>2,9328903531E+16</v>
      </c>
      <c r="E2" s="0">
        <v>5,404939826E+16</v>
      </c>
      <c r="F2" s="0">
        <v>2,3923963705E+16</v>
      </c>
      ...
    </row>
    ...
  </sheetData>
  <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>

虽然“,”可能是 UI 中十进制符号的所需格式,但 XLSX 内部格式必须符合 XML 标准,“.”小数点符号。

我该如何解决这个问题?

底线,为了让TransferSpreadsheet方法正常工作,如果你想改变数字的格式,不要使用"Customize Format"设置:

您应该将这些值重置为默认值,然后 select 在前面的对话框中选择一个适当的区域,根据您的喜好设置数字格式:

选择了一个按需要格式化的区域,从而避免了 TransferSpreadsheet 错误。当您执行此操作时,电子表格将正确显示在 Excel:

但 XLSX 也将被正确格式化:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
  <dimension ref="D3:F3"/>
  <sheetViews>
    <sheetView tabSelected="1" workbookViewId="0">
      <selection activeCell="F12" sqref="F12"/>
    </sheetView>
  </sheetViews>
  <sheetFormatPr defaultRowHeight="15" x14ac:dyDescent="0.25"/>
  <cols>
    <col min="4" max="6" width="26.85546875" style="1" bestFit="1" customWidth="1"/>
  </cols>
  <sheetData>
    <row r="3" spans="4:6" x14ac:dyDescent="0.25">
      <c r="D3" s="1">
        <v>2.9328903531E+16</v>
      </c>
      <c r="E3" s="1">
        <v>5.40493826E+16</v>
      </c>
      <c r="F3" s="1">
        <v>2.3923963705E+16</v>
      </c>
    </row>
  </sheetData>
  <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>