Excel upload not working on windows Server 2012 - c#

I have developed an application where the user have to upload a winzip archive file. The code then extracts an Excel file (in .xls format, 97-2003 workbook) from that archive and uploads the data within that Excel file into my SQL database table. I'm using ZipFile.ExtractToDirectory(zipPath, extractPath) to extract the file.
Below is the code I'm using for the excel upload.
using (OleDbConnection connExcel = new OleDbConnection(Excelconstring))
{
using (OleDbCommand cmdExcel = new OleDbCommand())
{
using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
{
cmdExcel.Connection = connExcel;
connExcel.Open();
DataTable dtExcelSchema;
string sheetName = "Chat Summary$";
//DateTime.Today.ToString("M.d.y$");
// Read Data from First Sheet.
cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
odaExcel.SelectCommand = cmdExcel;
odaExcel.Fill(dt);
connExcel.Close();
}
}
}
The problem is this works absolutely fine on my local system 64-bit Windows 7 with Microsoft Office 2010. I'm using Microsoft.Jet.OLEDB.4.0 on my local system. The problem is when I deploy the same code on my server (Windows Server 2012 with Microsoft Office 2013), this doesn't work. I have installed Microsoft Access 2010 redistributable on the server and change the provider to Microsoft.ACE.OLEDB.12.0 but still it doesn't work. The error I get after changing the provider to ACE is
External table is not in the expected format
Also when I extract the file myself, open it once and then archive it again - it works fine. I have tried many solutions from different forums but none of them seems to work. I have changed the debug mode from "anycpu" to "x86" and "x64" but still it doesn't work.
The steps I have already tried are installing the Microsoft Access 2010 redistributable, changing the debug mode from "anycpu" to "x86", changing the connection string from Jet.OLEDB.4.0 to ACE.OLEDB.12.0.

Excel 2010 is still a 32-bit application. Therefore, try setting Enable 32-bit applications to True on your IIS App Pool.

Related

Visual Studio Project Installer not installing all dll's

I have a C# exe, called 'start.exe' that is when run will check a SQL database and copy a file to a folder if certain criteria are met.
This works on most computers.
But on some computers when I try to run it I get the following error message.
System.invalidOperationException: The 'Microsoft.ACE.OLEDB.12.0' is not registered on the local machine
To fix this I created a setup project that should add the missing .dll.
I created the set up project, went to the applications folder, added the primary output of the project.
I then ran a batch build on all the files and copied the msi and setup .exe files to a problem computer and ran it.
It adds System.Net.Http.dll and 'start.exe' to the computer.
When I run the exe, it still gives me the same error.
I believe the message box comes from these lines of code.
try
{
using (var conn = new OleDbConnection(#"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=" + CurFeSource + ";"))
{
conn.Open();
using (var cmdBE = new OleDbCommand("SELECT value FROM Tab1", conn))
{
OleDbDataReader rdrBE = cmdBE.ExecuteReader();
rdrBE.Read();
be_version = (rdrBE.GetString(0));
rdrBE.Close();
}
using (var cmdFE = new OleDbCommand("SELECT Col FROM Links", conn))
{
OleDbDataReader rdrFE = cmdFE.ExecuteReader();
rdrFE.Read();
fe_version = (rdrFE.GetString(0));
rdrFE.Close();
}
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return false;
}
Edit:
As SQLGeorge said, I need to install Microsoft.ACE.OLEDB.12.0.
But how do I add it to a setup project.
The Microsoft.ACE.OLEDB.12.0 must be downloaded from Microsoft:
https://www.microsoft.com/en-US/download/details.aspx?id=13255
Excerpt From Microsoft:
This download will install a set of components that facilitate the transfer of data between existing Microsoft Office files such as Microsoft Office Access 2010 (*.mdb and .accdb) files and Microsoft Office Excel 2010 (.xls, *.xlsx, and *.xlsb) files to other data sources such as Microsoft SQL Server. Connectivity to existing text files is also supported. ODBC and OLEDB drivers are installed for application developers to use in developing their applications with connectivity to Office file formats.
As noted by Microsoft:
As a general replacement for Jet (If you need a general replacement for Jet you should use SQL Server Express Edition).

Can I access to Microsoft Access file without Microsoft Access application through odbc driver on Windows 10(64bit) machine?

I would like to run a c# code in windows 10(64bit).
The code uses odbc to access to a Microsoft Access file.
The code works well on PCs which have Microsoft Access 2010 and 2013.
However it cannot access to the file on the PC which does not have Microsoft
Access Application. Can I access to the file without Microsoft Access Application. Or do I need to install something more than "Microsoft Access Driver 64bit version" to access the file?
What I did are followings,
(1) I installed "Microsoft Access Driver (*.mdb, .accdb)" 64bit version.
(2) I Confirmed ODBC data source administrator recognized ACEODBC.DLL as "Microsoft Access Driver (.mdb, *.accdb)".
The followings shows the part of code calling odbc driver.
public static DataTable simpleQuery(string query, string pathToDatabase )
{
DataTable dt = new DataTable();
try
{
using (var con = new OdbcConnection())
{
con.ConnectionString =
#"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
#"Dbq=" + pathToDatabase + ";";
con.Open();
OdbcDataAdapter adapter = new OdbcDataAdapter(query, myConnectionString);
adapter.Fill(dt);
con.Close();
}
return dt;
}
catch (Exception e)
{
throw;
}
}
Thank you very much.
I solved the problem.
The machine has 32 bit Microsoft Office although it is running on 64 bit Windows.
So I needed to install Microsoft Access Driver (*.mdb, .accdb) 32 bit version, not 64 version.
Thank you.

C# with Dll should i import in my project that he can work without instal Excel

I have a problem, i deployed my application where i have a connection with Excel file but it doesn't work in other laptop. i have that code :
{ string constr1 = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + m + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";
OleDbConnection con = new OleDbConnection(constr1);}
i wanna just to know witch .dll should i import and includ it in my project that my application connect with Excel file even if Excel is not installed
You'll need to install the Microsoft Access Database Engine and your app should work. This link will get you the Access 2010 version (https://www.microsoft.com/en-ca/download/details.aspx?id=13255) which should work for OLEDB.12.
If you're looking for support for later versions of Excel be aware that MS has altered the names that they use for this set of libraries.

OleDbConnection.Open() throws exception only in one project, same code works in other projects

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
I know the common fix for this which is to install:
Microsoft Access Database Engine 2010 Redistributable
Or
2007 Office System Driver: Data Connectivity Components
Both are installed on my local PC. This is the code which I have
OleDbConnection conn = new OleDbConnection();
string fileName = "test.xlsx";
try
{
string connectString = String.Format(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR={1};'",
fileName, "YES");
conn.ConnectionString = connectString;
conn.Open(); //exception is thrown here !!!
OleDbCommand comm = new OleDbCommand();
comm.CommandText =
string.Format("CREATE TABLE [{0}] ", "Test");
comm.Connection = conn;
comm.ExecuteNonQuery();
OleDbDataAdapter ad = new OleDbDataAdapter(
string.Format("SELECT * FROM [{0}]", "Test"), conn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(ad);
}
catch(Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
I try this code in other projects on my local machine and everything is working. I have project which creates excel exports and I don't have this problem.
The problem is in the project I don't understand how to fix it. Also on the current project I create one new .aspx page and in Page_Load put only this code, same exception.
Additional information: this project was written on vs 2008, convert to 2010 and after that used in vs 2012. Everything was working in the project till now.
Also same thing for JET connection string !
EDIT
After the answer of Joe I see that this project is run on 64bitProcess:
bool test = Environment.Is64BitProcess; //return true.
My driver is for 32bit. What I can do now, can I change the environment process. I can't install the driver for 64bit process because I have installed Office 2010 x32.
The OLEDB drivers for 32-bit and 64-bit applications are different.
If you only have the 32-bit driver installed, then 64-bit applications that attempt to use it will get this error. Similarly, if you have only the 64-bit version installed, then 32-bit applications that attempt to use it will get this error.
You say:
I try this code in other projects on my local machine and everything is working
Ergo, at least one of the two must be correctly installed.
To understand what's happening you could examine Environment.Is64BitProcess in both the application that works, and the one that doesn't. That will tell you which version is missing.
Then download and install the 32-bit or 64-bit version that's missing from:
http://www.microsoft.com/en-us/download/details.aspx?id=13255
You need AccessDatabaseEngine.exe (32-bit) or AccessDatabaseEngine_64.exe (64-bit)
Note that you may need to specify the provider as 'Microsoft.ACE.OLEDB.14.0' for the Office 2010 version (12.0 was for Office 2007).
After some hours I found a solution:
What were the steps, first I try to run the project on x86 - Properties/Build/Platform Target. Exception was thrown that I can't rebuild, because a registry was false. I create the registry. How to do it:
In notepad file paste this code and save it like reg file. Make the file name something to remember why you have it in the future(Fusion.reg).
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion]
"EnableLog"=dword:00000001
After that I had a problem because not all of my assemblies was readable by the application pool. Solution about that was going to IIS/Application Pools/Application Pool 4.0/General/Enable 32- Bit Applications. After that restart the IIS, close the project and open it again and everything was working with 32bit version.

Parsing excel files fails

I have to pull data from an excel file. I was using the below piece of code since last year:
OleDbConnection cnn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + destFile + "; Extended Properties=Excel 12.0;");
string qText = #"select * from [sheetXY$]";
OleDbCommand oconn = new OleDbCommand(qText, cnn);
cnn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
adp.Fill(dt);
cnn.Close();
Since yesterday I get this error message:
I had Office 2010 pro on my machine. According to some google suggestions I installed microsoft database access engine, made no difference. I re-installed whole office, made no difference. Then I installed Office 2013, no go.
What could possibly go wrong from a day to another, and how shall I fix it? I'm out of idea.
Save yourself time and headache and use EPPLUS instead. EPPLUS can be installed via Nuget and you don't have to deal with installing the Office Data access on other machine where your code runs.
You can go for openXML SDK. Which requires no machine level dependency.
http://www.codeproject.com/Articles/371203/Creating-basic-Excel-workbook-with-Open-XML
http://msdn.microsoft.com/en-us/library/office/gg575571(v=office.15).aspx

Categories

Resources