When I run a SSIS package Interactively I am getting the error Object reference not set to an instance of an object on a Script task during run time.
So I debugged the code by using break points and after going step by step the code fails at this line objExcelWbk.Close(true, Type.Missing, Type.Missing);
I have checked all my references they are all there and the imports are fine too.
The function is provided below
public void FormatExcel_DVP(string strFinalFileName, string ExcelOutputFolder, SqlConnection Conn)
{
Microsoft.Office.Interop.Excel.ApplicationClass objExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook objExcelWbk = default(Excel.Workbook);
Microsoft.Office.Interop.Excel.Worksheet objWrksheet = default(Excel.Worksheet);
string sFilePath = string.Empty;
DataSet ds = new DataSet();
string DVP_Name = string.Empty;
string sFilename = string.Empty;
int RowCount = 0;
try
{
SqlCommand cmd = new SqlCommand("select distinct LTRIM(RTRIM(DVP_Name))as DVP_Name from SBBCP_DVP_SVP Order By DVP_Name", Conn);
SqlDataAdapter _da = new SqlDataAdapter(cmd);
_da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
DVP_Name = dr[0].ToString().Trim().Replace("'", "''");
sFilename = DVP_Name.Trim();
sFilePath = strFinalFileName + ExcelOutputFolder.Trim() + sFilename;
if (System.IO.File.Exists(sFilePath))
{
objExcelWbk = objExcelApp.Workbooks.Open(sFilePath.Trim(), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
objExcelApp.DisplayAlerts = false;
objExcelApp.Visible = false;
objWrksheet = (Excel.Worksheet)objExcelWbk.Worksheets["Details"];
((Microsoft.Office.Interop.Excel._Worksheet)objWrksheet).Activate();
Microsoft.Office.Interop.Excel.Range range;
range = (Excel.Range)objWrksheet.get_Range("A1:J1", Type.Missing);
range.Interior.ColorIndex = 15;
range.Interior.Pattern = Microsoft.Office.Interop.Excel.XlPattern.xlPatternSolid;
range.NumberFormat = "@";
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
range.WrapText = true;
range.Font.Bold = true;
range.Font.Name = "Arial";
range.Font.Size = 10;
range.AutoFilter(1, Type.Missing, Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);
RowCount = objWrksheet.UsedRange.Rows.Count;
range = objWrksheet.get_Range("A2:J2", "A" + RowCount + ":J" + RowCount);
range.WrapText = true;
FormatPivotTable(ref objExcelWbk, "Summary");
objExcelWbk.SaveAs(sFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
}
}
objExcelWbk.Close(true, Type.Missing, Type.Missing);
objExcelApp.Quit();
}
catch (Exception e)
{
throw e;
}
}