使用 OLE DB 从标准格式的 excel 字段中获取整数值

Getting integer values out of an standard formatted excel field using OLE DB

我使用 OLE DB 将 excel 文件转换为制表位分隔的文本文件。在此 excel 文件中可以是 GTIN 或 EAN 代码。如果 excel 列的格式为标准格式,它会将例如 54260279477798 剪切为 5,42602E+13,这与我在文件中得到的字符串相同。

我的 OLE DB 连接字符串如下所示:

Provider=Microsoft.ACE.OLEDB.12.0;
Data Source={filename};
Extended Properties="Excel 12.0 Xml;HDR=NO;IMEX=1";

我的代码是这样的:

for (int x = 0; x < tDataTable.Rows.Count; x++)
{
    string tRowString = String.Empty;

    for (int y = 0; y < tDataTable.Columns.Count; y++)
    {
        if (y == maxColumns)
        {
            tRowString += tDataTable.Rows[x][y].ToString();
        }
        else
        {
            tRowString += tDataTable.Rows[x][y].ToString() + "\t";
        }
    }
    tRowString = Regex.Replace(tRowString, @"(?<!\r)\n", "");
    tStreamWriter.WriteLine(tRowString);
}

我使用

访问sheet
tCommand = new OleDbCommand("SELECT * FROM [" + "Tabelle1" + "$]", tConnection)
{
    CommandType = CommandType.Text
};

是否可以在提取值时重新格式化这些值?

我的一个朋友向我展示了解决方案。我不得不将它解析为两倍。

string tColumnString = String.Empty;

tColumnString = tDataTable.Rows[x][y].ToString();

if (tColumnString.Contains("e+013") || tColumnString.Contains("e+012") || tColumnString.Contains("e+011") || tColumnString.Contains("e+07"))
{
    double tColumnDouble = 0;
    if (double.TryParse(tColumnString, out tColumnDouble))
    {
        Console.WriteLine("Double konvertiert: " + tColumnDouble.ToString());
        tColumnString = Convert.ToString(tColumnDouble);
    }
}

我希望这也能帮助到其他人。