问题描述
大家好,
我的网络应用程序中有一项功能是通过浏览按钮上传excel表格
它在我的本地机器上工作正常。但是在IIS服务器上部署之后,当我尝试通过URL上传excel表时,它在本地工作正常时给出错误。
错误: - Microsoft Jet数据库引擎无法打开文件''。它已由其他用户专门打开,或者您需要获得查看其数据的权限。
我的代码: -
public DataTable GetExcelData(string _sfile)
{
OleDbConnection xlconn = new OleDbConnection();
string ext = System.IO.Path.GetExtension(_sfile);
string connectionStringExcel = string.Empty;
switch(ext)//这个开关代码验证允许只上传excel文件的文件你可以更改任何文件
{
case.xls:
connectionStringExcel =Provider = Microsoft .Jet.OLEDB.4.0;数据源=+ _sfile +;扩展属性= \Excel 8.0; HDR =是; IMEX = 2 \;
break;
case.xlsx:
connectionStringExcel =Provider = Microsoft.ACE.OLEDB.12.0; Data Source =+ _sfile +; Extended属性= \Excel 12.0; HDR =是; 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(dtExcel.Rows中的DataRow行)
{
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();
返回xldt;
}
我需要在IIS中添加任何权限吗?
请帮助。
谢谢advance
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
推荐答案
这篇关于部署后上载Excel工作表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!