• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

完整ASP.NetExcel导入程序(支持2007)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

   //把EXCEL文件上传到服务器并返回文件路径
        private String typename(FileUpload fileloads)
        {
            string fullfilename = fileloads.PostedFile.FileName;
            string filename = fullfilename.Substring(fullfilename.LastIndexOf("\\") + 1);
            string type = fullfilename.Substring(fullfilename.LastIndexOf(".") + 1);
            string murl = "";
            if (type == "xls" || type == "xlsx")
            {
                fileloads.PostedFile.SaveAs(Server.MapPath(filename));
                //fileloads.PostedFile.SaveAs(Server.MapPath(Tools.GetAppSettings("ExcelPath")) + "\\" + filename);
                murl = Server.MapPath(filename);
            }
            else
            {
                Tools.ShowMsg("导入文件格式不对!");
                //Response.Write("<script language='javascript'>alert('导入文件格式不对!');</script>");
            }
            return murl;
        }

        public DataTable ImportToDataSet(string path)
        {
            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "Data Source=" + path + ";" + "Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';";

            OleDbConnection conn = new OleDbConnection(strConn); 
            try
            { 
                DataTable dt = new DataTable(); 
                if (conn.State != ConnectionState.Open) 
                    conn.Open(); 
                string strExcel = "select * from [Sheet1$]"; 
                OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, conn); 
                adapter.Fill(dt); 
                return dt; 
            } 
            catch (Exception ex) 
            { 
                throw new Exception(ex.Message); 
            } 
            finally
            { 
                if (conn.State != ConnectionState.Closed) 
                    conn.Close(); 
            } 
        } 

        protected void btnImport_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = ImportToDataSet(typename(selectfile));
                int j = 0;
                bool sec = true;
               
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ShippingCharge sc = new ShippingCharge();
                    string RegionID = dt.Rows[i][1].ToString();
                    if (!RegionID.IsNullOrEmpty() && Service.GetShippingRegion(dt.Rows[i][1].ToString()) != null)
                    {
                        sc.RegionID = Service.GetShippingRegion(dt.Rows[i][1].ToString()).RegionID;
                    }

                    sc.Status = Convert.ToByte(dt.Rows[i][2].ToString()=="否"?0:1);

                    sec = Service.AddShippingCharge(sc);
                    if (sec == false)
                    {
                        j++;
                    }
                }
               
                if(j > 0)
                {
                    Tools.ShowMsg("导入文件失败,请确认文件格式再重试一次!");
                }
                else
                {
                     //关闭,删除 文件 
                    string filepath = Server.MapPath(selectfile.PostedFile.FileName);
                    if (File.Exists(filepath))
                        File.Delete(filepath); 
                    Tools.ShowMsg("导入文件成功!", "ShippingCharges_List.aspx");
                }
            }
            catch (Exception ex)
            {
                Tools.ShowMsg("导入文件失败,请确认文件格式再重试一次!");

            }
        }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.net运行原理发布时间:2022-07-10
下一篇:
【转】Asp.net(C#)对文件操作的方法(读取,删除,批量拷贝,删除...) ...发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap