我写了一个C#应用程序来读取单元格值并显示到我的应用程序中。如果decimalSeperator的区域设置为.
而groupSeperator为,
,但如果decimalSeperator和groupseperator的设置从.
和,
更改,那么我得到的值是错误的。Excel数字格式与国际设置问题
在下面的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);
}
}
你可以在复制其中的值之前,将整个标签的单元格属性设置为普通文本吗?这将是最简单的方法 –
我不知道对象ItemArray [2]是什么,但是为什么你使用'.ToString()'?也许你可以得到一个“双”或“十进制”,如果它呢? – JOG