Link to home
Start Free TrialLog in
Avatar of expertsexchangehunter
expertsexchangehunter

asked on

How do I add values to the Excel Column Headers?

I am using MDE 2003 to create a Excel file that will be a report. I want to add values to the column header. I have code that adds the calues to row 1 now.

                For intColumn = 0 To ds.Tables(0).Columns.Count - 1
                    .Cells(1, intColumn + 1).Value = ds.Tables(0).Columns(intColumn).ColumnName.ToString
                Next
 How Do I add the values to the column headers?
Avatar of expertsexchangehunter
expertsexchangehunter

ASKER

Here is more of the code
            With myExcel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add().Worksheets(1).Select()
                'TO Display the column name in the the excel file.  
                For intColumn = 0 To ds.Tables(0).Columns.Count - 1
                    .Cells(1, intColumn + 1).Value = ds.Tables(0).Columns(intColumn).ColumnName.ToString
                Next
Avatar of Rory Archibald
You can't. The only thing you can do is hide the column and row identifiers (you can't just do the column headers) and use Row 1 as a header instead.
Regards,
Rory
OK. The code adds values in the first row and then this code adds the row values. Except it adds them in the first row also. How do I add the actual values starting 1 row below the row headers which are in row 1?

                   For intRow = 0 To ds.Tables(0).Rows.Count - 1
                    For intColumnValue = 0 To ds.Tables(0).Columns.Count - 1
                        .Cells(1, intColumnValue + 1).Value = ds.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString
                    Next
                Next
You could change it to:
 

For intRow = 0 To ds.Tables(0).Rows.Count - 1
                    For intColumnValue = 0 To ds.Tables(0).Columns.Count - 1
                        .Cells(intRow + 2, intColumnValue + 1).Value = ds.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString
                    Next
                Next

Open in new window

Ok perfect. Can you show me how can I display the spreadsheet from the program without creating the file? Thanks.
What do you mean by "without creating the file"?
I want to execute the code described above and then have the spreadsheet displayed.
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I added that and it looks like it's trying to start  Excel as seen on the browser where you see the bar when things are loading. Here is the code and am I missing anything?

        Try
            Const conControlNumber As Int32 = 4 'SQL column number column

            Dim sqlConnection As New SqlConnection(CStr(Application("myConnString")))

            Dim strsql = Session.Item("strQRY")

             Dim da As New SqlClient.SqlDataAdapter
            da.SelectCommand = New SqlClient.SqlCommand(strsql, sqlConnection)
            da.Fill(ds)

            Dim intColumn, intRow, intColumnValue As Integer
 
            Dim dt As DataTable = ds.Tables("Qry")
            strControlNumber = ds.Tables(0).Columns(conControlNumber).ToString()
            Dim myExcel As Object
            myExcel = CreateObject("Excel.Application")

            With myExcel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add().Worksheets(1).Select()
                'TO Display the column name in the the excel file.  
                For intColumn = 0 To ds.Tables(0).Columns.Count - 1
                    .Cells(1, intColumn + 1).Value = ds.Tables(0).Columns(intColumn).ColumnName.ToString
                Next
                'For displaying the column value row-by-row in the the excel file.        
                For intRow = 0 To ds.Tables(0).Rows.Count - 1
                    For intColumnValue = 0 To ds.Tables(0).Columns.Count - 1
                        .Cells(intRow + 2, intColumnValue + 1).Value = ds.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString
                        '                        .Cells(intRow + 1, intColumnValue + 1).Value = ds.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString
                    Next
                Next

                 .ActiveWorkbook.Close()
            End With

            myExcel.visible = True


            sqlConnection.Close()

        Catch ex As Exception
            ' Display error message
         End Try
You should remove this line:
.ActiveWorkbook.Close()
as it's closing the workbook you just created, but other than that it looks OK to me.
I removed ActiveWorkbook.Close() and it doesn't even try now.
Doesn't even try what?
This is the line that starts Excel:
  myExcel = CreateObject("Excel.Application")


the line I asked you to remove simply closes the workbook you just created so I don't see the point of it.
I can tell because it finishes the code and displays the browser with no indication that anything is loading and there are not any windows opening. Where as before with the workbook close I could see some activity in the browder loading area at first. Except the bar for loading would fill about halfway and stop there.
Where exactly does the browser come into this?
The app is a Web app and uses the browser to run the app.
Ah. I know nothing about Excel in a web app, I'm afraid.
Rory
Thats bad. Any suggestions on what I can do for help now?

Post a new question? Since it's not part of your original question anyway...  :) You'll want to aim more at .Net and Web related zones than Excel though for that.
Regards,
Rory