部署后上载Excel工作表时出错 [英] Error while uploading excel sheet after deployment

查看:61
本文介绍了部署后上载Excel工作表时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的网络应用程序中有一项功能是通过浏览按钮上传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文件在客户端位置关闭。因为如果已经为同一文件打开了另一个连接,则Microsoft Jet OleDB连接无法打开该文件。



确保您的计算机具有可用的Microsoft Jet DLL或Office安装在您在IIS服务器上托管应用程序的计算机上。
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.


这篇关于部署后上载Excel工作表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆