try
{
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = false;
oXL.UserControl = false;
//Get a new workbook.
oWBook = (Excel._Workbook)(oXL.Workbooks.Open(strFilename, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false));
oSheet = (Excel._Worksheet)oWBook.ActiveSheet;
oSheet.Activate();
//Create header row 2
oXL.ReferenceStyle = Microsoft.Office.Interop.Excel.XlReferenceStyle.xlR1C1;
Excel.Range oRange2 = oSheet.get_Range("A2", "A2");
oRange2.FormulaR1C1 = HeaderLine1;
//Create header row 2
oRange2 = oSheet.get_Range("A3", "A3");
oRange2.FormulaR1C1 = HeaderLine2;
oRange2 = oSheet.get_Range("A" + StartRowNumber.ToString().Trim(), Missing.Value);
var temp = oSheet.QueryTables.Add("TEXT;" + TextFileName, oRange2, Missing.Value);
temp.Name = TextFileName.ToUpper().Replace(".TXT", "");
temp.PreserveFormatting = true;
temp.AdjustColumnWidth = false;
temp.TextFileStartRow = 1;
temp.TextFileParseType = Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited;
temp.TextFileTextQualifier = Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierDoubleQuote;
temp.TextFileConsecutiveDelimiter = false;
temp.TextFileTabDelimiter = true;
temp.TextFileSemicolonDelimiter = false;
temp.TextFileCommaDelimiter = false;
temp.TextFileSpaceDelimiter = false;
temp.TextFileOtherDelimiter = "|";
temp.TextFilePlatform = 1252;//65001;//1252;//65001;//1200;
//http://en.wikipedia.org/wiki/Code_page
temp.TextFileColumnDataTypes = GetDataType(ColumnDataType);
temp.TextFileTrailingMinusNumbers = true;
temp.Refresh(false);
//temp.Connection.ToString();
temp.Delete();
oXL.ReferenceStyle = Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1;
oWBook.Save();
oWBook.Close(false, Missing.Value, Missing.Value);
}
catch (Exception ex)
{
oWBook.Close(false, Missing.Value, Missing.Value);
}
finally
{
oXL.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
oSheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWBook);
oWBook = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
oXL = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}