2023年6月21日发(作者:)

C#将excel⽂件导⼊到SqlServer数据库项⽬结构(很标准的三层架构):⼀、做⼀些准备1. 这⾥上传⽂件⽤到的控件是webuploader,下载地址:webuploader的使⽤⽅法:2. 如果需要使⽤到easyUI,下载地址:,使⽤⽅法也在这⾥3. 导⼊ExcelDataReader和et4. 导⼊模板,记得与数据库的内容对应(1)我的excel模板,注意:保留⼀张sheet(2)数据库(其实这⾥是不允许为空的,因为没有对excel表中为空的数据进⾏处理,因为注重的不是这个,所以没有做处理。⾃⼰添加约束即可。)⼆、正式开始注:为了贴图⽅便,顺序可能和实际编写代码的顺序不⼀致1. 下⾯是前端页⾯的代码。标明了导⼊必须的代码以及⾮必须的代码,可以根据需要查看。 导⼊学员

再次提醒!!这⾥和实际编写代码顺序不⼀样,这⾥相当于程序实际运⾏起来的调⽤顺序,所以会有⼀些⼩问题,继续看下去就好啦2. using System;using c;using ;using ;using ;namespace Model{ //和数据库的字段⼀⼀对应 public class InAndOutModel { public int id { get; set; } public string name { get; set; } public int gender { get; set; } public string idcardnum { get; set; } public string address { get; set; } public string phone { get; set; } }}3.

using System;using c;using ;using tion;using ;using Model;using BLL;using ;using ;using ExcelDataReader;namespace 导⼊导出{ ///

/// InAndOut

的摘要说明 ///

public class InAndOut : IHttpHandler { public void ProcessRequest(HttpContext context) { tType = "text/plain"; //这⾥是⽤委托调⽤对应⽅法 var type = t["type"]; if(!OrEmpty(type)) { MethodInfo method = e().GetMethod(type); if(method != null) { (this, null); } } } /// ///

获取列表,⾮必须 ///

public void GetDataList() { InAndOutBll bll = new InAndOutBll(); List list = new List(); list = t(""); list = t(""); if(list != null && > 0) { var item = new { code = 1, list = list }; //这⾥还需要引⼊⼀个⼈Newtonsoft包 (izeObject(item)); } else { var item = new { code = 0, msg = "当前没有数据可以绑定" }; (izeObject(item)); } } /// ///

重要步骤 ///

public void UploadImportExcel() { //1.获取上传的⽂件 HttpFileCollection files = ; //2.获取要保存⽂件的路径 string path = h("~/upload/"); //3.判断该路径⽂件夹是否存在 if (!(path)) { //3.1不存在就创建 Directory(path); } //4.获取当前时间戳,⽤来命名确保⽂件名字的唯⼀性 string time = ng(); //5.获取上传⽂件的⽂件名(主要是为了获取后缀名),如果介意新的命名太长可以只获取后缀名 string filename = files[0].FileName; //6.得到完整的命名 string name = path + time + filename; //7.保存 files[0].SaveAs(name); //8.将excel取出转化为datatable var tables = GetExcelSheets(name); var datatable = GetExcelSheet(name, tables[0]); //9.判断datatable中是否有数据 if( < 2) { var item = new { code = 0, msg = "表中没有数据,请检查" }; (izeObject(item)); } //9.2

有数据进⾏的操作 else { InAndOutBll bll = new InAndOutBll(); //因为涉及到多条数据的插⼊,所以需要⽤到事务。 bool res = _Insert(datatable); if(res) { { var item = new { code = 1, msg = "插⼊成功" }; (izeObject(item)); } else { var item = new { code = 0, msg = "插⼊失败" }; (izeObject(item)); } } } #region 获取⼯作簿 ///

///

获取所有的sheet名称 ///

/// /// public static List GetExcelSheets(string path) { List TableNames = new List(); DataSet ds = GetDataForExcel(path); for (int i = - 1; i >= 0; i--) { ([i].TableName); } return TableNames; } /// /// sheetName

获取单个sheet数据

///

/// /// /// public static ble GetExcelSheet(string path, string sheetname) { ble dt = new ble(); DataSet ds = GetDataForExcel(path); for (int i = 0; i < ; i++) { if ([i].TableName == sheetname) { dt = [i]; break; } } return dt; } /// ///

获取整个excel

数据 ///

/// /// public static DataSet GetDataForExcel(string filePath) { using (var stream = (filePath, , )) { { using (var reader = Reader(stream)) { DataSet result = Set(); return result; } } } #endregion public bool IsReusable { get { return false; } } }}4. using System;using c;using ;using ;using ;using Model;using DAL;using ;using DBUtility;namespace BLL{ public class InAndOutBll { private readonly InAndOutDal dal = new InAndOutDal(); /// ///

获取列表 ///

/// 查询条件 /// public List GetList(string condition) { return t(condition); } /// ///

多条数据插⼊ ///

/// 需要插⼊的datatable /// public bool Trans_Insert(DataTable dt) { bool istrue = true; //这⾥的SQLServerHelper是数据库帮助类,⽹上有很多可以⽤ ction transaction = new ction(); //遍历这个表的每⼀⾏ foreach(DataRow dr in ) { if(dr["Column0"].ToString() == "姓名") { continue; } if(istrue) { istrue = _Insert(transaction, dr) > 0; } else { istrue = false; break; } } //所有⾏都成功插⼊,提交事务 if(istrue) { (); } //否则回滚 else { ck(); } return istrue; } }}调试的时候可以看到datatable的结构:5.

using System;using c;using ;using ;using ;using Model;using DBUtility;using ;using ent;namespace DAL{ public class InAndOutDal { ///

///

获取列表,导⼊⾮必须 ///

/// 查询条件 /// public List GetList(string condition) { List list = new List(); string sql = "select * from InAndOut where 1 = 1 " + condition; DataSet ds = (sql); if(ds != null && > 0) { DataTable dt = [0]; if( > 0) { foreach (DataRow dr in ) { (el(dr)); } } } return list; } /// ///

多条数据插⼊ ///

/// 事务 /// 要插⼊的⾏ /// public int Trans_Insert(ction transaction, DataRow dr) { InAndOutModel model = new InAndOutModel(); model = rGetModel(dr); string sql = "insert into InAndOut(name, gender, idcardnum, address, phone) values (@name, @gender, @idcardnum, @address, @phone);select

@@IDENTITY"; SqlParameter[] parameters = { new SqlParameter("@name", ar,10), new SqlParameter("@gender", , 4), new SqlParameter("@idcardnum", ar, 18), new SqlParameter("@address", ar, 20), new SqlParameter("@phone", ar, 12), }; parameters[0].Value = ; parameters[1].Value = ; parameters[2].Value = num; parameters[2].Value = num; parameters[3].Value = s; parameters[4].Value = ; object obj = bject(transaction, sql, parameters); if(obj == null) { return 0; } else { return 32(obj); } } public InAndOutModel GetModel(DataRow dr) { InAndOutModel model = new InAndOutModel(); if (dr != null) { if(dr["name"] != null) { = dr["name"].ToString(); } if(dr["gender"] != null) { = 32(dr["gender"]); } if(dr["idcardnum"] != null) { num = dr["idcardnum"].ToString(); } if(dr["address"] != null) { s = dr["address"].ToString(); } if(dr["phone"] != null) { = dr["phone"].ToString(); } } return model; } public InAndOutModel ExcelDrGetModel(DataRow dr) { InAndOutModel model = new InAndOutModel(); if (dr != null) {//这⾥⽤dr[0]是根据dr结构的,调试可以看到的哦 if (dr[0] != null) { = dr[0].ToString(); } if (dr[1] != null) { if(dr[1].ToString() == "男") { dr[1] = 0; } else { dr[1] = 1; } = 32(dr[1]); } if (dr[2] != null) if (dr[2] != null) { num = dr[2].ToString(); } if (dr[3] != null) { s = dr[3].ToString(); } if (dr[4] != null) { = dr[4].ToString(); } } return model; } }}如果你使⽤了easyUI进⾏数据绑定,那么运⾏会有这样的效果⽹页数据库因为这⾥主要将excel⽂件导⼊数据库,所以很多细节没有处理,⽐如判空、判重等等等,需要⾃⼰添加限制条件哦。

2023年6月21日发(作者:)

C#将excel⽂件导⼊到SqlServer数据库项⽬结构(很标准的三层架构):⼀、做⼀些准备1. 这⾥上传⽂件⽤到的控件是webuploader,下载地址:webuploader的使⽤⽅法:2. 如果需要使⽤到easyUI,下载地址:,使⽤⽅法也在这⾥3. 导⼊ExcelDataReader和et4. 导⼊模板,记得与数据库的内容对应(1)我的excel模板,注意:保留⼀张sheet(2)数据库(其实这⾥是不允许为空的,因为没有对excel表中为空的数据进⾏处理,因为注重的不是这个,所以没有做处理。⾃⼰添加约束即可。)⼆、正式开始注:为了贴图⽅便,顺序可能和实际编写代码的顺序不⼀致1. 下⾯是前端页⾯的代码。标明了导⼊必须的代码以及⾮必须的代码,可以根据需要查看。 导⼊学员

再次提醒!!这⾥和实际编写代码顺序不⼀样,这⾥相当于程序实际运⾏起来的调⽤顺序,所以会有⼀些⼩问题,继续看下去就好啦2. using System;using c;using ;using ;using ;namespace Model{ //和数据库的字段⼀⼀对应 public class InAndOutModel { public int id { get; set; } public string name { get; set; } public int gender { get; set; } public string idcardnum { get; set; } public string address { get; set; } public string phone { get; set; } }}3.

using System;using c;using ;using tion;using ;using Model;using BLL;using ;using ;using ExcelDataReader;namespace 导⼊导出{ ///

/// InAndOut

的摘要说明 ///

public class InAndOut : IHttpHandler { public void ProcessRequest(HttpContext context) { tType = "text/plain"; //这⾥是⽤委托调⽤对应⽅法 var type = t["type"]; if(!OrEmpty(type)) { MethodInfo method = e().GetMethod(type); if(method != null) { (this, null); } } } /// ///

获取列表,⾮必须 ///

public void GetDataList() { InAndOutBll bll = new InAndOutBll(); List list = new List(); list = t(""); list = t(""); if(list != null && > 0) { var item = new { code = 1, list = list }; //这⾥还需要引⼊⼀个⼈Newtonsoft包 (izeObject(item)); } else { var item = new { code = 0, msg = "当前没有数据可以绑定" }; (izeObject(item)); } } /// ///

重要步骤 ///

public void UploadImportExcel() { //1.获取上传的⽂件 HttpFileCollection files = ; //2.获取要保存⽂件的路径 string path = h("~/upload/"); //3.判断该路径⽂件夹是否存在 if (!(path)) { //3.1不存在就创建 Directory(path); } //4.获取当前时间戳,⽤来命名确保⽂件名字的唯⼀性 string time = ng(); //5.获取上传⽂件的⽂件名(主要是为了获取后缀名),如果介意新的命名太长可以只获取后缀名 string filename = files[0].FileName; //6.得到完整的命名 string name = path + time + filename; //7.保存 files[0].SaveAs(name); //8.将excel取出转化为datatable var tables = GetExcelSheets(name); var datatable = GetExcelSheet(name, tables[0]); //9.判断datatable中是否有数据 if( < 2) { var item = new { code = 0, msg = "表中没有数据,请检查" }; (izeObject(item)); } //9.2

有数据进⾏的操作 else { InAndOutBll bll = new InAndOutBll(); //因为涉及到多条数据的插⼊,所以需要⽤到事务。 bool res = _Insert(datatable); if(res) { { var item = new { code = 1, msg = "插⼊成功" }; (izeObject(item)); } else { var item = new { code = 0, msg = "插⼊失败" }; (izeObject(item)); } } } #region 获取⼯作簿 ///

///

获取所有的sheet名称 ///

/// /// public static List GetExcelSheets(string path) { List TableNames = new List(); DataSet ds = GetDataForExcel(path); for (int i = - 1; i >= 0; i--) { ([i].TableName); } return TableNames; } /// /// sheetName

获取单个sheet数据

///

/// /// /// public static ble GetExcelSheet(string path, string sheetname) { ble dt = new ble(); DataSet ds = GetDataForExcel(path); for (int i = 0; i < ; i++) { if ([i].TableName == sheetname) { dt = [i]; break; } } return dt; } /// ///

获取整个excel

数据 ///

/// /// public static DataSet GetDataForExcel(string filePath) { using (var stream = (filePath, , )) { { using (var reader = Reader(stream)) { DataSet result = Set(); return result; } } } #endregion public bool IsReusable { get { return false; } } }}4. using System;using c;using ;using ;using ;using Model;using DAL;using ;using DBUtility;namespace BLL{ public class InAndOutBll { private readonly InAndOutDal dal = new InAndOutDal(); /// ///

获取列表 ///

/// 查询条件 /// public List GetList(string condition) { return t(condition); } /// ///

多条数据插⼊ ///

/// 需要插⼊的datatable /// public bool Trans_Insert(DataTable dt) { bool istrue = true; //这⾥的SQLServerHelper是数据库帮助类,⽹上有很多可以⽤ ction transaction = new ction(); //遍历这个表的每⼀⾏ foreach(DataRow dr in ) { if(dr["Column0"].ToString() == "姓名") { continue; } if(istrue) { istrue = _Insert(transaction, dr) > 0; } else { istrue = false; break; } } //所有⾏都成功插⼊,提交事务 if(istrue) { (); } //否则回滚 else { ck(); } return istrue; } }}调试的时候可以看到datatable的结构:5.

using System;using c;using ;using ;using ;using Model;using DBUtility;using ;using ent;namespace DAL{ public class InAndOutDal { ///

///

获取列表,导⼊⾮必须 ///

/// 查询条件 /// public List GetList(string condition) { List list = new List(); string sql = "select * from InAndOut where 1 = 1 " + condition; DataSet ds = (sql); if(ds != null && > 0) { DataTable dt = [0]; if( > 0) { foreach (DataRow dr in ) { (el(dr)); } } } return list; } /// ///

多条数据插⼊ ///

/// 事务 /// 要插⼊的⾏ /// public int Trans_Insert(ction transaction, DataRow dr) { InAndOutModel model = new InAndOutModel(); model = rGetModel(dr); string sql = "insert into InAndOut(name, gender, idcardnum, address, phone) values (@name, @gender, @idcardnum, @address, @phone);select

@@IDENTITY"; SqlParameter[] parameters = { new SqlParameter("@name", ar,10), new SqlParameter("@gender", , 4), new SqlParameter("@idcardnum", ar, 18), new SqlParameter("@address", ar, 20), new SqlParameter("@phone", ar, 12), }; parameters[0].Value = ; parameters[1].Value = ; parameters[2].Value = num; parameters[2].Value = num; parameters[3].Value = s; parameters[4].Value = ; object obj = bject(transaction, sql, parameters); if(obj == null) { return 0; } else { return 32(obj); } } public InAndOutModel GetModel(DataRow dr) { InAndOutModel model = new InAndOutModel(); if (dr != null) { if(dr["name"] != null) { = dr["name"].ToString(); } if(dr["gender"] != null) { = 32(dr["gender"]); } if(dr["idcardnum"] != null) { num = dr["idcardnum"].ToString(); } if(dr["address"] != null) { s = dr["address"].ToString(); } if(dr["phone"] != null) { = dr["phone"].ToString(); } } return model; } public InAndOutModel ExcelDrGetModel(DataRow dr) { InAndOutModel model = new InAndOutModel(); if (dr != null) {//这⾥⽤dr[0]是根据dr结构的,调试可以看到的哦 if (dr[0] != null) { = dr[0].ToString(); } if (dr[1] != null) { if(dr[1].ToString() == "男") { dr[1] = 0; } else { dr[1] = 1; } = 32(dr[1]); } if (dr[2] != null) if (dr[2] != null) { num = dr[2].ToString(); } if (dr[3] != null) { s = dr[3].ToString(); } if (dr[4] != null) { = dr[4].ToString(); } } return model; } }}如果你使⽤了easyUI进⾏数据绑定,那么运⾏会有这样的效果⽹页数据库因为这⾥主要将excel⽂件导⼊数据库,所以很多细节没有处理,⽐如判空、判重等等等,需要⾃⼰添加限制条件哦。