错误写入 Excel 文件 - C++

Bug writing into Excel file - C++

当我将一个变量发送到 Excel 时,它改变了它的值。它恰好发生在 excel。当变量存储在容器中时也会发生这种情况。我想如果你看到代码就更清楚了:

#include<iostream>
#include<array>
#include<vector>
#include<fstream>

const int aSize{ 150000 };
std::array<double, aSize> anArray{};

int main(void)
{
    double aValue{ 0.00000005467 };
    std::vector<double> aVector;
    for (int i = 0; i < aSize; ++i)
    {
        anArray[i] = aValue;
        aVector.push_back(aValue);
    }

    std::ofstream fileOne, fileTwo, fileThree, fileFour, fileFive;
    fileOne.open("array.xls"); 
    fileTwo.open("array.txt");
    fileThree.open("vector.xls");
    fileFour.open("vector.txt");
    fileFive.open("value.xls");

    fileOne << anArray[0];
    fileTwo << anArray[0];
    fileThree << aVector[0];
    fileFour << aVector[0];
    fileFive << aValue;

    std::cout << aValue << "\n" << anArray[0] << "\n" << aVector[0];

    return 0;
}

我所做的只是填充一个向量和一个数组。如果我打印变量的值,我会得到预期的值。如果我将它发送到 .txt 中,我会得到预期的值。如果我只将值发送到 Excel,我会得到预期的值。

当我将容器中的值发送到 Excel 时,一切都崩溃了。为什么会这样?

MS-XLS 文件格式不是简单的文本文件。您不能简单地将文本放入其中并期望它正确显示。您需要更多代码 and/or 专用库才能与之交互。

the suggestions here

这里的问题似乎是 Excel 解释 c++ 程序的(格式化的)数字输出的方式。即使文本可能是正确的(从 cout 函数的角度来看),它也可能没有 'correct' 小数点字符(即点而不是逗号,或 反之亦然).

解决方案:确保 Excel 设置为使用与默认 c++ 语言环境相同的 "locale",或者将 c++ 语言环境设置为 [=10] =] 正在使用。