C# Excel 工作表对象引用未设置为对象的实例

C# Excel Worksheet Object reference not set to an instance of an object

当我 运行 交互式 SSIS 包时,在 运行 时间。

所以我使用断点调试了代码,一步步执行后,代码在这一行失败了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;
    }
}

在你的代码中你有

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

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

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