Solved

Formating an Excel Document

Posted on 2006-06-19
17
1,852 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:Millkind
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
17 Comments
 

Author Comment

by:Millkind
ID: 16935707
oSheet.Range("C:C").EntireColumn.AutoFit

Will make the columns expand to fit, don't need that answer anymore.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16935756
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("Worksheet not open")
    End If

    Return CType(_worksheet.Cells.Select(), 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
0
 

Author Comment

by:Millkind
ID: 16936134

for the line =             .Columns(3).NumberFormat = "Currency"

Error:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in microsoft.visualbasic.dll

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("Worksheet not open")
            End If
            Return CType(.Sheets.Item("Usage").Cells.Select(), Microsoft.Office.Interop.Excel.Range)

Error:
An unhandled exception of type 'System.InvalidCastException' occurred in Charge Slip Monthly Sender.exe

Additional information: Specified cast is not valid.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 10

Accepted Solution

by:
vadim63 earned 500 total points
ID: 16939491
           .Columns(1).NumberFormat = "$#,##0.00"
            .Range("A1", "Z100").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            .Range("A1", "Z100").Columns.AutoFit()
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16939542
or for entire sheet:

            eApp.Cells.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            eApp.Columns.AutoFit()
0
 

Author Comment

by:Millkind
ID: 16941220
           .Range("A1", "Z100").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter

errors out saying that xlHALign is not found

the formatting worked great
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16941758
Try this:

.HorizontalAlignment = xlCenter
0
 

Author Comment

by:Millkind
ID: 16941795
same thing
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16942282
What is the versions of Office, VS?
0
 

Author Comment

by:Millkind
ID: 16942975
2003 im using the Microsoft Excel 11.0 Object Library
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16943665
I have VS2005. Not sure about 2003.
.Range("A1", "Z100").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter    -   works fine for me.

Could you post a whole Sub for formatting?
0
 

Author Comment

by:Millkind
ID: 16943990
   Private Function fncExcelExport(ByVal ds As DataSet, ByVal ds2 As DataSet)

        Dim Excel As New Microsoft.Office.Interop.Excel.Application
        Dim intColumn, intRow, intColumnValue As Integer
        With Excel
            .SheetsInNewWorkbook = 2
            .Workbooks.Add()
            .Sheets.Item("Sheet1").Name = "Usage"
            .Sheets.Item("Sheet2").Name = "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.Count - 1
                    If ds2.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString.StartsWith("Front") Then
                        .Cells(intRow + 2, intColumnValue + 1).Value = "Dining Hall"
                    ElseIf ds2.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString.StartsWith("BH") Then
                        .Cells(intRow + 2, intColumnValue + 1).Value = "Bullet Hole"
                    ElseIf ds2.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString.StartsWith("IKES") Then
                        .Cells(intRow + 2, intColumnValue + 1).Value = "Ike's Sub Shop"
                    Else
                        .Cells(intRow + 2, intColumnValue + 1).Value = ds2.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString
                    End If
                Next
            Next
            .Rows(1).Font.Bold = True
            .Rows(1).Font.Underline = True
            .Range("A:F").EntireColumn.AutoFit()
            '.Range("A1", "Z100").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            .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(intColumnValue).ToString
                Next
            Next
            .Rows(1).Font.Bold = True
            .Rows(1).Font.Underline = True
            .Range("A:F").EntireColumn.AutoFit()
            .ActiveWorkbook().SaveAs("C:\stuff\" & co & "-" & DatePart(DateInterval.Month, 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
0
 

Author Comment

by:Millkind
ID: 16944074
.Range("A1", "Z100").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter

WORKED!!
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16944128
Yep! I guess, you didn't import "Microsoft.Office.Interop" first. Right?
0
 

Author Comment

by:Millkind
ID: 16944150
nope i just excel, figured why bring all of office if im just using excel.  Live and Learn
Thanks for your help sorry
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16944249
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").Name = "Usage"
            .Sheets.Item("Sheet2").Name = "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.Count - 1
                    If ds2.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString.StartsWith("Front") Then
                        .Cells(intRow + 2, intColumnValue + 1).Value = "Dining Hall"
                    ElseIf ds2.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString.StartsWith("BH") Then
                        .Cells(intRow + 2, intColumnValue + 1).Value = "Bullet Hole"
                    ElseIf ds2.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString.StartsWith("IKES") Then
                        .Cells(intRow + 2, intColumnValue + 1).Value = "Ike's Sub Shop"
                    Else
                        .Cells(intRow + 2, intColumnValue + 1).Value = ds2.Tables(0).Rows(intRow).ItemArray(intColumnValue).ToString
                    End If
                Next
            Next
            .Rows(1).Font.Bold = True
            .Rows(1).Font.Underline = True
            .Range("A:F").EntireColumn.AutoFit()
            .Range("A1", "Z100").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            .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(intColumnValue).ToString
                Next
            Next
            .Rows(1).Font.Bold = True
            .Rows(1).Font.Underline = True
            .Range("A:F").EntireColumn.AutoFit()
            .ActiveWorkbook().SaveAs("C:\stuff\" & co & "-" & DatePart(DateInterval.Month, 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.
0
 

Author Comment

by:Millkind
ID: 16945001
Man that is perfect i was wondering why i had excels running around in my task manager.
You Rock
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question