带有国际设置问题的Excel数字格式

时间:2016-11-07 00:40:01

标签: c# excel internationalization

我编写了一个C#应用程序来读取单元格值并显示到我的应用程序中。如果decimalSeperator的区域设置为.且groupSeperator为,,则它工作正常,但如果decimalSeperator和groupseperator的设置从.,更改,则值为我得到的是错的。

在下面的readValues中,要读取的单元格值为23.14。由于国际背景是针对欧洲国家的,因此价值为23,14。但我在strNumberCell中获得的价值是2314。这是为什么?

private DataSet _ds = new DataSet();

public Form1()
{
    InitializeComponent();
    LoadExcel();
    ReadValues();
}

private void ReadValues()
{
    //The cell value to be read is 23.14
    //Since the International setting for Europe country its value is 23,14
    //Value i am getting in strNumberCell is 2314
    string strNumberCell = _ds.Tables[1].Rows[1].ItemArray[2].ToString();
}

private void LoadExcel()
{
    try
    {
        // XLSX - Excel 2007, 2010, 2012, 2013
        using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" +
                                                            "Data Source=" + System.Windows.Forms.Application.StartupPath + "\\Test.xlsx;" +
                                                            "Extended Properties=Excel 12.0 Xml;"))
        {
            conn.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;

            // Get all Sheets in Excel File
            System.Data.DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            // Loop through all Sheets to get data
            foreach (DataRow dr in dtSheet.Rows)
            {
                string sheetName = dr["TABLE_NAME"].ToString();

                if (!sheetName.EndsWith("$"))
                    continue;

                // Get all rows from the Sheet
                cmd.CommandText = "SELECT * FROM [" + sheetName + "]";

                System.Data.DataTable dt = new System.Data.DataTable();
                dt.TableName = sheetName;

                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                da.Fill(dt);
                _ds.Tables.Add(dt);


            }

            cmd = null;
            conn.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + " at LoadExcel function.", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
}

1 个答案:

答案 0 :(得分:0)

我猜测Excel本身,当你在Excel中查看它时,将其值格式化为一个字符串,使用另一种格式化方法,而不是使用默认ToString而没有任何参数。

尝试使用CultureInfo投入格式化程序。以下是MSDN on ToString的示例。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      String[] cultureNames = { "en-US", "en-GB", "fr-FR",
                                "hr-HR", "ja-JP" };
      Decimal value = 1603.49m;
      foreach (var cultureName in cultureNames) {
         CultureInfo culture = new CultureInfo(cultureName);
         Console.WriteLine("{0}: {1}", culture.Name,
                           value.ToString("C2", culture));
      }
   }
}
// The example displays the following output:
//       en-US: $1,603.49
//       en-GB: £1,603.49
//       fr-FR: 1 603,49 €
//       hr-HR: 1.603,49 kn
//       ja-JP: ¥1,603.49

我不知道ItemArray[2]到底是什么类型的对象,但是如果你想要那个特定单元格的文化信息,也许你也可以从那里得到它。