我正在使用SmartXLSX成功读取excel文件,但是我遇到了一个问题。我的程序正在将列标题与电子表格中的所有其他行一起读取为新行。我应该如何更新我的程序以跳过列标题并读取其他行。
private void GetCompanies()
{
int count = 0;
Companies = new List<Company>();
string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string path = Path.Combine(directory, "Toll-Companies.xlsx");
SmartXLS.WorkBook WB = new WorkBook();
WB.readXLSX(path);
DataTable dt = WB.ExportDataTable();
string CurrentType = string.Empty;
string CurrentCategory = string.Empty;
string Removerow = string.Empty;
string b,c;
//DataRow rowe = dt.Rows[0];
//dt.Rows.Remove(rowe);
//loop through each row
foreach (DataRow dr in dt.Rows)
{
//Get company name in column c from Excel
c = dr[2].ToString();
//Get value in column B from Excel
b = dr[0].ToString();
if (b.StartsWith("Type:"))
{
CurrentType = b.Substring(6).Trim();
}
if (b.StartsWith("Primary Specialty"))
{
CurrentCategory = b.Substring(20).Trim();
}
//if company name is empy then skip row
if (string.IsNullOrEmpty(c)) continue;
//string Type = dr[7].ToString();
//dt.Columns.Add(Type)
//string cmp_type = dr[0].ToString();
//if (string.IsNullOrEmpty(cmp_type) || cmp_type == "Type") continue;
var cmp = new Company();
cmp.company_type = CurrentType;
cmp.company_category = CurrentCategory;
cmp.name = dr[2].ToString();
Companies.Add(cmp);
count++;
}
这将得到你正在寻找的结果。我不确定您是否在 SmartXLSX 库中寻找一种方法。
foreach (DataRow dr in dt.Rows)
{
if(count==0) continue;
您可以使用
和跳过第一行...