作者:姚成栋
先要明白,导入功能无非是把excel等表中的数据放到数据库中,所以重要的部分就是取数据和添加数据。
First of all,你得弄一个导入的模板,excel中规定好单元格格式。然后,上传好excel放到一个临时文件夹,批量向数据库中插入数据。插入时,
- 如果是枚举可以以这种方式来取数据:
actypedesc = reader.GetCellValue(row, 0);
actypedesc = actypedesc == null ? "" : actypedesc;
actypedesc = actypedesc.Replace("\r", "").Replace("\n", "").Trim();
switch (actypedesc)
{
case "A320": actype = "1"; break;
case "B733": actype = "2"; break;
default: actype = ""; break;
}
if (actype == "")
{
throw new Common.WebException.AlertRefreshParentException("第【" + row + "】行机型错误!目前只能识别A320/B733中一种");
}
- 如果是只存tid的要先将name转成tid:
trainsitename = reader.GetCellValue(row, 1);
trainsitename = trainsitename == null ? "" : trainsitename;
trainsitename = trainsitename.Replace("\r", "").Replace("\n", "").Trim();
Model.QtBiBasDictionary paras = new Model.QtBiBasDictionary();
paras.BD_CName = trainsitename;
Model.QtBiBasDictionary[] inseqb = DB.QtBiBasDictionary.GetBasDictionaryList(loginUser, paras, null);
if (inseqb == null)
{
throw new Common.WebException.AlertRefreshParentException("第【" + row + "】行训练地点中文名称错误!请参考训练计划-理论训练-训练地点的名称");
}
trainsitetid = inseqb[0].Tid;
- 如果是string类型的就简单的了:
memo = reader.GetCellValue(row, 5);
memo = memo == null ? "" : memo;
memo = memo.Replace("\r", "").Replace("\n", "").Trim();
- 单元格格式是日期格式的,如下:
startdate = reader.GetDateTimeCellValue(row, 3);
startdate = startdate.ToString("yyyy-MM-dd") == "0001-01-01" ? DateTime.Parse("1900-01-01") : startdate;
- 最后只要插入就行了