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

C#Excel对象引用未设置为对象的实例,c#,sql-server,ssis,sql-job,C#,Sql Server,Ssis,Sql Job,我知道有很多问题说对象引用未设置为对象的实例,但我的问题不同,但给出了相同的错误 我正在SSIS包中运行此脚本,该包位于脚本任务中 当通过visual studio 2013 professional运行时,无论是在本地还是在服务器(部署包的位置)上,它都可以很好地构建和遵守 但是,当我运行sql作业时,包失败,并在我的日志文件中写入以下错误格式化文件方法已启动。。 出现错误。请查看错误消息:对象引用未设置为对象的实例。 我无法理解为什么在服务器上它在visual studio中运行时成功构建,

我知道有很多问题说对象引用未设置为对象的实例,但我的问题不同,但给出了相同的错误

我正在SSIS包中运行此脚本,该包位于脚本任务中 当通过visual studio 2013 professional运行时,无论是在本地还是在服务器(部署包的位置)上,它都可以很好地构建和遵守

但是,当我运行sql作业时,包失败,并在我的日志文件中写入以下错误格式化文件方法已启动。。 出现错误。请查看错误消息:对象引用未设置为对象的实例。

我无法理解为什么在服务器上它在visual studio中运行时成功构建,但在sql作业中失败

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.CSharp;

    public void FormatFile(string sFilename)
        {

            Microsoft.Office.Interop.Excel.Application objExcelApp = new Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook objExcelWbk = default(Excel.Workbook);
            Microsoft.Office.Interop.Excel.Worksheet objWrksheetGP = default(Excel.Worksheet);
            Microsoft.Office.Interop.Excel.Worksheet objWrksheetSummary = default(Excel.Worksheet);
            Microsoft.Office.Interop.Excel.Worksheet objWrksheetDetail = default(Excel.Worksheet);

            try
            {
                object missing = Missing.Value;
                int RowCount, RowCountGP, RowCountDetail;
                int cStart = 2, RowStart = 0, RowEnd = 2;


                objExcelApp.DisplayAlerts = false;
                objExcelApp.Visible = false;
                objExcelWbk = objExcelApp.Workbooks.Open(sFilename, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, true);
                objWrksheetGP = (Excel.Worksheet)objExcelWbk.Worksheets["Template"];
                objWrksheetDetail = (Excel.Worksheet)objExcelWbk.Worksheets["Detail"];
                objWrksheetSummary = (Excel.Worksheet)objExcelWbk.Worksheets["Summary"];
                ((Microsoft.Office.Interop.Excel._Worksheet)objWrksheetDetail).Activate();
                RowCountGP = objWrksheetGP.UsedRange.Rows.Count;
                RowCount = objWrksheetDetail.UsedRange.Rows.Count;
                Excel.Range Crange, Crange1;

                Crange = (Excel.Range)objWrksheetDetail.get_Range("B2:AO" + RowCount.ToString(), missing);
                Crange.Copy(missing);
                Crange1 = (Excel.Range)objWrksheetSummary.Cells[3, 3];
                Crange1.PasteSpecial(Excel.XlPasteType.xlPasteValues, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, missing, missing);

                RunFormat(ref objWrksheetGP, 'B', RowCountGP, "Number");
                while (cStart <= RowCountGP)
                {
                    Crange1 = (Excel.Range)objWrksheetGP.get_Range("B" + cStart.ToString() + ":B" + cStart.ToString(), missing);
                    RowCountDetail = Convert.ToInt32(Crange1.Value2);
                    RowStart = RowEnd + 1;
                    RowEnd = RowStart + RowCountDetail - 1;
                    Crange1 = (Excel.Range)objWrksheetSummary.get_Range("A" + RowEnd.ToString() + ":AP" + RowEnd.ToString(), missing);
                    Crange1.Interior.ColorIndex = 15;
                    Crange1.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
                    Crange1.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlMedium;
                    Crange1.Borders[Excel.XlBordersIndex.xlEdgeBottom].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
                    cStart = cStart + 1;
                }

                //Commented line of code as this is not present is vb code from which code is regenerated.
                /*Crange = objWrksheetDetail.get_Range("E2:E" + RowCount.ToString(), missing);
                Crange1 = objWrksheetSummary.get_Range("E3:E" + (RowCount + 1).ToString(), missing);
                Crange1.Value2 = Crange.Value2;*/

                Crange1 = objWrksheetSummary.get_Range("D2:E" + RowCount.ToString(), missing);
                Crange1.Font.Bold = true;

                Crange1 = objWrksheetSummary.get_Range("D2:D" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("E2:E" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("I2:I" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("L2:L" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("O2:O" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("R2:R" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("U2:U" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("X2:X" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("AA2:AA" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("AD2:AD" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("AG2:AG" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("AJ2:AJ" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("AM2:AM" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

                Crange1 = objWrksheetSummary.get_Range("AP2:AP" + (RowCount + 1).ToString(), missing);
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;
                Crange1.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;


                objWrksheetDetail.Visible = Excel.XlSheetVisibility.xlSheetHidden;
                objWrksheetGP.Visible = Excel.XlSheetVisibility.xlSheetHidden;
                objExcelWbk.Save();
                objExcelWbk.Close(true, missing, missing);
                objExcelApp.Quit();
            }
            catch
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWbk);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp);
                throw;
            }
            finally
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelWbk);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcelApp);
                System.GC.Collect();
            }
        }
使用系统;
使用系统数据;
使用Microsoft.SqlServer.Dts.Runtime;
使用System.Windows.Forms;
使用System.IO;
使用系统文本;
使用System.Data.SqlClient;
使用系统诊断;
运用系统反思;
使用Excel=Microsoft.Office.Interop.Excel;
使用Microsoft.CSharp;
公共void格式文件(字符串sFilename)
{
Microsoft.Office.Interop.Excel.Application objExcelApp=新建Excel.Application();
Microsoft.Office.Interop.Excel.Workbook objExcelWbk=默认值(Excel.Workbook);
Microsoft.Office.Interop.Excel.Worksheet objWrksheetGP=默认值(Excel.Worksheet);
Microsoft.Office.Interop.Excel.Worksheet objWrksheetSummary=默认值(Excel.Worksheet);
Microsoft.Office.Interop.Excel.Worksheet objWrksheetDetail=默认值(Excel.Worksheet);
尝试
{
缺少对象=缺少.Value;
int RowCount、RowCountGP、RowCountDetail;
int cStart=2,RowStart=0,RowEnd=2;
objExcelApp.DisplayAlerts=false;
objExcelApp.Visible=false;
objExcelWbk=objExcelApp.Workbooks.Open(sFilename、missing、missing、missing、missing、missing、missing、missing、missing、missing、missing、missing、true);
objWrksheetGP=(Excel.Worksheet)objExcelWbk.Worksheets[“模板”];
objWrksheetDetail=(Excel.Worksheet)objExcelWbk.Worksheets[“Detail”];
objWrksheetSummary=(Excel.Worksheet)objExcelWbk.Worksheets[“摘要”];
((Microsoft.Office.Interop.Excel._工作表)objWrksheetDetail).Activate();
RowCountGP=objWrksheetGP.UsedRange.Rows.Count;
RowCount=objWrksheetDetail.UsedRange.Rows.Count;
Excel.范围起重机,起重机1;
Crange=(Excel.Range)objWrksheetDetail.get_Range(“B2:AO”+RowCount.ToString(),缺失);
起重机。副本(缺失);
Crange1=(Excel.Range)objWrksheetSummary.Cells[3,3];
Crange1.PasteSpecial(Excel.XlPasteType.xlPasteValues,Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,缺失,缺失);
运行格式(参考objWrksheetGP,‘B’,行计数GP,“编号”);

虽然(cStart我想指出,在这种情况下,Office interop是由Microsoft使用的。@CharlesMager我使用的Office版本是2010,其他一些软件包也使用Microsoft.Office.interop.Excel,它们运行良好,但由于KB文章中列出的所有原因,仍然不建议使用它。有很多托管替代软件ves(这也有速度快得多的好处)。如果要解决此问题,需要对其进行调试以确定空值。@CharlesMager我检查了变量并成功构建了它。在运行sql作业时,是否有方法进行调试?