reading from text file to excel file

Discussion in 'C Programming' started by mail2atulmehta@yahoo.com, Apr 12, 2005.

  1. Guest

    Hi,
    I do not know if this is right place for this, but i need some help. I
    have a text file, whose values are seprated by a delimiter. I want to
    open this file in excel, ( not import it) . I have written the driver
    prg in c#. The code opens the file, but it is not writting the values
    from text file into excel file. I can not figure out the problem here.
    This is my code:
    StreamReader dataFileReader = null;
    FileInfo file = null;
    Excel.Application ExcelObj = new Excel.Application();
    Excel.Workbook workbook = null;
    Excel.Range currentCell = null;
    Excel.Worksheet worksheet = null;
    string txtoutput = null;
    Excel.Sheets excelSheets = null;
    Excel.Range sampleResults = null;


    }


    public void transformation()
    {


    try
    {
    //GC.Collect();
    workbook =
    ExcelObj.Workbooks.Add(Excel.X­lWBATemplate.xlWBATWorksheet);
    excelSheets = workbook.Worksheets;
    worksheet =
    (Excel.Worksheet)excelSheets.g­et_Item(1);
    if (worksheet == null)
    {
    Console.WriteLine ("ERROR in
    worksheet == null");
    }
    // Opens the text file.
    string inFile ="C:\\dip\\output2.txt";
    using (StreamReader reader = new
    StreamReader(inFile) )
    {
    String currentLine;
    sampleResults =

    worksheet.get_Range("A1", Type.Missing);
    int row = 1;
    int count = 0;
    // Populates the table of
    sample results.
    // Continues reading lines
    until there are no more.
    while ((currentLine =
    reader.ReadLine()) != null)
    {
    count++;

    Console.WriteLine("Reading string # {0} to populate excel row #
    {1}", count, count);
    string[] textInfo =
    currentLine.Split(',');
    // Places each sample
    result into the appropriate cell.
    for (int i = 0; i <
    textInfo.Length; i++)
    {
    currentCell =

    (Excel.Range)sampleResults.Cel­ls[row, i+1];

    currentCell.Value2 =
    textInfo.Trim();
    }
    row++;
    currentLine =
    reader.ReadLine();
    }

    //thisWorkbook.SaveAs("C://dip­//test.xls",Excel.XlFileFormat­.xlWorkbookNormal,

    null,null,false,false,Excel.Xl­SaveAsAccessMode.xlShared,fals­e,false,null,null);


    workbook.SaveAs("C://dip//test­.xls",Excel.XlFileFormat.xlWor­kbookNormal,

    null,null,false,false,Excel.Xl­SaveAsAccessMode.xlShared,fals­e,false,null,null);

    }
    }


    catch(Exception ex)
    {
    //Debug.WriteLine(ex.Message +
    ex.StackTrace);

    Console.WriteLine("{0}\n{1}",e­x.Message,ex.StackTrace);
    //Thread.Sleep(3000);
    }
    finally
    {
    //excelTW.Close();
    //dataFileReader.Close();
    //delete files for one time usage
    //File.Delete(output2);
    //close resources, allocated for the
    Excel process
    workbook.Close(false, Type.Missing,
    Type.Missing);
    ExcelObj.Workbooks.Close();
    ExcelObj.Quit();

    //System.Runtime.InteropServic­es.Marshal.ReleaseComObject
    (currentCell);

    //System.Runtime.InteropServic­es.Marshal.ReleaseComObject
    (ExcelObj);

    //System.Runtime.InteropServic­es.Marshal.ReleaseComObject
    (worksheet);

    //System.Runtime.InteropServic­es.Marshal.ReleaseComObject
    (workbook);
    worksheet=null;
    workbook=null;
    ExcelObj = null;
    //try to force GC
    GC.Collect();


    }
    }
     
    , Apr 12, 2005
    #1
    1. Advertising

  2. "" <> writes:
    > I do not know if this is right place for this, but i need some help. I
    > have a text file, whose values are seprated by a delimiter. I want to
    > open this file in excel, ( not import it) . I have written the driver
    > prg in c#.

    [SNIP]

    No, this isn't the right place. C and C# are two different languages;
    we only know about one of them here. You probably want one of the
    microsoft.* groups, or perhaps comp.os.ms-windows.programmer.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 12, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    676
    Kevin Spencer
    Nov 11, 2004
  2. Luis Esteban Valencia
    Replies:
    1
    Views:
    1,395
    Carl Prothman [MVP]
    Jan 12, 2005
  3. pps!
    Replies:
    0
    Views:
    1,033
  4. =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=

    Problem with Excel reports ::::Excel 2003 Migration To Excel 2007

    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=, Oct 5, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    1,512
    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=
    Oct 24, 2007
Loading...

Share This Page