[英]Not able to create and save excel file when C# executable is run form Task Scheduler
Using below code, created C# console app which will create Test.xlsx in Files folder.使用下面的代码,创建了 C# 控制台应用程序,它将在 Files 文件夹中创建 Test.xlsx。
public class Program
{
public string dailyUpdateFile;
Microsoft.Office.Interop.Excel.Application excel;
Microsoft.Office.Interop.Excel.Workbook worKbooK;
Microsoft.Office.Interop.Excel.Worksheet worKsheeT;
static void Main(string[] args)
{
System.Diagnostics.Debugger.Launch();
Program obj = new Program();
obj.excel = new Microsoft.Office.Interop.Excel.Application();
obj.excel.DisplayAlerts = false;
DirectoryInfo dInfo = Directory.GetParent(Environment.CurrentDirectory);
dInfo = Directory.GetParent(dInfo.FullName);
obj.dailyUpdateFile = dInfo + "\\Files\\Test.xlsx";
if (!File.Exists(obj.dailyUpdateFile))
{
obj.worKbooK = obj.excel.Workbooks.Add(Type.Missing);
obj.worKsheeT = (Microsoft.Office.Interop.Excel.Worksheet)obj.worKbooK.ActiveSheet;
obj.worKsheeT.Name = "TestFile";
obj.worKsheeT.Cells[1, 1] = "Date";
obj.worKsheeT.Cells[1, 2] = "Day";
}
obj.worKbooK.SaveAs(obj.dailyUpdateFile);
obj.excel.Quit();
}
}
Now when application executable is run from Task scheduler, getting below exception:现在,当从任务调度程序运行应用程序可执行文件时,出现以下异常:
Exception Info: System.Runtime.InteropServices.COMException at Microsoft.Office.Interop.Excel._Workbook.SaveAs(System.Object, System.Object, System.Object, System.Object, System.Object, System.Object, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode, System.Object, System.Object, System.Object, System.Object, System.Object) at Test.Program.Main(System.String[])异常信息:System.Runtime.InteropServices.COMException at Microsoft.Office.Interop.Excel._Workbook.SaveAs(System.Object, System.Object, System.Object, System.Object, System.Object, System.Object, Microsoft.Office .Interop.Excel.XlSaveAsAccessMode, System.Object, System.Object, System.Object, System.Object, System.Object) 在 Test.Program.Main(System.String[])
Above exception can be catch from Event Viewer.以上异常可以从事件查看器中捕获。
Thought I am pretty closed to find the solution but not able to get in a day.以为我很难找到解决方案,但一天无法解决。
One think want to point out- when app is run from task scheduler, it is pointing "C:\\Files " but that it ok, it should create Test.xlsx under that folder.一个想法想指出 - 当应用程序从任务计划程序运行时,它指向“C:\\Files”,但没关系,它应该在该文件夹下创建 Test.xlsx 。
While debugging got below exception虽然调试低于异常
Help on this.帮助解决这个问题。
I would rather save the files in shared path with unique filename like random filename with datetime.我宁愿使用唯一的文件名将文件保存在共享路径中,例如带有日期时间的随机文件名。 Only problem i see here is access issues with the user under which the task is running.
我在这里看到的唯一问题是运行任务的用户的访问问题。
Get the path from config and save the files to shared path which will have full permissions to read and write.从配置中获取路径并将文件保存到共享路径,该路径将具有完全的读写权限。
It is not recommended to save files in the application server.不建议在应用服务器中保存文件。
Thanks Kiran谢谢基兰
Is it possible that the permission issue?是否有可能是权限问题? Please try to grant certain permission to the user in the Security settings, or change another user with enough permission.
请尝试在安全设置中向用户授予某些权限,或更改具有足够权限的其他用户。
Using COM to interoperate with Office without human interaction is not recommeneded.不建议使用 COM 与 Office 进行互操作而无需人工交互。 Please check this page for detail.
请查看此页面了解详细信息。 I recommeneded to use NPOI instead, it would be more efficient and reliable for this kind of operations.
我建议改用NPOI,这样的操作会更高效、更可靠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.