当我以交互方式运行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;
}
}
答案 0 :(得分:1)
在您的代码中
for each row
{
if file exists
{
open spreadsheet.
do stuff
}
}
Close spreadsheet
问题是可能是因为你的电子表格从未打开并试图关闭它但尚未设置的情况。
你应该在打开它的同一范围内关闭电子表格,所以在我的伪代码中,在“do stuff”之后 - 那么,如果你打开它,因为它没有barf或死,你有一些你可以关闭。