幽梦紫曦

使用微软本身自带读取Excel数据

电脑版发表于:2019/11/28 16:09

使用步奏:

   1.导入插件using System.Data.OleDb;

   2.获取文件路径

   3.连接字符串

   4.实例化Excel读取连接对象

   5.打开连接对象

   6.实例化Excel操作对象

   7.使用while循环去除数据

示例:

             //获取文件路径

            string url = Server.MapPath("~/Excel/students.xls");

            //连接字符串

            string constr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";Data Source=" + url;

            //实例化Excel读取连接对象

            using (OleDbConnection con = new OleDbConnection(constr))

            {

               //打开连接

                con.Open();

                //实例化Excel操作对象

                using (OleDbCommand com = new OleDbCommand("select * from [Sheet1$]", con))

                {

                    OleDbDataReader reader = com.ExecuteReader();

                    while (reader.Read())

                    {

                        var num = reader[0];

                       var name = reader[1];

                        Response.Write(num + "   " + name + "<br>");

                    }

                }

            }

关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢