2

当我以交互方式运行 SSIS 包时,我收到错误Object reference not set to an instance on an Script task during run time。

所以我通过使用断点调试代码,一步一步地代码在这一行失败objExcelWbk.Close(true, Type.Missing, Type.Missing);

我检查了我所有的参考资料,它们都在那里,进口也很好。

功能如下

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;
    }
}
4

1 回答 1

1

在您的代码中,您有

for each row
{
  if file exists 
  {
     open spreadsheet.
     do stuff
  }
}
Close spreadsheet

问题是这可能是您的电子表格从未打开过并且试图关闭它但尚未设置的情况。

您应该在打开电子表格的同一范围内关闭电子表格,因此在我的伪代码中,在“做事”之后 - 因为那时,如果您打开它,因为它没有呕吐或死亡,您有一些可以关闭的东西。

于 2016-06-07T14:18:59.297 回答