B/S中Exel导入操作

来源:互联网 发布:centos root密码忘记了 编辑:程序博客网 时间:2024/05/06 01:26

第一:将上传文件保存到内存中

前端

       private Stream ReadPostedFile()
        {
           int intDocLen = fl_path.PostedFile.ContentLength;//文件具体大小
           byte[] docbuffer = new byte[intDocLen];//缓存大小,为上传文件创建缓存数组

           Stream objStream = fl_path.PostedFile.InputStream;

           objStream.Read(docbuffer, 0, intDocLen);

           return objStream;
        }

第二:使用操作Exel的第三方NPOI工具

后台

        /// <summary>
        /// 读取Exel表格中的数据
        /// </summary>
        /// <param name="exelSream">Exel文件流</param>
        /// <returns>tb_AttendanceTable实体数据集合</returns>
        public List<tb_AttendanceTable> ReadExelData(Stream exelSream)
        {
            //创建以后对象
            List<tb_AttendanceTable> lst = new List<tb_AttendanceTable>();

            using (exelSream)//读取文件
            {
                ExelHandleHelp(exelSream, lst);
            }
            return lst;//返回对象集合
        }

        /// <summary>
        /// 返回 List集合对象
        /// </summary>
        /// <param name="exelSream">文件读取流</param>
        /// <param name="lst">tb_AttendanceTable实体类型集合</param>
        private static void ExelHandleHelp(Stream exelSream, List<tb_AttendanceTable> lst)
        {
            tb_AttendanceTable attendanceT = null;
            //创建一个工作簿
            using (Workbook wkbook = new HSSFWorkbook(exelSream))
            {
                for (int i = 0; i < wkbook.NumberOfSheets; i++)
                {
                    using (Sheet sheet = wkbook.GetSheetAt(i))
                    {
                        for (int j = 1; j <= sheet.LastRowNum; j++)//从第二行开始:第一行为标题头
                        {
                            attendanceT = new tb_AttendanceTable();//创建对象实例
                            attendanceT.DB_Option_Action_ = "Insert";

                            Row curRow = sheet.GetRow(j);
                            for (int k = 0; k < curRow.LastCellNum; k++)
                            {
                                switch (k)
                                {
                                    //获得cell中的值
                                    case 0:
                                        attendanceT.DepartmentName = curRow.GetCell(k).StringCellValue;//部门
                                        break;
                                    case 1:
                                        attendanceT.EmployeeName = curRow.GetCell(k).StringCellValue;//姓名
                                        break;
                                    case 2:
                                        attendanceT.AttendNumb = curRow.GetCell(k).StringCellValue;//考勤号
                                        break;
                                    case 3:
                                        attendanceT.AttendDateTime = DateTime.Parse(curRow.GetCell(k).StringCellValue);//日期时间
                                        break;
                                    case 4:
                                        attendanceT.MachineNumb = curRow.GetCell(k).StringCellValue;//机器号
                                        break;
                                    case 5:
                                        attendanceT.SerialNumber = curRow.GetCell(k).StringCellValue;//编号
                                        break;
                                    case 6:
                                        attendanceT.Comparison = curRow.GetCell(k).StringCellValue;//比对方式
                                        break;
                                    case 7:
                                        attendanceT.CardNo = curRow.GetCell(k).StringCellValue;//卡号
                                        break;
                                }
                            }
                            lst.Add(attendanceT);
                        }
                    }
                }
            }
        }

第三:执行对数据的操作

           Ex:导入数据库……

另一种上传Exel文件的方式为:

先保存到服务器,然后读取服务器(本地)文件*.xls;

读数据操作完后 然后删除本地文件;

 

 

注意:

         string path = fl_path.Value;//获取文件的路径 fl_path

         <input type="file" name="name" runat="server" id="fl_path" /> 文件选择控件

        <form id="form1" runat="server" enctype="multipart/form-data" method="post">设置表单的entype、method属性

        <asp:Button ID="Button1" runat="server" Text="数据导入" OnClick="Button1_Click" />放按钮

原创粉丝点击