简体   繁体   English

在服务器上读取 Excel 文件

[英]Reading Excel file on the server

I have asp.net application which loads excel file to read.我有 asp.net 应用程序加载 excel 文件读取。 It works fine on my local development server.它在我的本地开发服务器上运行良好。 However when I upload it to server it wont display the uploaded excel file.但是,当我将其上传到服务器时,它不会显示上传的 excel 文件。 I have MS Office installed on my local machine but not on server.我在本地计算机上安装了 MS Office,但没有在服务器上安装。 Thanks for your guidance感谢您的指导

The code I am using is...我使用的代码是...

if (fileExtension == ".xls")
            {
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; 
            }
            else if (fileExtension == ".xlsx")
            {
                connectionString = "Provider=Microsoft.ACE.OLEDB.14.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 14.0;HDR=Yes;IMEX=2\"";
            }
            //Create OleDB Connection and OleDb Command
            OleDbConnection con = new OleDbConnection(connectionString);
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = con;
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
            DataTable dtExcelRecords = new DataTable();
            con.Open();
            DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
            cmd.CommandText = "SELECT * FROM [" + getExcelSheetName +"]";
            dAdapter.SelectCommand = cmd;
            dAdapter.Fill(dtExcelRecords);
            con.Close();
            GridView1.DataSource = dtExcelRecords;
            GridView1.DataBind();

I keep getting error我不断收到错误

  " The 'Microsoft.ACE.OLEDB.14.0' provider is not registered on the local machine."

Even though I installed Access db and I can see ACE 14.0 still I keep getting this error即使我安装了 Access db 并且可以看到 ACE 14.0,我仍然不断收到此错误

Depending on the features of Excel you need you'll need to install Excel on server or the client, or a third party control(s) that understand the format. 根据Excel的功能,您需要在服务器或客户端或者了解格式的第三方控件上安装Excel。

  1. If the application is WEB application where the application actually showing the data locally on the client via a web brower : See this MS link : http://support.microsoft.com/kb/162059 . 如果应用程序是WEB应用程序,其中应用程序通过Web浏览器在客户端本地显示数据:请参阅此MS链接: http//support.microsoft.com/kb/162059 Here you're actually sending the xls file/stream to the client. 在这里,您实际上是将xls文件/流发送到客户端。

  2. If you are showing it a WEB application, and shows data on the web page where the rendered content is sent to the client then you need Excel Services on the server (I think this is part of the SharePoint family), or a third party AS.NET control that ready .XLS info and renders it into the page. 如果您将其显示为WEB应用程序,并在网页上显示已呈现内容发送到客户端的数据,那么您需要在服务器上使用Excel Services (我认为这是SharePoint系列的一部分),或者是第三方AS .NET控件准备好.XLS信息并将其呈现到页面中。

  3. If the application is a desktop application that shows excel data in stand alone app where the user logs into the server as a desktop user then you need to have Excel installed on the server. 如果应用程序是在独立应用程序中显示excel数据的桌面应用程序,其中用户以桌面用户身份登录到服务器,则需要在服务器上安装Excel。 Or you need third party .net control (dependant on app technology WinForms, WFP/Silverlight etc) that can read the .XLS info and present it. 或者您需要第三方.net控件(取决于应用程序技术WinForms,WFP / Silverlight等),它们可以读取.XLS信息并显示它。

  4. If the application is running on a virtual desktop, say like Citrix, then it's the same as 3. 如果应用程序在虚拟桌面上运行,例如Citrix,则它与3相同。

  5. If all you're doing is reading the files via OLEDB and then working with the data outside of excel then you can install the Access Connectivity Engine for free. 如果您所做的只是通过OLEDB读取文件,然后使用excel外部的数据,那么您可以免费安装Access连接引擎 This basically the components needed to read various Office data in a need 'service only' package (or driver if you prefer). 这基本上是在需要“仅服务”包(或者您喜欢的驱动程序)中读取各种Office数据所需的组件。 I don't if allows random access at the cell level, but I know it does allow you to run OLEDB queries over the .XLS file. 我不允许在单元级别进行随机访问,但我知道它确实允许您通过.XLS文件运行OLEDB查询。 We use for loading .XLS files into databases. 我们用于将.XLS文件加载到数据库中。

All of these come with different licensing restrictions and costs. 所有这些都有不同的许可限制和成本。 And without any of your use cases, all I can suggest is that you consider that Excel is a chunky set of application components and to use them you need to understand exactly how your application and they will function/interact and also their limitations in any particular deployment scenario you envisage. 在没有任何用例的情况下,我可以建议的是,您认为Excel是一组庞大的应用程序组件并使用它们,您需要准确了解您的应用程序和它们的功能/交互方式以及它们在任何特定方面的局限性您设想的部署方案。

Edit: Further info: If you are running on a 64 server and are using a 32 bit application then the only way I could ACE to work was to install the ACE 2007 and not 2010 version 编辑:更多信息:如果您在64服务器上运行并使用32位应用程序,那么ACE能够工作的唯一方法是安装ACE 2007而不是2010版本

If you just need to read file content you can use Csv format, wich I think will be the easiest way you can read excel files in your server. 如果您只需要阅读文件内容,可以使用Csv格式,我认为这是您在服务器中读取Excel文件的最简单方法。 you can 'save as' a excel file into *.csv. 你可以'保存为'excel文件到* .csv。 A csv file is no more than a text file wich is written this way(comma delimited): 一个csv文件只不过是一个以这种方式编写的文本文件(以逗号分隔):

"column1","column2","column3" “列1”,“列2”,“栏3”

So now all you just have to do is read a text file. 所以现在你要做的只是阅读一个文本文件。

CVS Files CVS文件

Hope it helps 希望能帮助到你

You can use OpenXml or ClosedXml - they work on the server您可以使用 OpenXml 或 ClosedXml - 它们在服务器上工作

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

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