Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用C#服务打开xls文件_C#_Excel_Import_Excel Interop_Interopservices - Fatal编程技术网

无法使用C#服务打开xls文件

无法使用C#服务打开xls文件,c#,excel,import,excel-interop,interopservices,C#,Excel,Import,Excel Interop,Interopservices,我必须创建一个服务,将数据从excel文件导入SQL Server表;整个系统属于我公司的一位客户,因此我必须使用Visual Studio 2010。某些打开的文件可能已打开,因此我决定将其复制到临时目录,然后解析此副本。我认为我做到了,因为它在调试模式下工作正常:打开文件,读取所有行,最后将值写入数据库。我将其作为服务安装,但不断出现相同的错误(翻译成英语): 该文件存在,我可以在文件夹中找到它,在服务试图打开它的那一刻,没有人在使用它(我只是复制了它)。我没有试图保存它,我以只读模式打开它

我必须创建一个服务,将数据从excel文件导入SQL Server表;整个系统属于我公司的一位客户,因此我必须使用Visual Studio 2010。某些打开的文件可能已打开,因此我决定将其复制到临时目录,然后解析此副本。我认为我做到了,因为它在调试模式下工作正常:打开文件,读取所有行,最后将值写入数据库。我将其作为服务安装,但不断出现相同的错误(翻译成英语):

该文件存在,我可以在文件夹中找到它,在服务试图打开它的那一刻,没有人在使用它(我只是复制了它)。我没有试图保存它,我以只读模式打开它,这样我也可以排除第三点。 我开发该服务的机器与运行该服务的机器是同一台机器,即Windows 7 Ultimate service Pack 1

我认为这可能是服务用户的问题,所以我尝试在VisualStudio中以调试模式启动服务,并使用相同的服务用户(管理级别)登录到系统,结果运行得非常好。 然后我想这可能是时间问题,也许服务试图在系统没有发布文件时“太早”打开文件,所以我添加了一个

System.Threading.Thread.Sleep(5000);

xlApp = new Microsoft.Office.Interop.Excel.Application(); 
但没什么变化。 当然,每次修改服务时,我都会停止它,将新文件复制到系统中,然后重新启动它

代码是:

string fileName = "[file name].xlsm";
string sourcePath = @"[origin]";
string targetPath = @"[destination]";
string fileDest = "[destination file name].xls";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string targetFile = System.IO.Path.Combine(targetPath, fileDest);

try
{
    System.IO.File.Copy(sourceFile, targetFile, true);
}
catch (Exception myEx)
{
    throw new Exception(myEx.ToString());
}

xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.DisplayAlerts = false;
xlApp.Visible = false;
try
{
    wbk = xlApp.Workbooks.Open(targetFile, 0, true, Type.Missing, "[pwd]", "[pwd]", true, Type.Missing, Type.Missing, false, false, Type.Missing, true, Type.Missing, Type.Missing); [<- this is the problem, the exception is raised with these instruction]
    sheet = new Worksheet();
    range = null;
    sheet = wbk.Worksheets.get_Item(4);
    range = sheet.UsedRange;
    numColonne = range.Columns.Count;
    numRighe = range.Rows.Count;
}
catch (Exception myEx)
{
    throw new Exception(myEx.ToString());
}
string fileName=“[file name].xlsm”;
字符串sourcePath=@“[origin]”;
字符串targetPath=@“[目的地]”;
字符串fileDest=“[destination file name].xls”;
字符串sourceFile=System.IO.Path.Combine(sourcePath,文件名);
字符串targetFile=System.IO.Path.Combine(targetPath,fileDest);
尝试
{
System.IO.File.Copy(sourceFile,targetFile,true);
}
catch(异常myEx)
{
抛出新异常(myEx.ToString());
}
xlApp=新的Microsoft.Office.Interop.Excel.Application();
xlApp.DisplayAlerts=false;
xlApp.Visible=false;
尝试
{

wbk=xlApp.Workbooks.Open(targetFile,0,true,Type.Missing,“[pwd]”,“[pwd]”,true,Type.Missing,Type.Missing,Type.true,Type.Missing,Type.Missing,Type.Missing);[我添加了这两个文件夹,C:\Windows\SysWOW64\config\systemprofile\Desktop(64位)和C:\Windows\System32\config\systemprofile\Desktop(32位).我在另一个论坛上找到了它们,我决定尝试一下;现在它起作用了。 别问我为什么

string fileName = "[file name].xlsm";
string sourcePath = @"[origin]";
string targetPath = @"[destination]";
string fileDest = "[destination file name].xls";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string targetFile = System.IO.Path.Combine(targetPath, fileDest);

try
{
    System.IO.File.Copy(sourceFile, targetFile, true);
}
catch (Exception myEx)
{
    throw new Exception(myEx.ToString());
}

xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.DisplayAlerts = false;
xlApp.Visible = false;
try
{
    wbk = xlApp.Workbooks.Open(targetFile, 0, true, Type.Missing, "[pwd]", "[pwd]", true, Type.Missing, Type.Missing, false, false, Type.Missing, true, Type.Missing, Type.Missing); [<- this is the problem, the exception is raised with these instruction]
    sheet = new Worksheet();
    range = null;
    sheet = wbk.Worksheets.get_Item(4);
    range = sheet.UsedRange;
    numColonne = range.Columns.Count;
    numRighe = range.Rows.Count;
}
catch (Exception myEx)
{
    throw new Exception(myEx.ToString());
}