Adjust the width of Excel cell

Post Reply
sajith
Posts: 110
Joined: Wed Feb 18, 2009 9:58 am
Location: India
Contact:

Adjust the width of Excel cell

Post by sajith »

I am trying to export the data of a recordset to excel sheet .I want to adjust the cell width of a particular cell,How can i do it.Following is my code.

Code: Select all

//------------------------------------------------------------------------------
FUNCTION ExportToExcel(oRecSet)
//------------------------------------------------------------------------------
   LOCAL  oExcel ,oBook,oSheet
   LOCAL  j, i ,cFormat

    TRY
       oExcel := CreateObject( "Excel.Application"  )
       oBook  := oExcel:Workbooks:Add()
       oSheet := oBook:Worksheets( 1 )
    CATCH
       MsgBox( "Excel not installed" )
       Return
    END
    oRecSet:movefirst()
    CreateExcelHeader(@oExcel,oRecSet)
   FOR J:=1 TO oRecSet:RecordCount


      FOR K:=1 TO oRecSet:Fields:Count-1

         DO CASE
         CASE ValType(oRecSet:Fields( K ):Value) == 'N'
           oExcel:Cells(J+1, k):Value:=oRecSet:Fields( K ):Value
           cFormat     := If( .F., If( .T., "#,##0", "#.##0" ), "0" )
           oSheet:Columns( k):NumberFormat :=cFormat
        CASE ValType(oRecSet:Fields( K ):Value) == 'D'
          oExcel:Cells(J+1, k):Value:=AllTrim(MySqlDateFormat(oRecSet:Fields( K ):Value))
          oSheet:Columns( k):NumberFormat := Lower( Set( _SET_DATEFORMAT ) )
        OTHERWISE
          oExcel:Cells(J+1, k):Value:=oRecSet:Fields( K ):Value
       ENDCASE

     NEXT
    oRecSet:moveNext()
  NEXT
    oSheet:Range( oSheet:Cells( 1, 1 ), oSheet:Cells(1,oRecSet:Fields:Count) ):Font:Bold = .T.
    oSheet:Range(oSheet:Cells( 1, 1 ), oSheet:Cells(1,oRecSet:Fields:Count) ):Select()
    oExcel:Selection:Borders(9):LineStyle := 1
    oExcel:Selection:Borders(9):Weight    := 4
    oExcel:Visible = .T.

    oBook  := Nil
    oSheet := Nil
    oExcel := Nil
RETURN Nil
//------------------------------------------------------------------------------
FUNCTION CreateExcelHeader(oExcel,oRecSet)
//------------------------------------------------------------------------------

   LOCAL Kcnt
   FOR Kcnt:=1 TO oRecSet:Fields:Count-1
         oExcel:Cells(1, Kcnt):Value:=oRecSet:Fields( Kcnt ):Name
   NEXT
RETURN Nil


 
User avatar
anserkk
Posts: 1280
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Adjust the width of Excel cell

Post by anserkk »

Dear Mr.Sajith,

Code: Select all

oExcel   := CreateObject( "Excel.Application" )
oBook   := oExcel:WorkBooks:Add()
oSheet   := oExcel:ActiveSheet
oSheet:Columns( 1 ):ColumnWidth:=20
Regards
Anser
sajith
Posts: 110
Joined: Wed Feb 18, 2009 9:58 am
Location: India
Contact:

Re: Adjust the width of Excel cell

Post by sajith »

Dear Anser,

Thanks for ur Replay.Its Works Fine

regards sajith
Post Reply