0

问题是在数据表中读取时,长数被转换为指数。例如,数字 1215313371140020130606144217 转换为 1.21531337114002E+27。

我的代码:

public static DataTable GetCSVRows(string path, bool IsFirstRowHeader)  
{
    string header = "Yes";
    string sql = string.Empty;
    string pathOnly = string.Empty;
    string fileName = string.Empty;
    DataTable dataTable = new DataTable();

    try
    {
        pathOnly = Path.GetDirectoryName(path);
        fileName = Path.GetFileName(path);
        sql = @"SELECT * FROM [" + fileName + "]";
        if (IsFirstRowHeader)
        {

            header = "Yes";
        }
        using (OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + "\""))
        {
            using (OleDbCommand command = new OleDbCommand(sql, connection))
            {
                using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
                {
                    dataTable = new DataTable();
                    adapter.Fill(dataTable);
                }
            }
        }
    }
}
4

1 回答 1

0

您需要将 OLEDB 4.0 更新为Microsoft.ACE.OleDb.12.0,并使用连接字符串 "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + FileName + ";Extended Properties='Excel 8.0;HDR=YES; IMEX =1' ",注意"IMEX=1"就可以解决了。

于 2013-10-19T15:26:51.110 回答