Outputting to a Excel File

Help I am outputting my VB program to a excel file. I am trying to get the Data into the appropiate cells in Excel and do not understand how this occurs, all my data goes into the 1 column and straight down. I can get the info into the excel file just not into the right cells. Any help would be appreciated


Doug

Comments

  • Hi Doug,

    Take a look at the following and see if this helps at all. I've got some features of an application that I'm working on that exports information in a datareader to an excel file as either a .csv file or an .xls file.

    -sFilePath represents a path ("C:DataExport.csv")
    -dr is a data reader
    -an excel file I believe is tab separated so where the code has...
    [code]
    sOutputLine = sOutputLine + .GetName(i).ToString() + "," 'file is csv
    [/code]
    ...you might have to replace the comma with the tab character (chr(9) I think)
    Here's the full code
    [code]
    oFS = New FileStream(sFilePath, FileMode.Create, FileAccess.Write)
    oSW = New StreamWriter(oFS)
    With dr 'datareader holding the data
    'Writing the Header Information
    For i As Long = 0 To .FieldCount - 1
    sOutputLine = sOutputLine + .GetName(i).ToString() + "," 'file is csv
    Next i
    oSW.WriteLine(sOutputLine)
    sOutputLine = ""
    While .Read()
    'Writing the Data
    For i As Long = 0 To .FieldCount - 1
    sOutputLine = sOutputLine + .GetValue(i).ToString() + "," 'file is csv
    Next i
    oSW.WriteLine(sOutputLine)
    sOutputLine = ""
    End While
    End With
    [/code]
    I've been wondering if a dataset has some feature that writes to excel automatically but haven't come across anything yet. Hope this is helpful.

    Cheers,
    Chris

    : Help I am outputting my VB program to a excel file. I am trying to get the Data into the appropiate cells in Excel and do not understand how this occurs, all my data goes into the 1 column and straight down. I can get the info into the excel file just not into the right cells. Any help would be appreciated
    :
    :
    : Doug
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion