我在ssis包中使用了脚本任务,每个月自动生成大量的excel文件。
直到现在脚本任务工作正常,但本月失败,当我在下面显示调试错误时
results = re.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
我该如何解决这个问题?
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
// TODO: Add your code here
string ssrsReport;
string destination;
string monthName;
string yyyy;
string currentReportName;
string currentReportShortName;
string password;
string filetype;
string version;
yyyy = (string)Dts.Variables["varYear"].Value;
monthName = (string)Dts.Variables["varMonthName"].Value;
filetype = (string)Dts.Variables["varFileType"].Value;
version = (string)Dts.Variables["varVersion"].Value;
// The SQL Task already ensures that these following five values are not null:
currentReportShortName = (string)Dts.Variables["varRSParameter"].Value;
currentReportName = (string)Dts.Variables["varRSReportName"].Value;
password = (string)Dts.Variables["varRSPassword"].Value;
destination = (string)Dts.Variables["varDestinationPath"].Value + "\\" + "Data - " + currentReportName + " " + monthName + " - " + yyyy + "." + filetype;
ssrsReport = (string)Dts.Variables["varSSRSReport"].Value;
try
{
CreateFolder((string)Dts.Variables["varDestinationPath"].Value);
RenderReport(destination, ssrsReport, currentReportShortName, password, filetype, version);
Dts.TaskResult = (int)ScriptResults.Success;
}
catch
{
Dts.TaskResult = (int)ScriptResults.Failure;
throw;
}
}
private void RenderReport(string destinationFile, string ssrsReport, string agent, string password, string filetype, string version)
{
string historyID = null;
string deviceInfo = null;
string format = null;
if (filetype == "xls")
{
format = "Excel"; //Can be XML, NULL, CSV, IMAGE, PDF, HTML4.0, HTML3.2, MHTML, EXCEL, and HTMLOWC
}
else if (filetype == "pdf")
{
format = "PDF";
}
Byte[] results;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
ReportExecutionService.Warning[] warnings = null;
string[] streamIDs = null;
ReportExecutionService.ReportExecutionService re = new ReportExecutionService.ReportExecutionService();
re.Credentials = System.Net.CredentialCache.DefaultCredentials;
re.Timeout = 600000;
try
{
ReportExecutionService.ExecutionInfo ei = re.LoadReport(ssrsReport, historyID);
// If we need to generate a parametised report then do that here:
// check if multiple agent parameters are being passed for this report
ReportExecutionService.ParameterValue[] rptParameters = new ReportExecutionService.ParameterValue[4];
rptParameters[0] = new ReportExecutionService.ParameterValue();
rptParameters[0].Name = "DataName";
rptParameters[0].Value = agent;
rptParameters[1] = new ReportExecutionService.ParameterValue();
rptParameters[1].Name = "Year";
rptParameters[1].Value = (string)Dts.Variables["varYear"].Value;
rptParameters[2] = new ReportExecutionService.ParameterValue();
rptParameters[2].Name = "Month";
rptParameters[2].Value = (string)Dts.Variables["varMonth"].Value;
rptParameters[3] = new ReportExecutionService.ParameterValue();
rptParameters[3].Name = "version";
rptParameters[3].Value = (string)Dts.Variables["varVersion"].Value;
re.SetExecutionParameters(rptParameters, "en-GB");
results = re.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
FileStream stream = File.Create(destinationFile, results.Length);
stream.Write(results, 0, results.Length);
stream.Close();
if (password != "NONE" && password.Length > 0)
{
PasswordProtectExcel(destinationFile, password);
}
}
catch (Exception e)
{
throw new Exception("Error caught within RenderReport method: ", e);
}
}
private void CreateFolder(string path)
{
try
{
bool exists = Directory.Exists((string)Dts.Variables["varDestinationPath"].Value);
if (!exists)
System.IO.Directory.CreateDirectory((string)Dts.Variables["varDestinationPath"].Value);
}
catch (Exception e)
{
throw new Exception("Error caught within CreateFolder method: ", e);
}
}
// Code adapted from http://www.sqlservercentral.com/articles/Integration+Services+(SSIS)/71222/
private void PasswordProtectExcel(string excelSourceFile, string password)
{
Microsoft.Office.Interop.Excel.Application excelApp = null;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
try
{
//open the Excel application
excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
excelWorkbook = excelApp.Workbooks.Open(excelSourceFile, 0, false, Type.Missing, Type.Missing, Type.Missing, true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", false, false,
0, false, false, false);
//Save the file with the "excelSourceFile" name
excelApp.DisplayAlerts = false;
excelWorkbook.SaveAs(excelSourceFile, Type.Missing, password, //password is used in the SaveAs function
Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, true, Type.Missing,
Type.Missing, Type.Missing);
}
catch (Exception e)
{
throw new Exception("Error caught within PasswordProtectExcel method: ", e);
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
//Close the workbook
excelWorkbook.Close(true, excelSourceFile, false);
Marshal.FinalReleaseComObject(excelWorkbook);
excelWorkbook = null;
//Quit the application
excelApp.Quit();
Marshal.FinalReleaseComObject(excelApp);
excelApp = null;
}
}
}
}