Click here to Skip to main content
14,694,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
i have one functionality in my web application to upload excel sheet via browse button
its working fine in my local machine . but after deployment in IIS server when i am trying to upload excel sheet via URL its giving error while in local its working fine.


Error :-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.

My Code :-
public DataTable GetExcelData(string _sfile)
{
OleDbConnection xlconn = new OleDbConnection();


string ext = System.IO.Path.GetExtension(_sfile);

string connectionStringExcel = string.Empty;
switch (ext) // this switch code validate the files which allow to upload only excel file you can change it for any file
{
case ".xls":

connectionStringExcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _sfile + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
break;
case ".xlsx":

connectionStringExcel = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _sfile + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
break;
}
xlconn = new OleDbConnection(connectionStringExcel);
xlconn.Open();
DataTable dtExcel = new DataTable();
dtExcel = xlconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
OleDbDataAdapter xlda = new OleDbDataAdapter();
DataTable xldt = new DataTable();

if (dtExcel != null)
{

String[] excelSheetNames = new String[dtExcel.Rows.Count];
int i = 0;

foreach (DataRow row in dtExcel.Rows)
{

excelSheetNames[i] = row["TABLE_NAME"].ToString();

string lastChars = excelSheetNames[i].Substring(excelSheetNames[i].Length - 1);

if (lastChars == "_")
{
excelSheetNames[i] = excelSheetNames[i].Remove(excelSheetNames[i].Length - 1);

}

string strQ = "select * from [" + excelSheetNames[i] + "]";
xlda = new OleDbDataAdapter(strQ, xlconn);

// DataTable xldt = new DataTable();
xlda.Fill(xldt);

// xldt.AcceptChanges();
i++;
}
}
xlconn.Close();

return xldt;
}


is there i need to add any Permission to IIS?
Kindly help.

Thanks in advance
Posted

1 solution

Before uploading the file make sure your excel file is closed at the client location. Because Microsoft Jet OleDB connection is not able to open the file if another connection is already open for the same file.

Make sure your machine have the Microsoft Jet DLL available or Office installed in the machine where you hosted the application on IIS server.
   
Comments
kapil0411 20-Aug-14 5:58am
   
Thanks for your reply.
yes i have checked that excel file is closed before uploading and also office is installed on server machine.but yet facing the issue....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900