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(intCo lumn).Colu mnName.ToS tring
Next
How Do I add the values to the column headers?
For intColumn = 0 To ds.Tables(0).Columns.Count
.Cells(1, intColumn + 1).Value = ds.Tables(0).Columns(intCo
Next
How Do I add the values to the column headers?
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
Regards,
Rory
ASKER
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( intColumnV alue).ToSt ring
Next
Next
For intRow = 0 To ds.Tables(0).Rows.Count - 1
For intColumnValue = 0 To ds.Tables(0).Columns.Count
.Cells(1, intColumnValue + 1).Value = ds.Tables(0).Rows(intRow).
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
ASKER
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"?
ASKER
I want to execute the code described above and then have the spreadsheet displayed.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(Applica tion("myCo nnString") ))
Dim strsql = Session.Item("strQRY")
Dim da As New SqlClient.SqlDataAdapter
da.SelectCommand = New SqlClient.SqlCommand(strsq l, sqlConnection)
da.Fill(ds)
Dim intColumn, intRow, intColumnValue As Integer
Dim dt As DataTable = ds.Tables("Qry")
strControlNumber = ds.Tables(0).Columns(conCo ntrolNumbe r).ToStrin g()
Dim myExcel As Object
myExcel = CreateObject("Excel.Applic ation")
With myExcel
.SheetsInNewWorkbook = 1
.Workbooks.Add().Worksheet s(1).Selec t()
'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(intCo lumn).Colu mnName.ToS tring
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( intColumnV alue).ToSt ring
' .Cells(intRow + 1, intColumnValue + 1).Value = ds.Tables(0).Rows(intRow). ItemArray( intColumnV alue).ToSt ring
Next
Next
.ActiveWorkbook.Close()
End With
myExcel.visible = True
sqlConnection.Close()
Catch ex As Exception
' Display error message
End Try
Try
Const conControlNumber As Int32 = 4 'SQL column number column
Dim sqlConnection As New SqlConnection(CStr(Applica
Dim strsql = Session.Item("strQRY")
Dim da As New SqlClient.SqlDataAdapter
da.SelectCommand = New SqlClient.SqlCommand(strsq
da.Fill(ds)
Dim intColumn, intRow, intColumnValue As Integer
Dim dt As DataTable = ds.Tables("Qry")
strControlNumber = ds.Tables(0).Columns(conCo
Dim myExcel As Object
myExcel = CreateObject("Excel.Applic
With myExcel
.SheetsInNewWorkbook = 1
.Workbooks.Add().Worksheet
'TO Display the column name in the the excel file.
For intColumn = 0 To ds.Tables(0).Columns.Count
.Cells(1, intColumn + 1).Value = ds.Tables(0).Columns(intCo
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
.Cells(intRow + 2, intColumnValue + 1).Value = ds.Tables(0).Rows(intRow).
' .Cells(intRow + 1, intColumnValue + 1).Value = ds.Tables(0).Rows(intRow).
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.
.ActiveWorkbook.Close()
as it's closing the workbook you just created, but other than that it looks OK to me.
ASKER
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.Applic ation")
the line I asked you to remove simply closes the workbook you just created so I don't see the point of it.
This is the line that starts Excel:
myExcel = CreateObject("Excel.Applic
the line I asked you to remove simply closes the workbook you just created so I don't see the point of it.
ASKER
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?
ASKER
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
Rory
ASKER
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
Regards,
Rory
ASKER
With myExcel
.SheetsInNewWorkbook = 1
.Workbooks.Add().Worksheet
'TO Display the column name in the the excel file.
For intColumn = 0 To ds.Tables(0).Columns.Count
.Cells(1, intColumn + 1).Value = ds.Tables(0).Columns(intCo
Next