zoukankan      html  css  js  c++  java
  • 完美解决Excel的连接字符串与上传方法

    1:第一个连接字符串为上传03的,第2个连接字符串能兼容所有的

     //string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filenameurl + ";extended properties='Excel 8.0;HDR=No;IMEX=1;'";
      string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filenameurl + ";" + "Extended Properties=Excel 8.0;";

    2:导入按钮

     protected void btnExport_Click(object sender,EventArgs e)
            {
                if (FileUpload1.HasFile == false)
                {
                    Response.Write("<script>alert('请您选择Excel文件')</script> ");
                    return;//当无文件时,返回
                }
                string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
                if (IsXls != ".xls" && IsXls != ".xlsx")
                {
                    Response.Write("<script>alert('只可以选择Excel文件')</script>");
                    return;//当选择的不是Excel文件时,返回
                }
                string filename = FileUpload1.FileName;                       //获取Execle文件名
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string FilePath = path + filename;
                if (File.Exists(FilePath))
                    File.Delete(FilePath);

                //FileUpload1.SaveAs(path + filename);
                DataTable ds = ExecleDs(path + filename);
            }

            private DataTable ExecleDs(string filenameurl)
            {
                //string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filenameurl + ";extended properties='Excel 8.0;HDR=No;IMEX=1;'";
                string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filenameurl + ";" + "Extended Properties=Excel 8.0;";
                OleDbConnection conn = new OleDbConnection(strCon);

                OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
                DataTable dt = new DataTable();
                odda.Fill(dt);
                return dt;
            }

  • 相关阅读:
    python 基础(三) list 的用法
    python 基础(二) string 的用法
    python 基础(四)购物车(list类型练习)
    python 基础(一)猜年龄游戏
    centOS 7 环境搭建之安装 python 3
    centOS 7 环境搭建之安装 JDK 8
    源代码管理相关命令(Git常用命令、Nuget常用命令、CMD常用命令)
    .Net Core 3.0 关于Windows Form和WPF的全面支持
    .Net Core 常用开发工具(IDE和运行时、Visual Studio插件、Visual Studio Code插件)
    .Net Core 精选公众号集合(保持更新)
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050621.html
Copyright © 2011-2022 走看看