Asp.Net(C#)-阅读Excel表格 [英] Asp.Net (c#) - Read Excel Sheet

查看:68
本文介绍了Asp.Net(C#)-阅读Excel表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Excel电子表格进行一些导入,并且一直在网上查看各种示例,它们似乎都使用与64Bit不兼容的"Jet"驱动程序.

I have a requirement to do some imports from an Excel spread sheet and have been looking at various examples on the web, they all seem to use the "Jet" driver which is not compatible with 64Bit.

现在,我完全知道可用的解决方法(更改IIS的运行方式等),但是我想知道是否有"Jet"驱动程序的替代品,因此我可以从正在运行的Asp.Net读取并生成excel工作表没有IIS修改的64位服务器.

Now I am fully aware of the workarounds available (in changing how IIS runs etc) however I would like to know if there is replacement for the "Jet" driver so I can read and generate excel sheets from Asp.Net running on a 64Bit server with no IIS modifications.

推荐答案

我上次研究那里没有x64驱动程序.我用它来打开xls文件.只要您使用x86进行编译,它就可以在64位盒上运行.

The last time I researched there isnt an x64 driver. I use this to open xls files. It works on 64bit boxes so long as you compile using x86.

 DataSet myDataset = new DataSet();
                string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + @";Extended Properties=""Excel 12.0 Xml;HDR=YES""";
                OleDbConnection myData = new OleDbConnection(strConn);
                try {
                                      myData.Open();
                }
                catch (OleDbException e) {
                    try {
                        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + "Extended Properties=Excel 8.0;HDR=YES;";
                        myData = new OleDbConnection(strConn);
                        myData.Open();
                    }
                    catch (Exception e2) {
                        strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + @";Extended Properties=""HTML Import;HDR=YES;IMEX=1"";";
                        myData = new OleDbConnection(strConn);
                        myData.Open();
                    }
                }

                int i = 0;
                foreach (DataRow row in myData.GetSchema("tables").Rows)
                    try {
                        i++;
                        string name = row[2].ToString().Replace("''", "'").TrimEnd('_');
                        DataSet ds = new DataSet();
                        OleDbDataAdapter d = new OleDbDataAdapter("SELECT * from [" + name + "]", strConn);
                        d.Fill(ds);
                        DataTable dt = ds.Tables[0].Copy();
                        dt.Namespace = name;
                        myDataset.Tables.Add(dt);
                    }
                    catch (Exception e) {
                    }
                return myDataset;

这篇关于Asp.Net(C#)-阅读Excel表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆