繁体   English   中英

任务计划程序未在应用程序中启动Excel

[英]Task Scheduler Not Launching Excel in Application

我编写了一个应用程序,该应用程序可以安装到我们的服务器上,检查是否创建了任何待处理的作业,是否处理了所有可用的作业,创建包含结果的excel文档,然后将其通过电子邮件发送给我们的用户。 它使用Microsoft Excel 2010以c#.net v3.5编写,并通过任务计划程序每30分钟启动一次。 我们的任务计划程序在运行2008 r2的服务器上运行。 任务计划程序成功运行,成功地更新了数据库,就像处理了作业一样,但是,它从未创建excel文件,而且我不知道为什么。 如果我在服务器计算机上手动运行该应用程序,则该应用程序运行顺利,但是即使任务计划程序调用它也无法运行一次。 因为我是管理员,所以没有权限问题,我甚至尝试使用我们的IT总监凭据运行它,但无济于事。 还要检查该文件夹对创建/删除/编辑/等具有完全访问权限。 还尝试提供到文件的绝对路径来代替相对路径。 可以在下面找到用于创建excel文件的代码以及用于调用它的代码。 提前致谢。

Excel代码:

public static void Export(System.Data.DataTable dt,
                                  string FileName,
                                  string EmailTo)
        {
            Application xls = new Application();
            xls.SheetsInNewWorkbook = 1;

            // Create our new excel application and add our workbooks/worksheets
            Workbooks workbooks = xls.Workbooks;
            Workbook workbook = workbooks.Add();
            Worksheet CompPartsWorksheet = (Worksheet)xls.Worksheets[1];

            // Hide our excel object if it's visible.
            xls.Visible = false;

            // Turn off screen updating so our export will process more quickly.
            xls.ScreenUpdating = false;

            // Turn off calculations if set to automatic; this can help prevent memory leaks.
            xls.Calculation = xls.Calculation == XlCalculation.xlCalculationAutomatic ? XlCalculation.xlCalculationManual : XlCalculation.xlCalculationManual;

            // Create an excel table and fill it will our query table.
            CompPartsWorksheet.Name = "Comp Request";
            CompPartsWorksheet.Select();
            {

                // Create a row with our column headers.
                for (int column = 0; column < dt.Columns.Count; column++)
                {
                    CompPartsWorksheet.Cells[1, column + 1] = dt.Columns[column].ColumnName;
                }

                // Export our datatable information to excel.
                for (int row = 0; row < dt.Rows.Count; row++)
                {
                    for (int column = 0; column < dt.Columns.Count; column++)
                    {
                        CompPartsWorksheet.Cells[row + 2, column + 1] = (dt.Rows[row][column].ToString());
                    }
                }
            }

            // Freeze our column headers.
            xls.Application.Range["2:2"].Select();
            xls.ActiveWindow.FreezePanes = true;
            xls.ActiveWindow.DisplayGridlines = false;

            // Autofit our rows and columns.
            xls.Application.Cells.EntireColumn.AutoFit();
            xls.Application.Cells.EntireRow.AutoFit();

            // Select the first cell in the worksheet.
            xls.Application.Range["$A$2"].Select();

            // Turn off alerts to prevent asking for 'overwrite existing' and 'save changes' messages.
            xls.DisplayAlerts = false;

            // ******************************************************************************************************************
            // This section is commented out for now but can be enabled later to have excel sheets show on screen after creation.
            // ******************************************************************************************************************
            // Make our excel application visible
            //xls.Visible = true;

            //// Turn screen updating back on
            //xls.ScreenUpdating = true;

            //// Turn automatic calulation back on
            //xls.Calculation = XlCalculation.xlCalculationAutomatic;

            string SaveFilePath = string.Format(@"{0}\Jobs\{1}.xlsx", Directory.GetCurrentDirectory(), FileName);
            // string SaveFilePath = @"\\netapp02\Batch_Processes\Comp_Wisp\Comp_Wisp\bin\Debug\Jobs";
            workbook.SaveAs(SaveFilePath, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            workbook.Close();

            // Release our resources.
            Marshal.ReleaseComObject(workbook);
            Marshal.ReleaseComObject(workbooks);
            Marshal.ReleaseComObject(CompPartsWorksheet);
            Marshal.ReleaseComObject(xls);
            Marshal.FinalReleaseComObject(xls);

            Send(Subject: FileName,
                 AttachmentPath: SaveFilePath,
                 EmailTo: EmailTo);
        }

调用Excel创建方法的代码:

public static void ProcessJob(System.Data.DataTable Job)
        {
            try
            {
                for (int j = 0; j < Job.Rows.Count; j++)
                {
                    // Temporary datatable to store our excel data.
                    System.Data.DataTable dt = new System.Data.DataTable();

                    // Output the current job name and split out our parts.
                    Console.Write(string.Format("JOB: {0}\r\n", Job.Rows[j]["JobID"]));
                    string[] searchlines = Job.Rows[j]["PartsList"].ToString().Replace("\n", "|").Split('|');

                    if (searchlines.Count() > MAX_TO_PROCESS)
                    {
                        // If our search is going to be above the desired processing maximum start the next job as well.
                        Thread RecursiveJob = new Thread(GetJob);
                        RecursiveJob.Start();
                    }

                    for (int i = 0; i < searchlines.Count(); i++)
                    {
                        // Determine if data reporting is turned on or off.
                        Boolean isReporting = DataCollection();
                        if (searchlines[i].Trim() != string.Empty)
                        {
                            Console.WriteLine(string.Format("Processing: {0}", searchlines[i]));
                            using (SqlConnection con = new SqlConnection(dbComp))
                            {
                                using (SqlDataAdapter da = Connect.ExecuteAdapter("[CompDatabase_SelectPartsFromComp]", con,
                                                                                     new SqlParameter("@PartNumber", searchlines[i].Trim()),
                                                                                     new SqlParameter("@CurrentMember", Job.Rows[j]["Email"].ToString()),
                                                                                     new SqlParameter("@DataCollection", isReporting)))
                                {
                                    da.Fill(dt);
                                }
                            }
                        }
                    }

                    // Export our datatable to an excel sheet and save it.
                    try
                    {
                        Export(dt: dt,
                               FileName: string.Format("Comp Scheduled Job {0}", Job.Rows[j]["JobID"].ToString()),
                               EmailTo: Job.Rows[j]["Email"].ToString().Trim());
                    }
                    catch (Exception ex)
                    {
                        ErrorLog.Write(SourceName: "Comp Wisp",
                                       ErrorMessage: ex.ToString(),
                                       MethodOrFunction: "ProcessJob",
                                       MemberName: Environment.UserName,
                                       ErrorDescription: string.Format("Failed to create excel file on Job {0}.", Job.Rows[j]["JobID"].ToString()));
                    }

                    // Update the job to Complete in our database.
                    using (SqlConnection con = new SqlConnection(dbComp))
                    {
                        Console.WriteLine("Updating Job Status");
                        using (SqlDataReader dr = Connect.ExecuteReader("[CompWisp_UpdateJobStatus]", con,
                                                                           new SqlParameter("@JobID", Job.Rows[j]["JobID"].ToString()))) { }
                    }
                    Console.Write("Complete\r\n\r\n");
                }
                GetJob();
            }
            catch (Exception ex)
            {
                ErrorLog.Write(SourceName: "CompWisp",
                               ErrorMessage: ex.ToString(),
                               MethodOrFunction: "ProcessJob");
            }
        }

当任务计划程序运行作业时,与手动运行应用程序时,对Directory.GetCurrentDirectory()的调用可能返回不同的值。

要对此进行测试,您可以尝试在服务器上搜索丢失的excel文件。

您可以尝试以下几种方法:

  1. 确保在其下运行任务的用户未启用UAC。 这可能会导致执行问题。

  2. 同样,请参阅以下文章是否有帮助。 答案(也许)是第一个列出的答案: MSDN论坛问题

我无法让Task Scheduler运行创建Excel电子表格的应用程序。 我需要创建此文件夹才能使其正常运行。 C:\\ windows \\ syswow64 \\ config \\ systemprofile \\ desktop

这是我从那里获得信息的地方

使用Microsoft Interop以64位阅读Excel

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM