【发布时间】:2018-06-17 14:25:39
【问题描述】:
我能够很好地读取数据。但现在我有一个问题。 我需要从 Excel 表中的 3 行开始读取数据。然后将数据转换为数据表
如何设置行位置以开始从 excel 表中读取数据[从 3 行开始读取] 示例:
excel sheet
1 list of names of the people
2 Employee Name
3 kumar
4 kiran
5 manu
6 manju
所以我应该开始从 excel 中的 2 行读取数据。以便 我的数据表会有
Employee Name
kumar
kiran
manu
manju
我正在使用 excel 2007。 这是我正在使用的代码下面是我需要更改的任何内容。
public static DataTable ExcelToDataTable(string strfilelocation)
{
OleDbConnection excelConn= new OleDbConnection();
DataTable dtPatterns = new DataTable();;
try
{
DataSet ds = new DataSet();
OleDbCommand excelCommand = new OleDbCommand(); OleDbDataAdapter excelDataAdapter = new OleDbDataAdapter();
string excelConnStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + strfilelocation + "; Extended Properties =Excel 8.0;";
excelConn =new OleDbConnection(excelConnStr);
excelConn.Open();
excelCommand = new OleDbCommand("SELECT `Employee Name` as PATTERN FROM [sheet1$]", excelConn);
excelDataAdapter.SelectCommand = excelCommand;
excelDataAdapter.Fill(dtPatterns);
//"dtPatterns.TableName = Patterns";
ds.Tables.Add(dtPatterns);
}
catch (Exception ex)
{
WriteError(ex.Message);
}
finally
{
excelConn.Close();
}
return dtPatterns;
}
任何帮助将不胜感激。寻找解决方案
【问题讨论】:
标签: c#