博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读入excel中的数据到数据库中
阅读量:6303 次
发布时间:2019-06-22

本文共 11170 字,大约阅读时间需要 37 分钟。

   项目中用到大量的导入数据,比如导入订单,导入供应商,导入用户等等, 现以导入供应商为例:

页面如下:

 

代码如下:

页面后台
1  public partial class ImportSupplyInfo : System.Web.UI.Page  2     {  3         protected void Page_Load(object sender, EventArgs e)  4         {  5   6         }  7         public DataTable GetExcelData(string filePath)  8         {  9             string oledbString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;'", filePath); 10             using (OleDbConnection con = new OleDbConnection(oledbString)) 11             { 12                 con.Open(); 13                 DataTable dtTableNames = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 14                 if (dtTableNames == null || dtTableNames.Rows.Count <= 0) 15                 { 16                     return new DataTable(); 17                 } 18                 DataSet ds = new DataSet(); 19                 string oledbSql = "SELECT * FROM [" + dtTableNames.Rows[0]["TABLE_NAME"].ToString().Trim() + "]"; 20                 OleDbDataAdapter da = new OleDbDataAdapter(oledbSql, con); 21                 da.Fill(ds); 22                 if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0) 23                 { 24                     return new DataTable(); 25                 } 26                 return ds.Tables[0]; 27             } 28         } 29         public int ValidateExcelFile(string filename) 30         { 31             string extentsion = Path.GetExtension(filename); 32             if (extentsion.ToLower() != ".xls") 33             { 34                 return 1; 35             } 36             return 0; 37         } 38         protected void btnSave_Click(object sender, EventArgs e) 39         { 40             try 41             { 42                 if (!this.fileupload1.HasFile) 43                 { 44                     string script = "请选择上传文件"; 45                     ScriptManager.RegisterStartupScript(Page, typeof(Page), DateTime.Now.ToString(), script, true); 46                     return; 47                 } 48                 string fileFullName = this.fileupload1.PostedFile.FileName; 49                 int fileState = ValidateExcelFile(fileFullName); 50                 if (fileState != 0) 51                 { 52                     string script = "上传的文件格式错误"; 53                     ScriptManager.RegisterStartupScript(Page, typeof(Page), DateTime.Now.ToString(), script, true); 54                     return; 55                 } 56                 string extension = Path.GetExtension(fileFullName); 57                 string serverPath = Server.MapPath(@"~\upload\") + DateTime.Now.ToString("yyyyMMddHHmmss") + extension; 58                 this.fileupload1.SaveAs(serverPath); 59                 DataTable dt = GetExcelData(serverPath); 60                 if (dt == null || dt.Rows.Count <= 0) 61                 { 62                     string script = "上传文件数据不能为空"; 63                     ScriptManager.RegisterStartupScript(Page, typeof(Page), DateTime.Now.ToString(), script, true); 64                     return; 65                 } 66                 for (int i = 0; i < dt.Rows.Count; i++) 67                 { 68                     if (string.IsNullOrEmpty(dt.Rows[i]["供应商名称"].ToString().Trim()) && (!string.IsNullOrEmpty(dt.Rows[i]["法人姓名"].ToString().Trim()) || !string.IsNullOrEmpty(dt.Rows[i]["注册资本"].ToString().Trim()) || !string.IsNullOrEmpty(dt.Rows[i]["注册号"].ToString().Trim()) || !string.IsNullOrEmpty(dt.Rows[i]["地址"].ToString().Trim()))) 69                     { 70                         string script = "供应商名称不能为空"; 71                         ScriptManager.RegisterStartupScript(Page, typeof(Page), DateTime.Now.ToString(), script, true); 72                         return; 73                     } 74                 } 75                 NeoBLL.SupplyInfoBLL supplyBll = new NeoBLL.SupplyInfoBLL(); 76                 for (int i = 0; i < dt.Rows.Count; i++) 77                 { 78                     if (string.IsNullOrEmpty(dt.Rows[i]["供应商名称"].ToString().Trim())) 79                     { 80                         continue; 81                     } 82                     NeoModel.SupplyInfoModel model = new NeoModel.SupplyInfoModel(); 83                     string supplyName = dt.Rows[i]["供应商名称"].ToString().Trim(); 84                     string leader = dt.Rows[i]["法人姓名"].ToString().Trim(); 85                     string money = dt.Rows[i]["注册资本"].ToString().Trim(); 86                     string cart = dt.Rows[i]["注册号"].ToString().Trim(); 87                     string address = dt.Rows[i]["地址"].ToString().Trim(); 88                     model.SUPPLYNAME = supplyName; 89                     model.LEGALPERSON = leader; 90                     model.REGISTERCARD = cart; 91                     model.ADDRESS = address; 92                     model.REGISTERCAPITAL = money; 93                     supplyBll.AddSupplyInfo(model); 94                 } 95                 string scrip1 = "供应商信息导入成功"; 96                 Page.RegisterStartupScript("", ""); 97             } 98             catch (Exception ex) 99             {100                //log.Error(ex.Message);101             }102         }103     }
BLL
1 public  class SupplyInfoBLL 2     { 3        NeoDAL.SupplyInfoDAL dal = new NeoDAL.SupplyInfoDAL(); 4        public SupplyInfoBLL() 5        { 6        } 7        public DataTable GetSupplyInfos() 8        { 9            return dal.GetSupplyInfos();10        }11        public bool AddSupplyInfo(NeoModel.SupplyInfoModel model)12        {13            return dal.AddSupplyInfo(model);14        }15     }
DAL
1  public class SupplyInfoDAL 2     { 3  4         public SupplyInfoDAL() 5         { 6         } 7  8         public DataTable GetSupplyInfos() 9         {10             string sql = "SELECT * FROM SupplyInfo";11             DataSet ds = DbHelperSQL.Query(sql);12             if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)13             {14                 return new DataTable();15             }16             return ds.Tables[0];17         }18         public bool AddSupplyInfo(NeoModel.SupplyInfoModel model)19         {20             try21             {22                 string sql = "  INSERT INTO SupplyInfo(SUPPLYNAME,LEGALPERSON,REGISTERCAPITAL,REGISTERCARD,[ADDRESS]) VALUES(@SUPPLYNAME,@LEGALPERSON,@REGISTERCAPITAL,@REGISTERCARD,@ADDRESS)";23                 SqlParameter[] sp = { 24                                 new SqlParameter("@SUPPLYNAME",model.SUPPLYNAME),25                                 new SqlParameter("@LEGALPERSON",model.LEGALPERSON),26                                 new SqlParameter("@REGISTERCAPITAL",model.REGISTERCAPITAL),27                                 new SqlParameter("@REGISTERCARD",model.REGISTERCARD),28                                 new SqlParameter("@ADDRESS",model.ADDRESS)29                                 };30                 return DbHelperSQL.ExecuteSql(sql, sp) > 0;31             }32             catch (Exception ex)33             {34                 return false;35             }36         }37     }
model
1 public class SupplyInfoModel 2     { 3  4         public SupplyInfoModel() 5         { 6  7         } 8         private int _id; 9         public int ID10         {11             get { return _id; }12             set { _id = value; }13         }14         private string _SUPPLYNAME;15         public string SUPPLYNAME16         {17             get { return _SUPPLYNAME; }18             set { _SUPPLYNAME = value; }19         }20         private string _LEGALPERSON;21         public string LEGALPERSON22         {23             get { return _LEGALPERSON; }24             set { _LEGALPERSON = value; }25         }26         private string _REGISTERCAPITAL;27         public string REGISTERCAPITAL28         {29             get { return _REGISTERCAPITAL; }30             set { _REGISTERCAPITAL = value; }31         }32         private string _REGISTERCARD;33         public string REGISTERCARD34         {35             get { return _REGISTERCARD; }36             set { _REGISTERCARD = value; }37         }38         private string _ADDRESS;39         public string ADDRESS40         {41             get { return _ADDRESS; }42             set { _ADDRESS = value; }43         }44         private string _CONTACTTEL;45         public string CONTACTTEL46         {47             get { return _CONTACTTEL; }48             set { _CONTACTTEL = value; }49         }50         private string _PRODUCTS;51         public string PRODUCTS52         {53             get { return _PRODUCTS; }54             set { _PRODUCTS = value; }55         }56         private string _ISBLACK;57         public string ISBLACK58         {59             get { return _ISBLACK; }60             set { _ISBLACK = value; } 61         }62         private string _APPRAISE;63         public string APPRAISE64         {65             get { return _APPRAISE; }66             set { _APPRAISE = value; }67         }68     }

页面前台:

View Code
1 
2
3 供应商管理 4
5
6
7
8
12
13
14
15 <%#((System.Data.DataRowView)Container.DataItem)["SUPPLYNAME"]%>16
17
18
19
20
21 <%#((System.Data.DataRowView)Container.DataItem)["LEGALPERSON"]%>22
23
24
25
26
27 <%#Eval("REGISTERCAPITAL") %>28
29
30
31
32
33 <%#Eval("ADDRESS")%>34
35
36
37
38
39
" title="编辑">40 编辑41 42
45
46
47
48
49
50
51
52
53
54 55 56 供应商57 58 59 法人代表60 61 62 注册资本63 64 65 地址66 67 68 操作69 70 71 72 73
当前没有查询记录74 75 76
77
78

 

转载地址:http://acfxa.baihongyu.com/

你可能感兴趣的文章
Bluedroid与BluZ,蓝牙测试方法的变动(基于bludroid和BlueZ的对比)
查看>>
接口和抽象类有什么区别
查看>>
Linux 下添加用户,修改权限
查看>>
请问view controller scene,该如何删除
查看>>
bootstrap新闻模块样式模板
查看>>
zzzzw_在线考试系统①准备篇
查看>>
App Store 审核被拒的23个理由
查看>>
剑指offer第二版-1.赋值运算符函数
查看>>
javascript 对象
查看>>
Android学习笔记——文件路径(/mnt/sdcard/...)、Uri(content://media/external/...)学习
查看>>
Echart:前端很好的数据图表展现工具+demo
查看>>
CATransform3D iOS动画特效详解
查看>>
Linux VNC黑屏(转)
查看>>
Java反射简介
查看>>
react脚手架应用以及iview安装
查看>>
shell学习之用户管理和文件属性
查看>>
day8--socket网络编程进阶
查看>>
node mysql模块写入中文字符时的乱码问题
查看>>
仍需"敬请期待"的微信沃卡
查看>>
分析Ajax爬取今日头条街拍美图
查看>>