//预处理,加列名,删除Excel中无用的行
//给每一列添加名称
foreach (DataColumn dc in ds.Tables[0].Columns)
{
if (dc.Ordinal == 0) dc.ColumnName = "科目";
else if (dc.Ordinal == 1) dc.ColumnName = "月份";
else
dc.ColumnName = ds.Tables[0].Rows[2][dc.Ordinal].ToString();//列名等于第3行(行标题)中各列对应的值
}
//获取表中的月分
string startMonth = ds.Tables[0].Rows[1][1].ToString().Trim();
string endMonth = ds.Tables[0].Rows[1][2].ToString().Trim();
startMonth = startMonth.Replace("起始日期:", "");
endMonth = endMonth.Replace("截止日期:", "");
startMonth = startMonth.Substring(0, 4) + (startMonth.Substring(5, startMonth.IndexOf("月") - 5)).PadLeft(2, '0');
endMonth = endMonth.Substring(0, 4) + (endMonth.Substring(5, endMonth.IndexOf("月") - 5)).PadLeft(2, '0');
if (!startMonth.Equals(endMonth))
{
lbl_messagebox.Text = "导入文件不是单月数据";
lbl_messagebox.Visible = true;
return;
}
//将第1列修改为月份
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
ds.Tables[0].Rows[i][1] = startMonth; //循环修改列传值
}
ds.AcceptChanges();//这句最关键,完成修改,做用是允许对DATASET的修改
ds.Tables[0].Rows.RemoveAt(0);//删除第0行,行号从0开始编号,下同
ds.Tables[0].Rows.RemoveAt(0);//删除当前的第0行,原来的第1行
//移除行标题
ds.Tables[0].Rows.RemoveAt(0);//删除当前的第0行,原来的第2行
ds.Tables[0].Rows.RemoveAt(0);//删除当前的第0行,原来的第3行
将Excel导入DataSet中预处理,然后再导入数据库
最新推荐文章于 2024-08-15 08:00:00 发布