nopi 导入
public void InportItemCls(string path) { try { using (Stream stream = new FileStream(@""+path+"", FileMode.Open, FileAccess.Read)) { HSSFWorkbook workbook = new HSSFWorkbook(stream); ISheet sheet = workbook.GetSheetAt(0); //读取数据,保存集合 //Execel第一行是标题,不是要导入数据库的数据 for (int i = 1; i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); Itemcls itemCls = new Itemcls(); itemCls.item_clsno = row.GetCell(0).CellType.ToString().Equals("String") ? row.GetCell(0).StringCellValue : row.GetCell(0).NumericCellValue + ""; temCls.item_clsname = row.GetCell(1).CellType.ToString().Equals("String") ? row.GetCell(1).StringCellValue : row.GetCell(1).NumericCellValue + ""; itemCls.cls_parent = row.GetCell(2).CellType.ToString().Equals("String") ? row.GetCell(2).StringCellValue : row.GetCell(2).NumericCellValue + ""; LItemCls.Add(itemCls);
////电话号码同样如此 //if (row.GetCell(3).CellType == HSSFCell.CELL_TYPE_NUMERIC) //{ // userinfo.Telephone = row.GetCell(3).NumericCellValue.ToString(); //}
//else //{ // userinfo.Telephone = row.GetCell(3).StringCellValue; //} //userinfo.AddDate = row.GetCell(4).DateCellValue; //userinfo.Address = row.GetCell(5).StringCellValue; ////注意:Excel中可空的地方,Remark可以不填,因此我们需要判断。 //if (row.GetCell(6) == null) //{ // userinfo.Remarks = ""; //} //else //{ // userinfo.Remarks = row.GetCell(6).StringCellValue; //} //new UserInfoBLL().AddNew(userinfo);
} } //Response.Write("导入数据成功"); } catch (Exception ex) { //Response.Write("错误:" + ex.Message); } SaveData(); } public void SaveData() { for (int i = LItemCls.Count-1; i >= 0; i--) { string sql = string.Format(@"INSERT INTO [t_bd_item_cls] ([item_clsno],[item_clsname] ,[cls_parent] ,[display_flag] ,[return_rate] ,[IsLeaf] ,[memo]) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", LItemCls[i].item_clsno.Trim(), LItemCls[i].item_clsname.Trim(), LItemCls[i].cls_parent.Trim(), LItemCls[i].display_flag.Trim(), LItemCls[i].return_rate.Trim(), LItemCls[i].IsLeaf.Trim(), LItemCls[i].memo.Trim()); SQLHelper helper = new SQLHelper(); helper.RunSQL(sql, null); } } #endregion