在服务器上读取Excel文件 [英] Reading Excel file on the server

查看:103
本文介绍了在服务器上读取Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有asp.net应用程序,该应用程序加载excel文件以进行读取.它在我的本地开发服务器上运行良好.但是,当我将其上载到服务器时,它不会显示上载的excel文件.我在本地计算机上安装了MS Office,但在服务器上却没有.感谢您的指导

I have asp.net application which loads excel file to read. It works fine on my local development server. However when I upload it to server it wont display the uploaded excel file. I have MS Office installed on my local machine but not on server. 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();

我不断出错

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

即使我安装了Access db,并且仍然可以看到ACE 14.0,但仍然不断收到此错误

Even though I installed Access db and I can see ACE 14.0 still I keep getting this error

推荐答案

根据所需的Excel功能,您需要在服务器或客户端或了解格式的第三方控件上安装Excel

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.

  1. 如果该应用程序是WEB应用程序,则该应用程序实际通过Web浏览器在客户端上本地显示数据:请参见以下MS链接:

  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. Here you're actually sending the xls file/stream to the client.

如果要向其显示一个WEB应用程序,并在将呈现的内容发送到客户端的网页上显示数据,则需要

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.

如果该应用程序是一个桌面应用程序,该应用程序在独立应用程序中显示excel数据,而该应用程序中用户以桌面用户身份登录到服务器,则需要在服务器上安装Excel.或者,您需要可以读取并显示.XLS信息的第三方.net控件(取决于应用程序技术WinForms,WFP/Silverlight等).

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. Or you need third party .net control (dependant on app technology WinForms, WFP/Silverlight etc) that can read the .XLS info and present it.

如果应用程序在虚拟桌面上运行,例如Citrix,则与3相同.

If the application is running on a virtual desktop, say like Citrix, then it's the same as 3.

如果您要做的就是通过OLEDB读取文件,然后使用excel之外的数据,则可以安装

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. This basically the components needed to read various Office data in a need 'service only' package (or driver if you prefer). 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. We use for loading .XLS files into databases.

所有这些都有不同的许可限制和费用.在没有任何用例的情况下,我只能建议您认为Excel是一组庞大的应用程序组件,要使用它们,您需要准确了解您的应用程序及其功能/交互方式以及它们在任何特定情况下的局限性您设想的部署方案.

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.

进一步的信息:如果您在64位服务器上运行并且正在使用32位应用程序,那么我可以使用ACE的唯一方法是安装ACE 2007而不是2010版本

这篇关于在服务器上读取Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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