简体   繁体   English

从任务计划程序运行 C# 可执行文件时无法创建和保存 excel 文件

[英]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.

相关问题 button1_Click Windows窗体c#创建调度程序任务 - button1_Click Windows form c# create scheduler task 从任务调度程序C#调用时,StreamWriter不写入文件 - StreamWriter not writing to file when called from task Scheduler C# 在C#可执行文件中运行表单 - Run Form in C# executable 在Windows Task Scheduler中运行C#脚本? - run c# script in windows task scheduler? 我可以编写一个C#程序 - 当作为计划任务运行时 - 检测任务计划程序何时尝试停止它 - can I write a C# program that - when run as a scheduled task - detects when Task Scheduler tries to stop it C# EF SQL 应用程序可以在手动运行时移动文件,但在由任务计划程序运行时不能移动 - C# EF SQL Application can move files when run manually but not when run by Task Scheduler 如何在 C# 中将 SELENIUM 项目作为任务调度程序中的任务运行 - how to run SELENIUM project in c# as task in task scheduler C#中的任务计划程序 - Task Scheduler in c# 无法使用c sharp创建基于事件的任务计划程序 - not able to create Event based Task Scheduler using c sharp C# Win 应用程序,任务计划程序,用户帐户:系统。 Microsoft Excel 无法访问该文件 - C# Win App, Task Scheduler, User Account: System. Microsoft Excel cannot access the file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM