• Asp.Net 导入Excel自动获取表名


    public static DataSet ReadExcel(string Path, string fileType)
        {
            //服务器需要安装驱动
            //http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/AccessDatabaseEngine.exe
            //try
            //{
                //string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Path + ";" + ";Extended Properties="Excel 12.0;HDR=YES;IMEX=2"";
    
                string connectionString = "";
                if (fileType == "xls")
                {
                    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1"";
                }
                else
                {
                    connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";;Extended Properties="Excel 12.0;HDR=Yes;IMEX=1"";
                }
                OleDbConnection connection = new OleDbConnection(connectionString);
                connection.Open();
                string selectCommandText = "";
                OleDbDataAdapter adapter = null;
                DataSet dataSet = null;
    
                System.Data.DataTable sTable = connection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
                string tableName = sTable.Rows[0][2].ToString().Trim();//默认表名Sheet1,如果修改了则无法导入,所以需要动态获取表面
                //selectCommandText = "select * from [Sheet1$]";//默认表名Sheet1
                selectCommandText = "select * from [" + tableName + "]";
                adapter = new OleDbDataAdapter(selectCommandText, connectionString);
                dataSet = new DataSet();
                adapter.Fill(dataSet, "table1");
                connection.Close();
                connection.Dispose();
                return dataSet;
            //}
            //catch
            //{
            //    return null;
            //}
        }
  • 相关阅读:
    一次性删除 .svn 文件夹
    vim
    03 练习题:字符串
    04 练习题:元组、列表
    05 练习题:字典
    Python基础学习笔记(07)基础类型操作及转化、编码
    Python基础学习笔记(06)代码块、集合、深浅copy
    Python基础学习笔记(05)字典
    Python基础学习笔记(04)列表、元组
    67.滚轮事件
  • 原文地址:https://www.cnblogs.com/webapi/p/10470035.html
Copyright © 2020-2023  润新知