how to program excel formatting options

D

douglas

I have the following excel 2003 formatting to ask:
I am writing a Visual Basic.Net 2005 desktop application write out data
from a sql server 2005 database to a excel 2003 spreadsheet.

After you look at my questions you can see a portion of the code that I am
currently working with. if you can answer any of the questions below, I would
appreciate it.

I would like to know how to change the following code to do the following:

1. I would like to have the center data in most of the columns including
the column headers.
2. I would like to see all the data in all the cels including the column
headers.
3. I would like the last column of the table to 'wraparound' to the next
line and
display no more that 75 to 100 characters in a line. (The last column
is varchar(500) due to the messages that could be displayed.)
4. I would like the column headers to be BOLD and the font be Times new
Roman
and be able to control the font size.
5. I would like the detail lines to be Times New Roman in a regular font
size.
6. Some of the column headers are larger than the detail column. Thus I
need the
column headers to wrap. Some of the column headers are larger four words
long. Thus, I do not want the column headers to 'wrap' in the middle of
a word. I would like the column headers only to wrap between words.



Private Sub ShowReport_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim xlClApp As Excel.ApplicationClass
Dim excelFile As String = " "
Dim dt As DataTable
Dim da4 As SqlDataAdapter
cnn4 = New SqlConnection(connectionString)
cmd4 = New SqlCommand("stored procedure name", cnn4)
cmd4.CommandType = CommandType.StoredProcedure
With cmd4
.Parameters.Add("@parm1", SqlDbType.VarChar).Value = strParm1
.Parameters.Add("@parm2", SqlDbType.VarChar).Value = strParm2
End With
da4 = New SqlDataAdapter(cmd4)
da4.Fill(ds4)
xlApp = New Excel.Application
xlClApp = New Excel.ApplicationClass
xlApp.Workbooks.Add()
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = CType(xlWorkBook.Sheets("sheet1"),
Microsoft.Office.Interop.Excel.Worksheet)
xlApp.Visible = False
xlApp.ScreenUpdating = True
dt = ds4.Tables(0)
'Add the column headings for the from the dataset
Dim dc As DataColumn
Dim iCols As Int32 = 0
For Each dc In dt.Columns
xlWorkSheet.Range("A1").Offset(0, iCols).Value = dc.ColumnName
xlWorkSheet.Range("A1").Offset(0, iCols).Font.Bold = True
xlWorkSheet.Range("A1").Offset(0, iCols).BorderAround()
iCols += 1
Next

Dim iRows As Int32
For iRows = 0 To dt.Rows.Count - 1
xlWorkSheet.Range("A2").Offset(0, iCols).Select()
xlWorkSheet.Range("A2").Offset(0, iCols).Justify()
xlWorkSheet.Range("A2").Offset(0, iCols).WrapText = True
xlWorkSheet.Range("A2").Offset(iRows).Resize(1, iCols).Value = _
dt.Rows(iRows).ItemArray()
xlWorkSheet.Range("A2").Offset(0, iCols).BorderAround()
Next
excelFile = "c:\exceltst.xls"
xlWorkBook.Sheets("sheet1").SaveAs(excelFile)
xlWorkBook.Close()
xlApp.Quit()
xlWorkSheet = Nothing
xlWorkBook = Nothing
xlClApp = Nothing
da4.Dispose()
cmd4.Dispose()
cnn4.Close()
 
J

Jacob Skaria

You can record a macro and get the code. Please review this link which will
help you http://office.microsoft.com/en-us/excel/HA010548371033.aspx

'Select All and format font
Cells.Select
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 8

'Select each range
Range("A2").Select
Selection.HorizontalAlignment = xlCenter
Selection.WrapText = True
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 11
Selection.Font.Bold = True

PS: I will respond to your other post soon..

If this post helps click Yes
 
D

douglas

"Jacob Skaria":

I want to thank you for all your help so far! However I have the
following additional questions to ask you:
1. If you look at the code that is listed below, I have put some headings
on my excelspreadsheet output. The headers are in rows 1 to 8, the column
header is on
is in row 9 and the detail data is in row 10. (Also what used to be row 1
is now row 9 and what was row 2 is now row 10.
2. I did what you replied back to me and everything worked except for the
xlcenter.
The vb.net 2005 deskop application did not know what this was. Thus can you
recommend another "center" for align in the cell?
3. What can I do so that all the words headers and detail data show? i
know it must have something to do with 'resize' that I listed below. What
would you change?
4. The very last column is varchar(500). Is there some way I can do a
'wrap text alignment on that cell?
***5. How would you change the formatting for the following:
On rows 5, 6, and 7 where I hardcoded 'customer name', 'customer number' ,
and 'process type', those are currently the first three columns in the detail
data right now. I want the data that is in column 1 to appear after the
'customer name' column. i want the data that is in column 2 to really appear
after the 'customer number'. Also I want the data that is in column 3 to
appear after the label called 'process type'.
Once this occurs, I will remove the data that is appearing in columns 1
through 3 right now. Can you tell me how to change this formatting?
6. You also mentioned that I can use a macro to get the code. However
since this is in a vb.net application, would i be able to 'record' a macro?

***Note my mofied code is listed below

'The following chartrange areas setup the heading format
chartRange = xlWorkSheet.Range("f1", "k1")
chartRange.Merge()
chartRange.FormulaR1C1 = "TITLE LINE 1"
chartRange.HorizontalAlignment = 3
chartRange.VerticalAlignment = 3
chartRange.Font.Bold = True
chartRange = xlWorkSheet.Range("f2", "k2")
chartRange.Merge()
chartRange.FormulaR1C1 = "TITLE LINE 2"
chartRange.HorizontalAlignment = 3
chartRange.VerticalAlignment = 3
chartRange.Font.Bold = True
chartRange = xlWorkSheet.Range("F3", "K3")
chartRange.Merge()
chartRange.FormulaR1C1 = "TITLE LINE 3"
chartRange.HorizontalAlignment = 3
chartRange.VerticalAlignment = 3
chartRange.Font.Bold = True
chartRange = xlWorkSheet.Range("A5", "B5")
chartRange.Merge()
chartRange.FormulaR1C1 = "CUSTOMER NAME:"
chartRange.HorizontalAlignment = 1
chartRange.VerticalAlignment = 1
chartRange.Font.Bold = True
chartRange = xlWorkSheet.Range("A6", "B6")
chartRange.Merge()
chartRange.FormulaR1C1 = "CUSTOMER NUMBER:"
chartRange.HorizontalAlignment = 1
chartRange.VerticalAlignment = 1
chartRange.Font.Bold = True
chartRange = xlWorkSheet.Range("A7", "B7")
chartRange.Merge()
chartRange.FormulaR1C1 = "PROCESS TYPE:"
chartRange.HorizontalAlignment = 1
chartRange.VerticalAlignment = 1
chartRange.Font.Bold = True

For Each dc In dt.Columns

xlApp.Range("A9").Offset(0, iCols).Value =
dc.ColumnName


'Select All and format font
xlApp.Cells.Select()
xlApp.Selection.Font.Name = "Times New Roman"
xlApp.Selection.Font.Size = 12
iCols += 1
Next
'Add the data
Dim iRows As Int32
For iRows = 0 To dt.Rows.Count - 1
lWorkSheet.Range("A10").Offset(iRows).Resize(1,
iCols).Value = _
dt.Rows(iRows).ItemArray()
'Select each range
xlApp.Range("A10").Select()

'xlApp.Cells.HorizontalAlignment = xlCenter
xlApp.Selection.WrapText = True
xlApp.Selection.Font.Name = "Times New Roman"
xlApp.Selection.Font.Size = 9
xlApp.Selection.Font.Bold = True
 
D

douglas

Jacob Skaria":

I also wanted to mention that when I closed the vb.net 2005 desktop
application, I had lots of 'excel.exe' processes running when I looked at the
task manager in my workstation. Thus I must need to set the excel objects
that the vb.net 2005 application is running to 'nothing' or dispose of those
execution. Thus would you have an idea of what other excel objects that I
would need to close are?

Thanks!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top