简体   繁体   English

Asp.Net(c#) - 阅读Excel表格

[英]Asp.Net (c#) - Read Excel Sheet

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. 我需要从Excel电子表格中进行一些导入,并且一直在查看Web上的各种示例,它们似乎都使用与64Bit不兼容的“Jet”驱动程序。

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. 现在我完全了解可用的解决方法(更改IIS的运行方式等)但是我想知道是否有替换“Jet”驱动程序,以便我可以从64位服务器上运行的Asp.Net读取和生成Excel工作表没有IIS修改。

The last time I researched there isnt an x64 driver. 我最后一次研究没有x64驱动程序。 I use this to open xls files. 我用它来打开xls文件。 It works on 64bit boxes so long as you compile using x86. 只要您使用x86进行编译,它就可以在64位盒子上运行。

 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;

http://npoi.codeplex.com http://npoi.codeplex.com

I use it myself on a 64bit server with IIS 6.0 Not the easiest one to use, but it works fine (fast, reliable, don't need office installed on server, etc). 我自己在使用IIS 6.0的64位服务器上使用它并不是最容易使用的,但它工作正常(快速,可靠,不需要在服务器上安装办公室等)。

您是否已经考虑过尝试使用Open XML SDK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM