2011-08-07 6 views
0

データグリッドにExcelファイルを表示するのに、次のコードを使用しています。問題は、大きな数値が指数形式で表示されていることです。つまり、1236548965132160がグリッドに1.23654896513216E +Excel to DataGrid指数問題

string xlsxString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=myFile.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\""; 
OleDbConnection excelOleDBConnection = new OleDbConnection(xlsxString); 
excelOleDBConnection.Open(); 

OleDbDataAdapter dataAdapterForExcelFile = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", excelOleDBConnection); 
DataTable dataTableForTheAdapter = new DataTable(); 
dataAdapterForExcelFile.Fill(dataTableForTheAdapter); 
this.dataGridView1.DataSource = dataTableForTheAdapter.DefaultView; 

コードからこの指数関数形式を削除する必要がありますが、Excelではなく、提案してください。

答えて

1

つのオプション:

DataTable dataTableForTheAdapter = new DataTable(); 
dataAdapterForExcelFile.FillSchema(dataTableForTheAdapter, SchemaType.Source); 
for(int i = 0; i < dataTableForTheAdapter.Columns.Count; i++) 
    if (dataTableForTheAdapter.Columns[i].DataType == typeof(double)) 
     dataTableForTheAdapter.Columns[i].DataType = typeof(decimal); 
dataAdapterForExcelFile.Fill(dataTableForTheAdapter); 

か:

 this.dataGridView1.DataSource = dataTableForTheAdapter.DefaultView; 
    for (int i = 0; i < this.dataGridView1.Columns.Count; i++) 
    { 
     if (this.dataGridView1.Columns[i].ValueType == typeof(double)) 
      this.dataGridView1.Columns[i].DefaultCellStyle.Format = "N"; 
    } 
+0

素晴らしいです!ありがとう非常に非常に多くの@BlueMonkMN! :-) –