博客
关于我
强烈建议你试试无所不能的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/

你可能感兴趣的文章
D2007在64位Win7出现 delphi 2007 assertion failure thread32.cpp 的解决办法
查看>>
STM32的TAMPER-RTC管脚作为Tamper的使用[转]
查看>>
[记]一个逐步“优化”的范例程序
查看>>
2012-01-09_2
查看>>
数学 - 线性代数导论 - #5 矩阵变换之置换与转置
查看>>
java数据结构:队列
查看>>
IPsec工具之ipsec-tools
查看>>
jmeter文件下载
查看>>
Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_labels(self, locator)...
查看>>
ASP.NET 中执行 URL 重写
查看>>
JS从定义到执行
查看>>
第七周学习报告
查看>>
10分钟学会在Ubuntu 18.04 LTS上安装NFS服务器和客户端
查看>>
Shell编程笔记
查看>>
更新Xcode7 后 .dylib变成了.tbd的问题解决
查看>>
hdu 多校
查看>>
Oracle定時email通知
查看>>
费用登记系统(开发小结)
查看>>
python去除列表最小的元素
查看>>
ubuntu12.10安装jdk1.7
查看>>