Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

Creating Excel file using SSIS

$
0
0
Hi,
I have strange problem while creating an SSIS package which convert CSV file into excel file using Excel Interop assembly. Problem is strange because when I executing it directly through IDE or through File system then it’s working fine but as well as I am scheduled it through SQL Server 2008r2 then it gives exception that object reference not set. I am using SQL SERVER 2008 R2 with Microsoft Office 2010 Installed on my system. All task are done on locally.
Here is my code for creating excel file.

    public string ConvertCSVToExcel(string csvFilePath, string sheetName)
        {
            ScriptMain.errorLog.Append("Convert CSV To Excel Method Is Called.");

            Microsoft.Office.Interop.Excel.Application objExcelApplication = null;
            Microsoft.Office.Interop.Excel.Workbook objExcelWorkBook = null;
            Microsoft.Office.Interop.Excel.Worksheet objExcelWorkSheet = null;
            Microsoft.Office.Interop.Excel.Sheets objExcelSheets = null;

            try
            {
                string tempExcelFilePath = this.CreateTempFilePath(csvFilePath);

                ScriptMain.errorLog.Append(tempExcelFilePath);

                this.DeleteFileIfExists(tempExcelFilePath);

                ScriptMain.errorLog.Append("File Deleted");

                objExcelApplication = new Microsoft.Office.Interop.Excel.Application();
                objExcelWorkBook = objExcelApplication.Workbooks.Open(csvFilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                ScriptMain.errorLog.Append("Creating object of Excel Addons.");

                objExcelSheets = objExcelWorkBook.Sheets;
                objExcelWorkSheet = objExcelSheets.get_Item(1) as Microsoft.Office.Interop.Excel.Worksheet;

                ScriptMain.errorLog.Append("Creating WorkSheet Object");

                objExcelWorkSheet.Name = sheetName;

                objExcelWorkBook.SaveAs(tempExcelFilePath, XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive
                    , Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                ScriptMain.errorLog.Append("Saving WorkSheet.");

                objExcelWorkBook.Close(false , false , false);

                ScriptMain.errorLog.Append("Closing WorkSheet Object");

                return tempExcelFilePath;
            }
            catch
            {
                throw;
            }
            finally
            {
                Marshal.ReleaseComObject(objExcelWorkSheet);
                Marshal.ReleaseComObject(objExcelSheets);
                Marshal.ReleaseComObject(objExcelWorkBook);
                Marshal.ReleaseComObject(objExcelApplication);
            }
        }

Here

    "objExcelApplication = new Microsoft.Office.Interop.Excel.Application();
                    objExcelWorkBook = objExcelApplication.Workbooks.Open(csvFilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);"

above line giving me exception. I am not able to find out reason behind this. I am looking for some helpful links or points.

Thanks.

Viewing all articles
Browse latest Browse all 24688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>