1

I currently have a routine which reads rows/columns and writes into a SQL table from a SELECT statement.

We were thinking of then reading the SQL table and updating an empty Excel worksheet. What I would like to do is read and then update the data directly into Excel.

My program currently does the following:

  1. Opening the workbook.
  2. Accessing the different worksheets.
  3. Saving As a new workbook name.
  4. Closing Excel.

I just need an example that:

  1. will allow the first row to be the column names
  2. then read out the data into the various cells for each row.
Prix
  • 19,417
  • 15
  • 73
  • 132

2 Answers2

0

I do not recommend using COM Interop either.

You can find here a sample about exporting dataset to Excel in C#.

It requires an Excel tool named EasyXLS.

alex.pulver
  • 2,107
  • 2
  • 31
  • 31
0

to open the file you can do it like that:

Dim exAppl As New Excel.Application    
exAppl.Visible = True
Dim exMappe As Excel.Workbook = exAppl.Workbooks.Open("C:\... .xlsx", , False)

You can acces to a Cell with

Console.WriteLine(exAppl.Range("A2").Value())

or when you whant to go throw it use something like that:

For i As Integer = 1 To exAppl.Rows.Count

    Dim a As Excel.Range = exAppl.Rows(i)

    For j As Integer = 1 To a.Columns.Count
        Dim b As Excel.Range = a.Columns(j)
        Console.WriteLine(b.Value)
    Next
Next
deru
  • 460
  • 1
  • 4
  • 15