编写一个C#应用程序,读取一个Excel文件中单元格中的数据
推荐于2017-12-16
展开全部
List<Employee> employeeList = new List<Employee>(); DataTable ExcelTable;
string sExcelFile = @"E:\documents\data.xls";
ExcelData excelData = new ExcelData();
string tableName = "num$";//schemaTable.Rows[2][2].ToString().Trim(); ;//获取 Excel 的表名,默认值是sheet1
ExcelTable = excelData.GetExcelData(sExcelFile, tableName);
int i = 0;
int comp = 0;
foreach (DataRow dr in ExcelTable.Rows)
{
if (i == 0)//第一行为标题 {
int count=dr.ItemArray.Count();
for (int j = 0; j < count; j++)
{
if ("CompName".Equals(dr[j].ToString()))
{
comp = j;
break;
}
}
i++; continue; }
if (dr.Table.Rows.Count < 1)
{
break;
}
Employee e = new Employee(); if (!string.IsNullOrEmpty(dr[comp].ToString())) {
e.EmployeeTel = dr[comp].ToString();
}employeeList .Add(e);
string sExcelFile = @"E:\documents\data.xls";
ExcelData excelData = new ExcelData();
string tableName = "num$";//schemaTable.Rows[2][2].ToString().Trim(); ;//获取 Excel 的表名,默认值是sheet1
ExcelTable = excelData.GetExcelData(sExcelFile, tableName);
int i = 0;
int comp = 0;
foreach (DataRow dr in ExcelTable.Rows)
{
if (i == 0)//第一行为标题 {
int count=dr.ItemArray.Count();
for (int j = 0; j < count; j++)
{
if ("CompName".Equals(dr[j].ToString()))
{
comp = j;
break;
}
}
i++; continue; }
if (dr.Table.Rows.Count < 1)
{
break;
}
Employee e = new Employee(); if (!string.IsNullOrEmpty(dr[comp].ToString())) {
e.EmployeeTel = dr[comp].ToString();
}employeeList .Add(e);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询