Reading Excel file on the server - c#

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();
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

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.
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.
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.
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.
If the application is running on a virtual desktop, say like Citrix, then it's the same as 3.
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.
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.
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

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. you can 'save as' a excel file into *.csv. A csv file is no more than a text file wich is written this way(comma delimited):
"column1","column2","column3"
So now all you just have to do is read a text file.
CVS Files
Hope it helps

You can use OpenXml or ClosedXml - they work on the server

Related

Microsoft Access list all database

I need to fetch all databases of Microsoft Access in localhost.
On clicking the database name I have to list the tables
I can able to fetch the tables for a particular database.
My problem is I cant able to list out the MS Access databases available in localhost
please suggest me the ideas to start coding
I tried
OleDbConnection con; // create connection
OleDbCommand com; // create command
OleDbDataReader dr; //Data read for read data from database
con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source =D:\mydb.mdb");
com = new OleDbCommand("Select * from Table1",con);
con.Open(); // open the connection dr = com.ExecuteReader();
expected result:
with out mentioning Data Source =D:\mydb.mdb" i have to list all ms access databases of
localhost
All available databases like
database1.accdb
database2.accdb
Access (or Jet, to use the name of the database engine) doesn't run a server, in the way that Sql Server or MySql do. An Access database is simply a file of particular format that is loaded on request, but the Jet engine libraries.
For this reason, there's no simple way to find all databases on a local machine: you would need to scan all files on the machine for .accdb or .mdb extensions. More likely, you should ask the user to select a specific file or scan a specified subfolder, since searching the whole machine will likely take a prohibitively long time.

How to get rid of the exception "The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine" in Win 7

I am running an ASP.NET C# application on .NET 4.0 framework using VS2010 on a Win 7 machine. Within my code I want to link Excel file with "DataTable" object. ie I want to access the data within Excel file and store it in DataTable object. So I used the following code snippet:
_
_connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" + excelFile + "\";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'";
}
DataTable table = new DataTable();
OleDbCommand command = new OleDbCommand();
command.Connection = new OleDbConnection(_connectionString);
command.CommandType = CommandType.Text;
command.CommandText = "select * from [NameOFExcelSheet$A1:D20]"; // Want to read the Excel sheet. The name of Excel sheet is "NameOfExcelSheet". Wan to read the celles in the range A1 and D20.
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = command;
adapter.Fill(table); // EXCEPTION OCCURS IN THIS LINE.
I installed the exe available at the link http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en
But still I am gettin the same exception msg while running my code.
The exception that I am getting is "The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine"
PLz help me with this.
THANKS IN ADVANCE.
You're probably on 64bit Windows and installed a 32bit driver. Either switch to 32bit compilation or source a 64bit driver.
You should try this (ensuring that you are in a x86 (32bits) machine ):
This download will install a set of components that can be used to facilitate transfer of data between 2007 Microsoft Office System files and non-Microsoft Office applications.
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en

Read excel file and insert records in database in C#- Windows Azure

The requirements for my application is to browse for an excel file, Upload the file , read each row from the excel file and insert into the database.
I am building this application in Windows Azure.
Prior to this I have successfully used OleDbDataAdapter to read the data from the excel file into the dataset.
The connection strings I used are:
FOR XLS file:
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strNewPath & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=2"""
FOR XLSX file:
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strNewPath & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
Will the following providers Microsoft.Jet.OLEDB.4.0 & Microsoft.ACE.OLEDB.12.0 be avaialable in Windows Azure?
Will approach also work in Cloud as well and is this the best way to access Excel files in c#?
Any new ideas and suggestions are welcome.
No, OLE providers will not be available under the Worker Role by default. It might be possible to have them later in a different VM setup though.
For the time being you are basically restricted to .NET-only options:
EPPlus for XLSX
NPOI for XLS
I am using OpenXMl SDK for excel processing. It works perfectly in cloud environment.
Since you mentioned you need to support .xls and .xlsx files, you could use a product like OfficeWriter which supports both file types in one library.
Disclaimer: I am one of the developers on OfficeWriter

Read from Excel using OleDb in a Windows Service?

Disclaimer: I know this is a bad way to do things. It is the only option we have with our client.
Problem:
We need to read data from an Excel file every x amount of time. The data is constantly changing via a 3rd party Excel plug in. The environment for the application is Windows XP, SP1 and .Net 2.0. No upgrading the OS to SP2/3 or upgrading the .Net Framework. Again, this is all per customer parameters.
Here is current code we have:
String sConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Settings.ReutersFilePath + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"";
using (OleDbConnection objConn = new OleDbConnection(sConnectionString))
{
try
{
objConn.Open();
_dataSet = new DataSet();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + ConfigurationManager.AppSettings["Excel.WorksheetName"] + "$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objAdapter1.Fill(_dataSet);
_dataTable = _dataSet.Tables[0];
objConn.Close();
// Persists new data to a database.
UpdateSQL();
}
catch (Exception ex) { }
finally
{
_isCurrentlyProcessing = false;
if (objConn != null && (objConn.State != ConnectionState.Broken || objConn.State != ConnectionState.Closed))
{
objConn.Close();
}
}
}
This code works when running from console or a Windows form, but when we run it from a Windows service, it cannot access the Excel file. Is there a limitation in XP w/SP1 that doesn't allow interaction with desktop applications like there is in Vista? Can this be done from a service? The reliability of running this from a service is much needed from the client.
Edit:
The service is running as LocalSystem.
Edit #2:
The error I get when running from a service is: The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data.
The Excel file IS open on the desktop, but it has to be open for it to get updates from the 3rd party application.
In addition to #sontek's answer.
[only applicable If you are the owners/developers of the Spreadsheet in question...]
You may find it easier to work with a "data push" model rather than a "data pull" model. It's usually an easier programming model to push the data via an excel macro directly into your data store, based upon some event from your plug-in. this is often a much better scenario than a service polling on a timer.
Excel doesn't allow you to open a file simultaneously. You need to either create it as a shared workbook or make a copy of the file and work off of the copy.
There is also full excel API (Microsoft.Office.Interop.Excel) that you could try using, which would allow you to hook into the currently opened excel/workbook.
What user is the service running as? Make sure that user has access to the specific file.
It seems most likely that it is a permissions issue. Windows services run under a different user account than desktop applications.
Also, you mention that you are executing this method every x amount of time. Make sure you are using the right timers. http://www.aspfree.com/c/a/C-Sharp/Timer-Objects-in-Windows-Services-with-C-sharp-dot-NET/
And you should be able to attach a debugger to the service by using "Attach to Process" and selecting your host process. This might give you more info on your problem. http://msdn.microsoft.com/en-us/library/7a50syb3(VS.80).aspx
SpreadsheetGear for .NET can open a file even if Excel currently has the file open and it is well suited to using from a Windows Service.
However, the file on disk will not reflect changes which have not been saved by Excel, and SpreadsheetGear would not be able to open a workbook while Excel is in the middle of saving, so the service would have to take that into account.
Disclaimer: I own SpreadsheetGear LLC
You could try making it a shared workbook (Tools > Share Workbook). This lets multiple users edit the same workbook concurrently. I'm not sure if that would work for you as some workbook features get locked down when it is shared

System.Data.OleDb.OleDbException: Invalid internet address. How do you connect to excel files located on a webserver using OleDb

I'm trying to create an OleDb connection to an Excel file that is located on a SharePoint server. The example code I'm playing with at the moment throws an OleDb exception "Invalid internet address":
public static void ConnectToRemoteExcelFile()
{
string connectionString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;\";", "http://horde.servername.co.uk/Docs/Documents/Sales.xlsx");
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
}
}
Anyone know how to connect to an excel spread sheet on a web server?
Update
Microsoft support have come back to me and agree that is not possible to connect to Excel files located on a web server. Both Jet .40 and the news ACE (Access Connectivity Engine) do not support this mode of operation. They cite reference to the KB Article "The Import/Link Data on an FTP or HTTP Server Help topic is not correct in Access 2000, Access 2002, Access 2003 and Access 2007"
As far as I know that is not possible, you would have to download the file first, then access it. The reason being is that the file could not be modified given its location.

Categories