zoukankan      html  css  js  c++  java
  • 文件_上传_下载

    //单击:下载模板.
        protected void lbtnUploadTemplate_Click(object sender, EventArgs e)
        {
             System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("../upLoad/template/客户需求表.xls"));
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment;filename="+System.Web.HttpUtility.UrlEncode(file.Name,System.Text.Encoding.UTF8));//这样中文名不会出现乱码.
            //Response.AddHeader("Content-Disposition", "attachment;filename=" + file.Name.ToString());
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
            Response.Flush();
            Response.Clear();

            Response.AddHeader("Content-Length", file.Length.ToString());
            
            Response.ContentType = "application/octet-stream";
        
            Response.WriteFile(file.FullName);
            Response.End();
            Response.Flush();
            Response.Clear();
        }



    //单击: 上传Excel表.
        protected void btn_upload_Click(object sender, ImageClickEventArgs e)
        {
            ImportFromExcel ole = new ImportFromExcel();
            string xlsfile = this.file_excel.PostedFile.FileName;

            string resultPath = Server.MapPath("~/upload/template/" + Path.GetFileName(this.file_excel.PostedFile.FileName));

            if (File.Exists(resultPath))
            {
                File.Delete(resultPath);
            }

            try
            {
                file_excel.SaveAs(resultPath);
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + ex.Message.Replace("\'", "\"") + "');", true);
            }


            DataTable dt = null;
            try
            {
                dt = ole.Import(resultPath);
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + ex.Message.Replace("\'", "\"") + "');", true);
                return;
            }

            if (!LoadFileExcel(dt))
            {
                return;
            }
            File.Delete(resultPath);

        }


        //绑定Excel数据;
        public DataTable Import(string xlsPath)
        {

            // 连接字符串
            //string xlsPath = this.file_excel.PostedFile.FileName;//Server.MapPath("~/Excel/客户需求表.xls");

            if (xlsPath == "")
            {
                return null;
            }
            // 相对物理路径:        
            string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=\"Excel 8.0;IMEX=1;HDR=NO;\";" + "data source=" + xlsPath;
            /**
             * 用于 Extended Properties 值的有效 Excel 版本。
             * 对于 Microsoft Excel 8.0 (97)、9.0 (2000) 和 10.0 (2002) 工作簿,请使用 Excel 8.0。
             * 对于 Microsoft Excel 5.0 和 7.0 (95) 工作簿,请使用 Excel 5.0。
             * 对于 Microsoft Excel 4.0 工作簿,请使用 Excel 4.0。
             * 对于 Microsoft Excel 3.0 工作簿,请使用 Excel 3.0。
             **/
            // 查询语句
            string sql = "SELECT * FROM [Sheet1$]";

            if (xlsPath == "")
            {
                throw new Exception("文件路径不能为空!");
            }

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

            DataSet ds = null;
            try
            {
                using (OleDbDataAdapter da = new OleDbDataAdapter(sql, connStr))
                {
                    ds = new DataSet();
                    da.Fill(ds); // 填充DataSet  
                }
            }
            catch (Exception ex)
            {
                throw new Exception("上传的Excel文件被占用,请保存或关闭这个文件!", ex);//抛出的是用户自定义的异常信息.
            }
            return ds.Tables[0];
        }

        //验证后缀名是否为: ".xls"
        public bool ValidateExtension(string fileName)
        {
            string fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
            if (fileExtension == ".xls")
                return true;
            else
                return false;
        }
  • 相关阅读:
    第四周
    第二次作业
    jsp
    软件测试第一次
    增删改查
    用户添加
    登录
    购物商城
    jsp第七周作业
    jsp第六周作业
  • 原文地址:https://www.cnblogs.com/MySpace/p/1599726.html
Copyright © 2011-2022 走看看