Millkind
asked on
Formating an Excel Document
This is going to be a multiple part question. It all falls under the same area.
Formating an excel document.
I can do bold = .Rows(1).Font.Bold = True
I can underline = .Rows(1).Font.Underline = True
I can format as text = .Columns(6).NumberFormat = "@"
I can't format a column as currency
I can't center justify the entire sheet
I can't get the columns to expand to encompass the size of the largest cell in them
Formating an excel document.
I can do bold = .Rows(1).Font.Bold = True
I can underline = .Rows(1).Font.Underline = True
I can format as text = .Columns(6).NumberFormat = "@"
I can't format a column as currency
I can't center justify the entire sheet
I can't get the columns to expand to encompass the size of the largest cell in them
1) Center justify cells:
' Select all cells, returning the selection as a range
Public Function SelectAll() As Excel.Range
If _worksheet Is Nothing Then
Throw New ArgumentNullException("Wor ksheet not open")
End If
Return CType(_worksheet.Cells.Sel ect(), Excel.Range)
End Function
Setting alignment:
Public Sub CellAlignment(ByVal range As Excel.Range, ByVal horizontal As Excel.Constants, ByVal vertical As Excel.Constants)
range.HorizontalAlignment = horizontal
range.VerticalAlignment = vertical
End Sub
2) NumberFormat = "Currency"
Bob
' Select all cells, returning the selection as a range
Public Function SelectAll() As Excel.Range
If _worksheet Is Nothing Then
Throw New ArgumentNullException("Wor
End If
Return CType(_worksheet.Cells.Sel
End Function
Setting alignment:
Public Sub CellAlignment(ByVal range As Excel.Range, ByVal horizontal As Excel.Constants, ByVal vertical As Excel.Constants)
range.HorizontalAlignment = horizontal
range.VerticalAlignment = vertical
End Sub
2) NumberFormat = "Currency"
Bob
ASKER
for the line = .Columns(3).NumberFormat = "Currency"
Error:
An unhandled exception of type 'System.Runtime.InteropSer
Additional information: Unable to set the NumberFormat property of the Range class
for the lines =
If .Sheets.Item("Usage") Is Nothing Then
Throw New ArgumentNullException("Wor
End If
Return CType(.Sheets.Item("Usage"
Error:
An unhandled exception of type 'System.InvalidCastExcepti
Additional information: Specified cast is not valid.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or for entire sheet:
eApp.Cells.HorizontalAlign ment = Excel.XlHAlign.xlHAlignCen ter
eApp.Columns.AutoFit()
eApp.Cells.HorizontalAlign
eApp.Columns.AutoFit()
ASKER
.Range("A1", "Z100").HorizontalAlignmen t = Excel.XlHAlign.xlHAlignCen ter
errors out saying that xlHALign is not found
the formatting worked great
errors out saying that xlHALign is not found
the formatting worked great
Try this:
.HorizontalAlignment = xlCenter
.HorizontalAlignment = xlCenter
ASKER
same thing
What is the versions of Office, VS?
ASKER
2003 im using the Microsoft Excel 11.0 Object Library
I have VS2005. Not sure about 2003.
.Range("A1", "Z100").HorizontalAlignmen t = Excel.XlHAlign.xlHAlignCen ter - works fine for me.
Could you post a whole Sub for formatting?
.Range("A1", "Z100").HorizontalAlignmen
Could you post a whole Sub for formatting?
ASKER
Private Function fncExcelExport(ByVal ds As DataSet, ByVal ds2 As DataSet)
Dim Excel As New Microsoft.Office.Interop.E xcel.Appli cation
Dim intColumn, intRow, intColumnValue As Integer
With Excel
.SheetsInNewWorkbook = 2
.Workbooks.Add()
.Sheets.Item("Sheet1").Nam e = "Usage"
.Sheets.Item("Sheet2").Nam e = "Patrons"
.Worksheets(1).Select()
.Columns(5).NumberFormat = "@"
.Columns(3).NumberFormat = "$#,##0.00"
.Cells(1, 1).Value = "Date"
.Cells(1, 2).Value = "Location"
.Cells(1, 3).Value = "Amount"
.Cells(1, 4).Value = "Name"
.Cells(1, 5).Value = "Account Number"
'For displaying the column value row-by-row in the the excel file.
For intRow = 0 To ds2.Tables(0).Rows.Count - 1
For intColumnValue = 0 To ds2.Tables(0).Columns.Coun t - 1
If ds2.Tables(0).Rows(intRow) .ItemArray (intColumn Value).ToS tring.Star tsWith("Fr ont") Then
.Cells(intRow + 2, intColumnValue + 1).Value = "Dining Hall"
ElseIf ds2.Tables(0).Rows(intRow) .ItemArray (intColumn Value).ToS tring.Star tsWith("BH ") Then
.Cells(intRow + 2, intColumnValue + 1).Value = "Bullet Hole"
ElseIf ds2.Tables(0).Rows(intRow) .ItemArray (intColumn Value).ToS tring.Star tsWith("IK ES") Then
.Cells(intRow + 2, intColumnValue + 1).Value = "Ike's Sub Shop"
Else
.Cells(intRow + 2, intColumnValue + 1).Value = ds2.Tables(0).Rows(intRow) .ItemArray (intColumn Value).ToS tring
End If
Next
Next
.Rows(1).Font.Bold = True
.Rows(1).Font.Underline = True
.Range("A:F").EntireColumn .AutoFit()
'.Range("A1", "Z100").HorizontalAlignmen t = Excel.XlHAlign.xlHAlignCen ter
.Worksheets(2).Select()
.Columns(3).NumberFormat = "@"
.Columns(4).NumberFormat = "$#,##0.00"
.Cells(1, 1).Value = "ID Number"
.Cells(1, 2).Value = "Name"
.Cells(1, 3).Value = "Account Number"
.Cells(1, 4).Value = "Max Limit Remaining"
'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
Next
Next
.Rows(1).Font.Bold = True
.Rows(1).Font.Underline = True
.Range("A:F").EntireColumn .AutoFit()
.ActiveWorkbook().SaveAs(" C:\stuff\" & co & "-" & DatePart(DateInterval.Mont h, DateAdd(DateInterval.Month , -1, Date.Today)) & "-" & DatePart(DateInterval.Year , DateAdd(DateInterval.Month , -1, Date.Today)) & ".xls")
.ActiveWorkbook.Close()
End With
NormalExit:
Excel.Quit()
Excel = Nothing
GC.Collect()
Exit Function
End Function
Dim Excel As New Microsoft.Office.Interop.E
Dim intColumn, intRow, intColumnValue As Integer
With Excel
.SheetsInNewWorkbook = 2
.Workbooks.Add()
.Sheets.Item("Sheet1").Nam
.Sheets.Item("Sheet2").Nam
.Worksheets(1).Select()
.Columns(5).NumberFormat = "@"
.Columns(3).NumberFormat = "$#,##0.00"
.Cells(1, 1).Value = "Date"
.Cells(1, 2).Value = "Location"
.Cells(1, 3).Value = "Amount"
.Cells(1, 4).Value = "Name"
.Cells(1, 5).Value = "Account Number"
'For displaying the column value row-by-row in the the excel file.
For intRow = 0 To ds2.Tables(0).Rows.Count - 1
For intColumnValue = 0 To ds2.Tables(0).Columns.Coun
If ds2.Tables(0).Rows(intRow)
.Cells(intRow + 2, intColumnValue + 1).Value = "Dining Hall"
ElseIf ds2.Tables(0).Rows(intRow)
.Cells(intRow + 2, intColumnValue + 1).Value = "Bullet Hole"
ElseIf ds2.Tables(0).Rows(intRow)
.Cells(intRow + 2, intColumnValue + 1).Value = "Ike's Sub Shop"
Else
.Cells(intRow + 2, intColumnValue + 1).Value = ds2.Tables(0).Rows(intRow)
End If
Next
Next
.Rows(1).Font.Bold = True
.Rows(1).Font.Underline = True
.Range("A:F").EntireColumn
'.Range("A1", "Z100").HorizontalAlignmen
.Worksheets(2).Select()
.Columns(3).NumberFormat = "@"
.Columns(4).NumberFormat = "$#,##0.00"
.Cells(1, 1).Value = "ID Number"
.Cells(1, 2).Value = "Name"
.Cells(1, 3).Value = "Account Number"
.Cells(1, 4).Value = "Max Limit Remaining"
'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).
Next
Next
.Rows(1).Font.Bold = True
.Rows(1).Font.Underline = True
.Range("A:F").EntireColumn
.ActiveWorkbook().SaveAs("
.ActiveWorkbook.Close()
End With
NormalExit:
Excel.Quit()
Excel = Nothing
GC.Collect()
Exit Function
End Function
ASKER
.Range("A1", "Z100").HorizontalAlignmen t = Microsoft.Office.Interop.E xcel.XlHAl ign.xlHAli gnCenter
WORKED!!
WORKED!!
Yep! I guess, you didn't import "Microsoft.Office.Interop" first. Right?
ASKER
nope i just excel, figured why bring all of office if im just using excel. Live and Learn
Thanks for your help sorry
Thanks for your help sorry
What I would suggest:
Put "Imports Microsoft.Office.Interop" before your "Class";
Rename Excel to something (e. g. "ExApp") because of the names conflict:
Dim ExApp As New Excel.Application
Dim intColumn, intRow, intColumnValue As Integer
With ExApp
.SheetsInNewWorkbook = 2
.Workbooks.Add()
.Sheets.Item("Sheet1").Nam e = "Usage"
.Sheets.Item("Sheet2").Nam e = "Patrons"
.Worksheets(1).Select()
.Columns(5).NumberFormat = "@"
.Columns(3).NumberFormat = "$#,##0.00"
.Cells(1, 1).Value = "Date"
.Cells(1, 2).Value = "Location"
.Cells(1, 3).Value = "Amount"
.Cells(1, 4).Value = "Name"
.Cells(1, 5).Value = "Account Number"
'For displaying the column value row-by-row in the the excel file.
For intRow = 0 To ds2.Tables(0).Rows.Count - 1
For intColumnValue = 0 To ds2.Tables(0).Columns.Coun t - 1
If ds2.Tables(0).Rows(intRow) .ItemArray (intColumn Value).ToS tring.Star tsWith("Fr ont") Then
.Cells(intRow + 2, intColumnValue + 1).Value = "Dining Hall"
ElseIf ds2.Tables(0).Rows(intRow) .ItemArray (intColumn Value).ToS tring.Star tsWith("BH ") Then
.Cells(intRow + 2, intColumnValue + 1).Value = "Bullet Hole"
ElseIf ds2.Tables(0).Rows(intRow) .ItemArray (intColumn Value).ToS tring.Star tsWith("IK ES") Then
.Cells(intRow + 2, intColumnValue + 1).Value = "Ike's Sub Shop"
Else
.Cells(intRow + 2, intColumnValue + 1).Value = ds2.Tables(0).Rows(intRow) .ItemArray (intColumn Value).ToS tring
End If
Next
Next
.Rows(1).Font.Bold = True
.Rows(1).Font.Underline = True
.Range("A:F").EntireColumn .AutoFit()
.Range("A1", "Z100").HorizontalAlignmen t = Excel.XlHAlign.xlHAlignCen ter
.Worksheets(2).Select()
.Columns(3).NumberFormat = "@"
.Columns(4).NumberFormat = "$#,##0.00"
.Cells(1, 1).Value = "ID Number"
.Cells(1, 2).Value = "Name"
.Cells(1, 3).Value = "Account Number"
.Cells(1, 4).Value = "Max Limit Remaining"
'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
Next
Next
.Rows(1).Font.Bold = True
.Rows(1).Font.Underline = True
.Range("A:F").EntireColumn .AutoFit()
.ActiveWorkbook().SaveAs(" C:\stuff\" & co & "-" & DatePart(DateInterval.Mont h, DateAdd(DateInterval.Month , -1, Date.Today)) & "-" & DatePart(DateInterval.Year , DateAdd(DateInterval.Month , -1, Date.Today)) & ".xls")
.ActiveWorkbook.Close()
End With
NormalExit:
ExApp.Quit()
ExApp = Nothing
GC.Collect()
Exit Function
End Function
And that's it.
And another thing:
I would suggest you to use Try ... Catch ... Finally block. If something in the middle goes wrong, you can catch the error and you can place into Finally block code like this:
Dim p() As Process = Process.GetProcessesByName ("EXCEL")
Dim x As Integer
For x = 0 To p.Length - 1
p(x).Kill()
Next
It kills excel process for sure.
Good luck. Thanks for the points.
Put "Imports Microsoft.Office.Interop" before your "Class";
Rename Excel to something (e. g. "ExApp") because of the names conflict:
Dim ExApp As New Excel.Application
Dim intColumn, intRow, intColumnValue As Integer
With ExApp
.SheetsInNewWorkbook = 2
.Workbooks.Add()
.Sheets.Item("Sheet1").Nam
.Sheets.Item("Sheet2").Nam
.Worksheets(1).Select()
.Columns(5).NumberFormat = "@"
.Columns(3).NumberFormat = "$#,##0.00"
.Cells(1, 1).Value = "Date"
.Cells(1, 2).Value = "Location"
.Cells(1, 3).Value = "Amount"
.Cells(1, 4).Value = "Name"
.Cells(1, 5).Value = "Account Number"
'For displaying the column value row-by-row in the the excel file.
For intRow = 0 To ds2.Tables(0).Rows.Count - 1
For intColumnValue = 0 To ds2.Tables(0).Columns.Coun
If ds2.Tables(0).Rows(intRow)
.Cells(intRow + 2, intColumnValue + 1).Value = "Dining Hall"
ElseIf ds2.Tables(0).Rows(intRow)
.Cells(intRow + 2, intColumnValue + 1).Value = "Bullet Hole"
ElseIf ds2.Tables(0).Rows(intRow)
.Cells(intRow + 2, intColumnValue + 1).Value = "Ike's Sub Shop"
Else
.Cells(intRow + 2, intColumnValue + 1).Value = ds2.Tables(0).Rows(intRow)
End If
Next
Next
.Rows(1).Font.Bold = True
.Rows(1).Font.Underline = True
.Range("A:F").EntireColumn
.Range("A1", "Z100").HorizontalAlignmen
.Worksheets(2).Select()
.Columns(3).NumberFormat = "@"
.Columns(4).NumberFormat = "$#,##0.00"
.Cells(1, 1).Value = "ID Number"
.Cells(1, 2).Value = "Name"
.Cells(1, 3).Value = "Account Number"
.Cells(1, 4).Value = "Max Limit Remaining"
'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).
Next
Next
.Rows(1).Font.Bold = True
.Rows(1).Font.Underline = True
.Range("A:F").EntireColumn
.ActiveWorkbook().SaveAs("
.ActiveWorkbook.Close()
End With
NormalExit:
ExApp.Quit()
ExApp = Nothing
GC.Collect()
Exit Function
End Function
And that's it.
And another thing:
I would suggest you to use Try ... Catch ... Finally block. If something in the middle goes wrong, you can catch the error and you can place into Finally block code like this:
Dim p() As Process = Process.GetProcessesByName
Dim x As Integer
For x = 0 To p.Length - 1
p(x).Kill()
Next
It kills excel process for sure.
Good luck. Thanks for the points.
ASKER
Man that is perfect i was wondering why i had excels running around in my task manager.
You Rock
You Rock
ASKER
Will make the columns expand to fit, don't need that answer anymore.