下载excel模板

来源:互联网 发布:手机远程拍照软件 编辑:程序博客网 时间:2024/04/26 18:28

 下载事件中写

 Page.ClientScript.RegisterStartupScript(GetType(), "alert", "alert('测试成功!');", true);

            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("../fellowship/template.xls"));//在项目的位置下放个Excel
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment;   filename=" + file.Name);
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
            Response.Flush();
            Response.Clear();

 

导入excel内容事件写这个方法

        private void uploadOperation()
        {
            //string fileName = "";

            // fileName = Request.QueryString["fileName"].Trim();
            if (!this.fileUpload.FileName.Substring(this.fileUpload.FileName.LastIndexOf(".")).ToUpper().Equals(".XLS"))
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "alert", "alert('只能导入Excel文件!')", true);
                return;

            }

            // ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updatepanel2", "alert('测试成功!');", true);
            string oldfileName = this.fileUpload.FileName;
            string newfileName = JinRi.Logic.GroupTicket.get_OrderNo(base.authorInfo.UserId) + ".xls";

            this.fileUpload.SaveAs(Server.MapPath(@"~/fellowship/" + newfileName + ""));
            StringBuilder xmlnode = new StringBuilder();

            DataTable dt = selectExcel(newfileName);
            if (dt != null)
            {
                Session["uploadFileName"] = newfileName;
            }
            else
            {
                Session["uploadFileName"] = null;
            }


        }

 

 

   private DataTable selectExcel(string fileName)
        {

            // 连接字符串
            string xlsPath = Server.MapPath("~/fellowship/" + fileName + "");

            if (xlsPath == "")
            {
                return null;
            }
            // 相对物理路径:       
            string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=/"Excel 8.0;IMEX=1;HDR=NO;/";" + "data source=" + xlsPath;

            // 查询语句
            string sql = "SELECT * FROM [Sheet1$]";


            if (!System.IO.File.Exists(xlsPath))
            {
                throw new Exception("文件不存在!");
            }

            DataSet ds = new DataSet();

            OleDbDataAdapter da = new OleDbDataAdapter(sql, connStr);
            try
            {
                da.Fill(ds); // 填充DataSet

                if (ds.Tables[0].Rows.Count > 501)
                {
                    this.txtUpload.Value = "error1";

                }
                else if (ds.Tables[0].Columns.Count != 2)
                {
                    this.txtUpload.Value = "error2";

                }
                else
                {
                    this.txtUpload.Value = "yes";

                }


            }
            catch (Exception)
            {
                return null;
                //throw new Exception(ex.Message);//则抛出的是系统自己的异常信息.

                //throw new UploadExeception("上传的Excel文件被占用,请关闭这个文件!");//抛出的是用户自定义的异常信息.
            }


            // 在这里对DataSet中的数据进行操作
            // 输出,绑定数据
            return ds.Tables[0];

        }

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 赌博欠了十几万怎么办 欠了网贷跑了会怎么办 欠银行钱还不起怎么办 欠小额贷款公司的钱还不上怎么办 负债30万没工作怎么办 华为p9耗电太快怎么办 酷派手机反应慢怎么办 网上买手机被骗了怎么办 买手机贵了怎么办12315 在手机店被骗了怎么办 在转转上被骗了怎么办 微信被骗了800怎么办 苹果6被偷走该怎么办 苹果7被偷走该怎么办 月经量突然少了怎么办 我月经老是不来怎么办 月经不来3个月怎么办 月经推迟3个月怎么办 乙肝五项25阳怎么办 乙肝五项25阳性怎么办 流浓鼻涕怎么办速效办法 鼻涕又浓又黄绿怎么办 玉露已经徒长了怎么办 多肉长出了多头怎么办 多肉已经徒长了怎么办 脖子抻筋了 很疼怎么办 声带肥大造成说话声音沙哑怎么办 轻微的骨性龅牙怎么办 19岁没了晨勃怎么办 早上晨勃消失了怎么办 阴茎勃而不坚怎么办 一个月婴儿痰多怎么办 孕妇咳绿色浓痰怎么办 买的戒指大了怎么办 孩子鞋子买大了怎么办 鞋子不大但掉跟怎么办 鞋子长了一码怎么办 新鞋有点顶脚怎么办 宝宝鞋子买小了怎么办 鞋子买的小了怎么办 粉底液颜色偏黄怎么办