我正在使用以下代码在数据网格中显示excel文件,问题是,大数字显示为指数形式,即1236548965132160在网格中显示为1.23654896513216E + 15,
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,请建议。
答案 0 :(得分: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";
}