jess romero

jess romero

  • NA
  • 14
  • 23.5k

Converting textfile to excel file in a clear format in c#2008 please help

Feb 11 2011 1:14 AM

Hi Sir,

Could anyone help me to revise or update this code in converting text file to excel in proper format (with header  and proper column)? the code below converts text file to excel file but I need the clear output in excel file in a standard format by column. Here's the sample text file data input and my code.

Here's my sample text file:

tag        ord_no   ref_no  acv_no prog_no      prog_date                  acv_type_id
---        ----------- --------   -------   ----------      --------------------------    -------------
 DL      6636500      1          9           11        Feb  3 2011 11:11AM     DLINSTLN
 DL      6649978      1        10            8         Feb  4 2011 12:29PM     DLINSTLN
 DL      6722809      1          6            3         Feb  2 2011  5:35PM      DLINSTLN

Here's my C# code in converting text file to excel:

private void Convert_TextFile_To_ExcelFile() { // Open the text file in Excel. m_objExcel = new Microsoft.Office.Interop.Excel.Application(); m_objBooks = (Microsoft.Office.Interop.Excel.Workbooks)m_objExcel.Workbooks; m_objBooks.OpenText(m_strSampleFolder + "sampledoc.txt", Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, 1, Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited, Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierDoubleQuote, false, true, false, false, false, false, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt); m_objBook = m_objExcel.ActiveWorkbook; // Save the text file in the typical workbook format and quit Excel. m_objBook.SaveAs(m_strSampleFolder + "sampledoc.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, m_objOpt, m_objOpt, m_objOpt, m_objOpt, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt); m_objBook.Close(false, m_objOpt, m_objOpt); m_objExcel.Quit(); }

Answers (4)