本文介绍了跳过第一行中的Excel文件的读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想一个Excel文件翻译成我的dataGridView而且它有列名的问题,因为Excel文件格式的方式有两种设置细胞的文档的其余部分。然而,列名实际上是对行#2。 ?我怎么能跳过第一行读取文件中,以便在DataGridView列显示从第二行的单元格值



当前代码:

  VAR的connectionString =的String.Format(供应商= Microsoft.ACE.OLEDB.12.0;数据源= {0};扩展属性= Excel的8.0; openFileDialog1.FileName); 

查询字符串=的String.Format(SELECT * FROM [{0} $],工作表Sheet1);
变种适配器=新OleDbDataAdapter的(查询的connectionString);

的DataSet DS =新的DataSet();

adapter.Fill(DS);

DataTable的DT = ds.Tables [0];

techGrid.DataSource = DT;


解决方案

就像Thit伦曾这样评价。拆下第一行之前设置DT作为数据源

 的DataRow行= dt.Rows [0]; 
dt.Rows.Remove(行);
techGrid.DataSource = DT;


Hello I'm trying to translate an Excel file to my dataGridView and it's having column name issues because the way the Excel file is formatted, there are two setup cells for the rest of the document. However the Column names are actually on Row #2. How can I skip the first row in the file read so that the Columns in the dataGridView show the cell values from the second row?

Current code:

  var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; data source={0}; Extended Properties=Excel 8.0;", openFileDialog1.FileName);

string query = String.Format("select * from [{0}$]", "Sheet1");
var adapter = new OleDbDataAdapter(query, connectionString);

DataSet ds = new DataSet();

adapter.Fill(ds);

DataTable dt = ds.Tables[0];

techGrid.DataSource = dt;
解决方案

Just like Thit Lwin has commented. Remove first row before set dt as datasource.

DataRow row = dt.Rows[0];
dt.Rows.Remove(row);
techGrid.DataSource = dt;

这篇关于跳过第一行中的Excel文件的读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:27