本文介绍了在读取 Excel 文件时跳过第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在尝试将 Excel 文件转换为我的 dataGridView,但由于 Excel 文件的格式设置方式不同,该文件存在列名问题,文档的其余部分有两个设置单元格.然而,列名实际上在第 2 行.如何跳过读取的文件中的第一行,以便 dataGridView 中的列显示第二行的单元格值?
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?
当前代码:
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;
推荐答案
就像 Thit Lwin 评论的那样.在将 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 文件时跳过第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!