c#


Exception thrown when opening a Excel spreedsheet from C# stating file is being used by another user


I am currently developing an application that requires an excel spreadsheet, location selected by the user, to be read into a DataTable and then stored in a sql server database.
The application works perfectly in my development environment, however when it is deployed into my production environment an exception is thrown with the following message.
The Microsoft Jet database engine cannot open the file '.xls'. It is already opened exclusively by another user, or you need permission to view its data.
My code to read the excel file is as follows:
OleDbConnection objConn = null;
DataSet objDataset1 = null;
string fileLocation = GetFileLocation();
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=" + fileLocation
+ ";" + "Extended Properties=Excel 8.0;";
objConn = new OleDbConnection(sConnectionString);
objConn.Open(); //This is where the exception is thrown
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, "XLData");
DataTable dt = objDataset1.Tables["XLData"];
Note the application and excel file are on different servers in the same domain.
Digging around various forumns and knowledge bases it would appear that the exception is thrown when the "user" does not have permission to use the file. Although not recommended permissions on the file have been set to Full Access for all users.
Apart from file permissions what else could cause this exception to be thrown?
Not sure if this is helpful but...
The error:
The Microsoft Jet database engine
cannot open the file '.xls'.
seems to point to a file named .xls, as opposed to something named, say myspreadsheet.xls or prodfile.xls.
It may be a long shot, but can you do some debugging to make sure that the file Jet is trying to open actually exists? It may be that the filename is not being constructed properly, for some reason.
Here's a few answers to the same root problem:
Read from Excel using OleDb in a Windows Service?
kill the previous opened excel process before starting the new process for reading the excel.it will solve your problem.
i wrote one function you can call it before opening the xls.
private void kill_excel_process()
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Equals("EXCEL"))
{
clsProcess.Kill();
break;
}
}
}

Related Links

Read console output while program is running c#
How to run a for loop after a for loop?
alert box from asp.net 3.5 Code-Behind
Quartz.Net 2.0.1 Trigger
WPF UI Animation Library
Using SqlConnection to execute query in VS 2010 Debugger: Evaluation of expression or statement timed out
Deserializing JSON to List<> is not working in C#
How to add an item to the Powerpont context menu using .net add-in project
Unable to add user controls to a panel dynamically
Callback from main function
Data Structure used for C#'s Dictionary.KeyCollection
How to add a string to this? [closed]
DataBinding to Textbox performance in C#.NET
How to use function max for auto number?
C# Add Object To List
How do I set the focus to the Desktop from within my C# application

Categories

HOME
gulp
javafx
livecharts
pandoc
bitbucket
mediarecorder
meson-build
pdf-generation
facebook-sharer
unreal-engine4
distinct
watson-dialog
yui
webseal
dompdf
brightway
facebook-marketing-api
web-parts
airbnb
orange
google-street-view
keystone
android-wifi
onclicklistener
android-wear-2.0
windowserror
paper-trail-gem
arp
key-bindings
mplab
angularjs-resource
mootools
powerbi-embedded
minitest
kdevelop
strategy-pattern
client-server
form-fields
pygraphviz
var
google-chrome-console
wicket-tester
tapply
android-graphview
mtm
codepen
django-cors-headers
remap
react.rb
sharpdevelop
intermediate-language
htop
lightspeed
therubyracer
spring-social-twitter
consolidation
virtualenvwrapper
tinybox2
geokit
airwatch
system-integration
3d-rendering
treelist
yfiles
gnu-sort
bioconductor
dnvm
cyberduck
eaaccessory
eggplant
pdcurses
stereotype
minko
backtrace
information-hiding
nsnotificationcenter
jsmpp
nodeunit
data-layers
imaplib
xcode6.1-gm-seed
svg-android
grunt-contrib-imagemin
oc4j
usersettings
dd4t
select2-rails
mysql2
gwt2
architectural-patterns
metapost
correctness
linear-interpolation
orchestration
wpml
xcode4.6.3
nserror
optimistic-concurrency
maven-eclipse-plugin
overlays
opengl-to-opengles
qlibrary
managed-code
vista64

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App