net以execl做数据库_ASP.NET关于对excel数据导入到数据库

1 //*******************************C#(UI代码)********************************/

2 Web界面上放一个FileUpload 名称为:FileUpload1, 一个button按钮,名称为button13 程序中应用的两个外部引用为:Ionic.Zip.dll 和 NPOI.dll ; Ionic.Zip.dll可以不引用,单独的NPOI.dll也是可以的4 //*********************************C#(UI代码)****************************/

5 namespaceWebApp.BaseData6 {7 public partial classTest : System.Web.UI.Page8 {9 public string strPath; /*strPath这个变量用来存储上传上来的文件名*/

10 protected void Button1_Click(objectsender, EventArgs e)11 {12 if (FileUpload1.FileName != "") /*1判断用户是否添加了文件*/

13 {14 strPath = FileUpload1.FileName; /*1获取文件名*/

15 string MapPath = Server.MapPath("../bin/"); /*1更改路径*/

16 FileUpload1.SaveAs(MapPath +FileUpload1.FileName);17 #region MyRegion

18 using (FileStream file = new FileStream(MapPath + strPath, FileMode.Open, FileAccess.Read))//1以文件流的形式打开文件

19 {20 using (Workbook wk = new HSSFWorkbook(file))/*1打开工作薄*/

21 {22 using (Sheet sheet = wk.GetSheetAt(0))23 {24 for (int j = 1; j <= sheet.LastRowNum; j++)//1循环行数量 j=1表示不要第一列,因为第一列是字段的名字

25 {26 Row row = sheet.GetRow(j);//1获取行

27 int length = row.LastCellNum; 1这条语句,在本程序中,没有作用,可以删除,仅做调试使用

28 List list = new List();29 for (int i = 0; i < row.LastCellNum; i++)30 {31 Cell cell = row.GetCell(i);//1获取单元格里的数据

32 if (cell != null)//1如果单元格里有数据

33 {34 list.Add(cell.ToString());//则将这个单元格里的数据添加到List集合里;

35 }36 }37 ArrayList array = new ArrayList(); //1创建一个Array数组

38 for (int k = 0; k < list.Count; k++)//1循环读取List集合里的一行数据,本程序是读一行,插入一行

39 {40 array.Add(list[k].ToString());//1将List集合里的一行数据,放到Array数组里,方便作为参数传递;

41 }42 BLL.AL.BLL_Test bll = newBLL.AL.BLL_Test();43 int num = bll.InsertDate(array);//1调用BLL的Test(参数)方法,将一行数据传过去

44 }45 }46 }47 }48 #endregion

49 }50 else

51 {52 Response.Write("");53 }54 }55 }56 }57

58

59 //*****************************************************C#(BLL代码)***************************************************/

60 DAL.AL.DAL_Test dal = newDAL.AL.DAL_Test();61 public intInsertDate(ArrayList array)62 {63 return dal.InsertDate(array);//将一行数据传给DAL

64 }65 //*****************************************************C#(DAL代码)***************************************************/

66 public intInsertDate(ArrayList array)67 {68 string sql = "insert into Test(T_ProdDate,T_Paln,T_Prod) values(@T_ProdDate,@T_Paln,@T_Prod)";//写插入的SQL语句

69 SqlParameter[] parameter ={ //1参数,这里面写的有点死

70 new SqlParameter("@T_ProdDate",array[0]), //1,我的数据库里只有三列数据

71 new SqlParameter("@T_Paln",array[1]),72 new SqlParameter("@T_Prod",array[2])73 };74 returnUtility.SqlHelper.ExecuteNonQuery(sql, parameter);75 }76 //*****************************************************C#(Excel里面的样式,将下面的数据复制到EXCEL表格里面)***************************************************/

77 时间 计划 实际78 2013-7-20 15:30 20 20

79 2013-7-21 15:30 21 21

80 2013-7-22 15:30 22 22

81 2013-7-23 15:30 23 23

82 2013-7-24 15:30 24 24

83 2013-7-25 15:30 25 25

84 2013-7-26 15:30 26 26

85 2013-7-27 15:30 27 27

86 2013-7-28 15:30 28 28

87 2013-7-29 15:30 29 29

88 2013-7-30 15:30 30 30

89 2013-7-31 15:30 31 31

90 2013-8-1 15:30 32 32

91 2013-8-2 15:30 33 33

92 2013-8-3 15:30 34 34

93 2013-8-4 15:30 35 35

94 2013-8-5 15:30 36 36

95 2013-8-6 15:30 37 37

96 2013-8-7 15:30 38 38

97 2013-8-8 15:30 39 39

98 2013-8-9 15:30 40 40

99

100 //*****************************************************C#(SQL Server 数据表结构)***************************************************/

101 create table Test102 (103 T_ID INT identity(1,1) primary key, //1序号

104 T_ProdDate varchar(30) not null, //1生产日期

105 T_Paln varchar(5) not null, //1计划产量

106 T_Prod varchar(5) not null, //1实际产量

107 T_IsDel CHAR(1) default '0' //1删除标志

108 )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值