Hi !,
am new for c# and SSIS Package Creation , I am Trying to Read Excel file, and load the value into Sqlserver using SSIS package . My Problem is , After Execution of SSIS package Still Running EXCEL.exe Process in my server. i need to kill that process . I post my Code Exactly where am release my excel file object , please guide me where am going to wrong?
Server Configuration
OS:windows7
SqlServer :2008r2
Framework:3.5
please give me some suggestion to correct my error .
Here is My Code:
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
var missing = System.Reflection.Missing.Value;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(filename, false, true, missing, missing, missing, true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, '\t', false, false, 0, false, true, 0);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range xlRange = xlWorkSheet.UsedRange;
Array myValues = (Array)xlRange.Cells.Value2;
int vertical = myValues.GetLength(0);int horizontal = myValues.GetLength(1);
System.Data.DataTable dt = new System.Data.DataTable();
bool isNameRead = false;
// get header information
for (int i = 1; i <= horizontal; i++)
{
string cellValue1 = "";
object cellObj = myValues.GetValue(1, i);
if (cellObj != null)
{
cellValue1 = myValues.GetValue(1, i).ToString();
if (cellValue1 == "Name")
{
if (!isNameRead)
{
dt.Columns.Add(new DataColumn(cellValue1));
isNameRead = true;
}
else
{
dt.Columns.Add(new DataColumn(cellValue1 + 1));
isNameRead = false;
}
}
else
{
dt.Columns.Add(new DataColumn(cellValue1));
}
}
}
// Get the row information
for (int a = 2; a <= vertical; a++)
{
//string cellrowvalue = "";
string isemt = "";
object[] poop = new object[horizontal];
for (int b = 1; b <= horizontal; b++)
{
isemt =(string) myValues.GetValue(a, b);
if (isemt != null)
{
poop[b - 1] = myValues.GetValue(a, b);
}
}
DataRow row = dt.NewRow();
row.ItemArray = poop;
dt.Rows.Add(row);
}
xlWorkBook.Close(true, missing, missing);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
return dt;
releaseObject
private void releaseObject(object obj){
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
Thanks
Parthasarathi Purushothaman