Sign in to follow this  
Followers 0
jerome

Excel file generation UDF (don't need Excel to be installed)

19 posts in this topic

Hi all,

I'm back with an Excel file generation UDF that can manage multi-sheet workbook but without the need to have Excel installed on the computer...

You can add more functions if you want but keep always my name in the post of greetings.

;----------------------------------------------------------------------------------------------
; Copyright Jerome DERN 2010
; ---------------------------------------------------------------------------------------------
#Include <File.au3>

; Const for cell format
Const $XMLXLS_STRING    = "String"
Const $XMLXLS_NUMBER    = "Number"
Const $XMLXLS_SDATE     = "Short Date"
Const $XMLXLS_PERCENT   = "Percent"
Const $XMLXLS_SCIENTIFIC= "Scientific"
Const $XMLXLS_DATE      = "[$-F400]h:mm:ss\ AM/PM"
Const $XMLXLS_FRACTION  = "#&quot; &quot;?/?"
Const $XMLXLS_COMPTAB   = "_-* #,##0.00\ &quot;€&quot;_-;\-* #,##0.00\ &quot;€&quot;_-;_-* &quot;-&quot;??\ &quot;€&quot;_-;_-@_-"
; Constants for cell alignment
Const $XMLXLS_CENTER    = "Center"
Const $XMLXLS_RIGHT     = "Right"
Const $XMLXLS_LEFT  = "Left"
Const $XMLXLS_BOTTOM    = "Bottom"
Const $XMLXLS_TOP   = "Top"
Const $XMLXLS_JUSTIFIED = "Justify"
; Font style
Global Enum Step *2 $XMLXLS_BOLD, $XMLXLS_ITALIC, $XMLXLS_STRIKETHROUGH, $XMLXLS_UNDERLINE, $XMLXLS_SUBSCRIPT, $XMLXLS_SUPERSCRIPT
; BitOr Values for border
Global Enum Step *2 $XMLXLS_BDRIGHT, $XMLXLS_BDLEFT, $XMLXLS_BDTOP, $XMLXLS_BDBOTTOM, $XMLXLS_BDCROSS1, $XMLXLS_BDCROSS2
; Page Set up
Const $XMLXLS_LANDSCAPE = "Landscape"
Const $XMLXLS_PORTRAIT = "Portrait"

$demo1 = True

$file = FileOpen("text.xls", 2)
; Create Workbook, active sheet will be se second one (2)
_XLSCreateWorkbook($file, 2)
; Define styles
_XLSCreateStyles($file)
; Define a new personal style
_XLSAddStyle($file, "Header", "Arial", 11, $XMLXLS_BOLD, "000000", "", $XMLXLS_CENTER, $XMLXLS_CENTER, BitOR($XMLXLS_BDRIGHT, $XMLXLS_BDLEFT, $XMLXLS_BDTOP, $XMLXLS_BDBOTTOM))
_XLSAddStyle($file, "Table", "Arial", 11, 0, "000000", "", $XMLXLS_LEFT, $XMLXLS_CENTER, BitOR($XMLXLS_BDRIGHT, $XMLXLS_BDLEFT, $XMLXLS_BDTOP, $XMLXLS_BDBOTTOM))
; Finish style definition
_XLSCloseStyles($file)

; In demo1 case the sheets are generated from an array in memory
; limitation: named area can't be defined
If $demo1 Then
    ; a typical excel sheet memory reservation (can be adjusted to real need)
    Dim $Sheet1[65536][255]
    Dim $Sheet2[65536][255]
    ; Create sheet 1
    _XLSInitSheet($Sheet1, "Page 1")
    _XLSSetRowHeight($Sheet1, 1, 40)
    _XLSSetColumnWidth($Sheet1, 1, 100)
    _XLSSetColumnWidth($Sheet1, 2, 110)
    _XLSSetColumnWidth($Sheet1, 3, 120)
    _XLSSetCell($Sheet1, 1, 1, "Qty", "", "", "Header")
    _XLSSetCell($Sheet1, 1, 2, "Value", "", "", "Header")
    _XLSSetCell($Sheet1, 1, 3, "Total", "", "", "Header")
    _XLSSetCell($Sheet1, 2, 1, 2, "", $XMLXLS_NUMBER, "Table", "qty")
    _XLSSetCell($Sheet1, 2, 2, 4, "", $XMLXLS_NUMBER, "Table", "value")
    ;_XLSSetCell($Sheet1, 2, 3, 0, "RC[-2]*RC[-1]", $XMLXLS_NUMBER, "Table")
    _XLSSetCell($Sheet1, 2, 3, 0, "qty*value", $XMLXLS_NUMBER, "Table")
    _XLSGenerateFromArray($file, $Sheet1, $XMLXLS_LANDSCAPE)
    ; Create sheet 2
    _XLSInitSheet($Sheet2, "Page 2")
    _XLSSetCell($Sheet2, 1, 1, "Name", "", "", "Header")
    _XLSSetCell($Sheet2, 1, 2, "Value", "", "", "Header")
    _XLSSetCell($Sheet2, 2, 1, "Beer", "", $XMLXLS_STRING, "Table")
    _XLSSetCell($Sheet2, 2, 2, 4.0, "", $XMLXLS_NUMBER, "Table")
    _XLSGenerateFromArray($file, $Sheet2)
Else ; in this demo, sheets are generated from direct commands to library
    ; Create a new worksheet
    _XLSAddWorkSheet($file, "Sheet1")
    ; Define specific names of this sheet (optional)
    _XLSCreateWSNames($file)
    ; Print area is a specific name for printing zone
    _XLSAddWSName($file, "Print_Area", "Sheet1!R1C1:R65535C255")
    ; Finish definition of names
    _XLSCloseWSNames($file)
    ; Start Worksheet content
    _XLSStartWSDefinition($file)
    ; Create a row
    _XLSAddRowInWorkSheet($file)
    ; Add data to first row, column 1
    _XLSAddCellInRow($file, "C1", "", $XMLXLS_STRING, "Table")
    ; Add data to first row, column 2
    _XLSAddCellInRow($file, "C2", "", $XMLXLS_STRING)
    ; Finish row definition
    _XLSCloseRow($file)
    ; Create a new row (2nd one)
    _XLSAddRowInWorkSheet($file)
    ; Add data to second row, column 1
    _XLSAddCellInRow($file, "1", "", $XMLXLS_NUMBER)
    ; Add data to second row, column 2
    _XLSAddCellInRow($file, "2", "RC[-1]*2", $XMLXLS_NUMBER)
    ; Finish row definition
    _XLSCloseRow($file)
    ; Close Worksheet content
    _XLSCloseWorkSheet($file, $XMLXLS_PORTRAIT, 1, 1, 1, 1, 0.5, 0.5)
    ; Add a second Worksheet
    _XLSAddWorkSheet($file, "Sheet2")
    _XLSStartWSDefinition($file)
    ; Create first row definition
    _XLSAddRowInWorkSheet($file)
    ; Add data to first row, column 1
    _XLSAddCellInRow($file, "CC1", "", $XMLXLS_STRING)
    ; Add data to first row, column 2
    _XLSAddCellInRow($file, "CC2", "", $XMLXLS_STRING)
    ; Finish row definition
    _XLSCloseRow($file)
    ; Close Worksheet content
    _XLSCloseWorkSheet($file)
EndIf
; Close Worksbook content, file is ready to use
_XLSCloseWorkBook($file)
ShellExecute("text.xls")

; -------------------------------------------------------------------------------------------------------------------------------------
; ------------------------------------------------------- ARRAY TO XML PRIMITIVES -----------------------------------------------------
; -------------------------------------------------------------------------------------------------------------------------------------

; -----------------------------------------------------------------------------------
; Initialize sheet : Set sheet name and set active size to 1 cell
; -----------------------------------------------------------------------------------
Func _XLSInitSheet(ByRef $array, $name)
    ; $array[0][0] store the name of the sheet;the maximum row number used;the maximum column number used
    $array[0][0] = $name & ";1;1"
EndFunc

; -----------------------------------------------------------------------------------
; Set row height
; -----------------------------------------------------------------------------------
Func _XLSSetRowHeight(ByRef $array, $row, $height)
    ; array[x][0] store the height of each row
    $array[$row][0] = $height
EndFunc

Func _XLSSetColumnWidth(ByRef $array, $col, $width)
    ; array[0][x] store the width of each column
    $array[0][$col] = $width
EndFunc

; ------------------------------------------------------------------------------------
; This format a cell in case of an array usage + generation with _XLSGenerateFromArray function
; ------------------------------------------------------------------------------------
Func _XLSSetCell(ByRef $array, $row, $col, $data, $formulae, $type=$XMLXLS_STRING, $style="Default", $name="")
    Local $decode
    If $formulae<>"" Then $data=""
    if $type = "" Then $type = $XMLXLS_STRING
    If $style = "" Then $style = "Default"
    $array[$row][$col] = $data & @CR & $formulae & @CR & $type & @CR & $style & @CR & $name
    ; check if max of row and column are to be updated
    $decode = StringSplit($array[0][0], ";")
    If $decode[0]<3 Then Return
    If $row>$decode[2] Then $decode[2]=$row
    If $col>$decode[3] Then $decode[3]=$col
    $array[0][0]=$decode[1]&";"&$decode[2]&";"&$decode[3]
EndFunc

; ----------------------------------------------------------------------------------
; Generate a sheet from a 2D array. array[0][0] has the sheet name, other array[row][col]
; are strings formatted using _XLSBuildCell function
; ----------------------------------------------------------------------------------
Func _XLSGenerateFromArray($file, ByRef $array, $orientation=$XMLXLS_PORTRAIT, $mbottom = 0.98, $mleft=0.78, $mright=0.78, $mtop=0.98, $hmargin=0.5, $fmargin=0.5)
    Local $col, $row, $sname, $decode, $data, $formula, $type, $style, $name, $height
    ; get array real size
    $decode = StringSplit($array[0][0], ";")
    If $decode[0] < 3 Then Return
    $sname = $decode[1]
    $maxrow = $decode[2]
    $maxcol = $decode[3]
    ; Create a new worksheet
    _XLSAddWorkSheet($file, $sname)
    ; Define specific names of this sheet (optional)
    _XLSCreateWSNames($file)
    ; Print area is a specific name for printing zone
    _XLSAddWSName($file, "Print_Area", "'"&$sname&"'" & "!R1C1:R"&$maxrow&"C"&$maxcol)
    ; Small patch in order to Manage here named cells
    Local $temp = $file, $nbnames=0
    Dim $cellnames[1000][2]
    $file =_TempFile()
    ; Finish definition of names
    _XLSCloseWSNames($file)
    ; Start Worksheet content
    _XLSStartWSDefinition($file)
    ; Write column size if needed
    For $col=1 to $maxcol
        If $array[0][$col] <> "" Then _XLSColumnSize($file, $col, $array[0][$col])
    Next
    ; generate sheet content
    For $row=1 to $maxrow
        $height = -1
        If $array[$row][0] <> "" Then $height = $array[$row][0]
        ; Create a row
        _XLSAddRowInWorkSheet($file, $height)
        For $col=1 to $maxcol
            $data = ""
            $formula = ""
            $type = $XMLXLS_STRING
            $style = "Default"
            $name = ""
            If $array[$row][$col]<>"" Then
                $e = StringSplit($array[$row][$col], @CR)
                If $e[0]=5 Then
                    $data   = $e[1]
                    $formula = $e[2]
                    $type   = $e[3]
                    $style = $e[4]
                    $name   = $e[5] ;, named cell not yet implemented
                    If $name<>"" Then
                        $cellnames[$nbnames][0] = $name
                        $cellnames[$nbnames][1] = "'"&$sname&"'"&"!R"&$row&"C"&$col
                        $nbnames+=1
                    EndIf
                EndIf
            EndIf
            If $type="" Then $type = $XMLXLS_STRING
            If $style="" Then $style = "Default"
            ; Add data to first row, column 1
            _XLSAddCellInRow($file, $data, $formula, $type, $style)
        Next
        ; Finish row definition
        _XLSCloseRow($file)
    Next
    ; Add named cell found
    For $col=1 to $nbnames
        _XLSAddWSName($temp, $cellnames[$col-1][0], $cellnames[$col-1][1])
    Next
    ; merge files
    Dim $aRecords
    _FileReadToArray($file,$aRecords)
    FileDelete($file)
    $file = $temp
    For $col = 1 to $aRecords[0]
        FileWriteLine($file, $aRecords[$col])
    Next
    ; Close Worksheet content
    _XLSCloseWorkSheet($file, $orientation, $mbottom, $mleft, $mright, $mtop, $hmargin, $fmargin)
EndFunc

; -------------------------------------------------------------------------------------------------------------------------------------
; ------------------------------------------------------- XML BASE PRIMITIVES ---------------------------------------------------------
; -------------------------------------------------------------------------------------------------------------------------------------

; --------------------------------------------------------------------------------------------------
; Create (Define) a new Excel workbook
; --------------------------------------------------------------------------------------------------
Func _XLSCreateWorkbook($file, $activesheet=1)
     If IsString($file) Then FileDelete($file)
     $activesheet -= 1
     FileWriteLine($file, '<?xml version="1.0"?>')
     FileWriteLine($file, '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">')
     FileWriteLine($file, '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Author>XLSXMLGenerator</Author><Created>'&@YEAR&'-'&@MON&'-'&@MDAY&'T'&@HOUR&':'&@MIN&':'&@SEC&'</Created><Company>XLSXML</Company></DocumentProperties>')
     FileWriteLine($file, '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"><ActiveSheet>'&$activesheet&'</ActiveSheet></ExcelWorkbook>')
EndFunc

; --------------------------------------------------------------------------------------------------
; Create (Define) a Style section
; --------------------------------------------------------------------------------------------------
Func _XLSCreateStyles($file)
     FileWriteLine($file, ' <Styles>')
     FileWriteLine($file, ' <Style ss:ID="Default" ss:Name="Normal"><Alignment ss:Vertical="Bottom"/><Borders/><Font ss:Size="11"/></Style>')
EndFunc

; --------------------------------------------------------------------------------------------------
; Create (Define) a new style. Borders must be constructed with BitOR() of desired border attribute
; --------------------------------------------------------------------------------------------------
Func _XLSAddStyle($file, $name="S21", $font="Arial", $size=11, $fstyle=0, $pcolor="000000", $icolor="", $AlignHor=$XMLXLS_CENTER, $AlignVer=$XMLXLS_BOTTOM, $Border=0)
     Local $f, $fontfam="Swiss"
     FileWriteLine($file, ' <Style ss:ID="' & $name &'">')
     FileWriteLine($file, '     <Alignment ss:Horizontal="' & $AlignHor & '" ss:Vertical="' & $AlignVer &'"/>')
     ;$Border = BitOR($XMLXLS_BDRIGHT, $XMLXLS_BDLEFT, $XMLXLS_BDTOP, $XMLXLS_BDBOTTOM, $XMLXLS_BDCROSS1, $XMLXLS_BDCROSS2)
     If $Border>0 Then FileWriteLine($file, '   <Borders>')
     If BitAnd($Border, $XMLXLS_BDBOTTOM)>0 Then FileWriteLine($file, '     <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>')
     If BitAnd($Border, $XMLXLS_BDLEFT)>0 Then FileWriteLine($file, '   <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>')
     If BitAnd($Border, $XMLXLS_BDRIGHT)>0 Then FileWriteLine($file, '  <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>')
     If BitAnd($Border, $XMLXLS_BDTOP)>0    Then FileWriteLine($file, '     <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>')
     If BitAnd($Border, $XMLXLS_BDCROSS1)>0 Then FileWriteLine($file, '     <Border ss:Position="DiagonalLeft" ss:LineStyle="Continuous" ss:Weight="1"/>')
     If BitAnd($Border, $XMLXLS_BDCROSS2)>0 Then FileWriteLine($file, '     <Border ss:Position="DiagonalRight" ss:LineStyle="Continuous" ss:Weight="1"/>')
     If $Border>0 Then FileWriteLine($file, '   </Borders>')
     $f = '     <Font ss:FontName="' & $font & '" x:Family="' & $fontfam & '" ss:Size="' & $size & '" '
     ;$fstyle = BitOR($XMLXLS_BOLD, $XMLXLS_ITALIC, $XMLXLS_STRIKETHROUGH, $XMLXLS_UNDERLINE, $XMLXLS_SUBSCRIPT)
     If BitAnd($fstyle, $XMLXLS_BOLD)>0     Then $f &= 'ss:Bold="1" '
     If BitAnd($fstyle, $XMLXLS_ITALIC)>0   Then $f &= 'ss:Italic="1" '
     If BitAnd($fstyle, $XMLXLS_STRIKETHROUGH)>0 Then $f &= 'ss:StrikeThrough="1" '
     If BitAnd($fstyle, $XMLXLS_UNDERLINE)>0    Then $f &= 'ss:Underline="Single" '
     If BitAnd($fstyle, $XMLXLS_SUBSCRIPT)>0    Then $f &= 'ss:VerticalAlign="Subscript" '
     If BitAnd($fstyle, $XMLXLS_SUPERSCRIPT)>0 Then $f &= 'ss:VerticalAlign="Superscript" '
     If $pcolor <>"" Then $f &= 'ss:Color="#'&$pcolor&'" '
     $f &= '/>'
     If $icolor<>"" Then FileWriteLine($file, '<Interior ss:Color="#'&$icolor&'" ss:Pattern="Solid"/>')
     FileWriteLine($file, $f)
     FileWriteLine($file, ' </Style>')
EndFunc

; --------------------------------------------------------------------------------------------------
; Close styles definitions
; --------------------------------------------------------------------------------------------------
Func _XLSCloseStyles($file)
     FileWriteLine($file, ' </Styles>')
EndFunc

; --------------------------------------------------------------------------------------------------
; Create (Define) a new Excel worksheet
; --------------------------------------------------------------------------------------------------
Func _XLSAddWorkSheet($file, $sheet)
     FileWriteLine($file, ' <Worksheet ss:Name="' & $sheet & '">')
EndFunc

; --------------------------------------------------------------------------------------------------
; Create (Define) an range named section
; --------------------------------------------------------------------------------------------------
Func _XLSCreateWSNames($file)
     FileWriteLine($file, ' <Names>')
EndFunc

; --------------------------------------------------------------------------------------------------
; Create (Define) a new range name
; --------------------------------------------------------------------------------------------------
Func _XLSAddWSName($file, $name="Print_Area", $location="'Sheet1'!R1C1:R65535C255")
    FileWriteLine($file, '  <NamedRange ss:Name="' & $Name & '" ss:RefersTo="=' & $location & '"/>')
     ; Ex: <NamedRange ss:Name="qty" ss:RefersTo="='Page 1'!R2C1"/>
    ; Ex: <NamedRange ss:Name="value" ss:RefersTo="='Page 1'!R2C2"/>
EndFunc

; --------------------------------------------------------------------------------------------------
; Close Worksheet named range section
; --------------------------------------------------------------------------------------------------
Func _XLSCloseWSNames($file)
     FileWriteLine($file, ' </Names>')
EndFunc

; --------------------------------------------------------------------------------------------------
; Begin WorkSheet definition
; --------------------------------------------------------------------------------------------------
Func _XLSStartWSDefinition($file)
     FileWriteLine($file, ' <Table x:FullColumns="1" x:FullRows="1">')
EndFunc

; --------------------------------------------------------------------------------------------------
; Define column size
; --------------------------------------------------------------------------------------------------
Func _XLSColumnSize($file, $col, $size)
     FileWriteLine($file, '     <Column ss:Index="'&$col&'" ss:AutoFitWidth="0" ss:Width="'&$size&'"/>')
EndFunc

; --------------------------------------------------------------------------------------------------
; Add a new row in current worksheet
; --------------------------------------------------------------------------------------------------
Func _XLSAddRowInWorkSheet($file, $height=-1)
     If $height = -1 Then
        FileWriteLine($file, '  <Row ss:AutoFitHeight="1">')
    Else
        FileWriteLine($file, '  <Row ss:AutoFitHeight="0" ss:Height="'& $height &'">')
    EndIf
EndFunc

; --------------------------------------------------------------------------------------------------
; Add a cell (column) in the cirrent row of the current worksheet
; --------------------------------------------------------------------------------------------------
Func _XLSAddCellInRow($file, $data, $formula="", $type=$XMLXLS_STRING, $style="Default")
     Local $Cell ='     <Cell '
     If $formula <> "" Then $Cell &= 'ss:Formula="=' & $formula & '" '
     ; formulae example: RC[-1]*2 or R1C1*2
     ; formulae example: Sheet2!R[1]C*2
     FileWriteLine($file, $Cell & 'ss:StyleID="' & $style & '"><Data ss:Type="' & $type & '">' & $data & '</Data></Cell>')
EndFunc

; --------------------------------------------------------------------------------------------------
; Close current row definition
; --------------------------------------------------------------------------------------------------
Func _XLSCloseRow($file)
     FileWriteLine($file, '     </Row>')
EndFunc

; --------------------------------------------------------------------------------------------------
; Close current worksheet definition
; --------------------------------------------------------------------------------------------------
Func _XLSCloseWorkSheet($file, $orientation=$XMLXLS_PORTRAIT, $mbottom = 0.98, $mleft=0.78, $mright=0.78, $mtop=0.98, $hmargin=0.5, $fmargin=0.5)
     FileWriteLine($file, ' </Table>')
     FileWriteLine($file, '     <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">')
    FileWriteLine($file, '  <PageSetup>')
    FileWriteLine($file, '  <Layout x:Orientation="'&$orientation&'"/>')
    FileWriteLine($file, '  <Header x:Margin="'&$hmargin&'"/>')
    FileWriteLine($file, '  <Footer x:Margin="'&$fmargin&'"/>')
    FileWriteLine($file, '  <PageMargins x:Bottom="'&$mbottom&'" x:Left="'&$mleft&'" x:Right="'&$mright&'" x:Top="'&$mtop&'"/>')
    FileWriteLine($file, '  </PageSetup>')
     FileWriteLine($file, '     </WorksheetOptions>')
     FileWriteLine($file, '     <ss:ActiveSheet>Page 2</ss:ActiveSheet>')
     FileWriteLine($file, ' </Worksheet>')
EndFunc

; --------------------------------------------------------------------------------------------------
; Close Excel workbook, the file now is readable by excel
; --------------------------------------------------------------------------------------------------
Func _XLSCloseWorkBook($file)
     FileWriteLine($file, '</Workbook>')
     If Not IsString($file) Then FileClose($file)
EndFunc

; -------------------------------------------------------------------------------
; Create a relative (to $l,$c) formulae to access to $l2,$c2 in sheet $sheet
; -------------------------------------------------------------------------------
Func _XLSlc2rc($l1, $c1, $l2, $C2, $sheet)
    Local $dc, $dl, $f
    $dc = $c2-$c1
    $dl = $l2-$l1
    $f="R"
    if $sheet <> "" Then $f = $sheet & "!" & $f
    if $dl<>0 Then $f &= '[' & $dl & ']'
    $f &= "C"
    if $dc<>0 Then $f &= '[' & $dc & ']'
    Return $f
EndFunc

Share this post


Link to post
Share on other sites



It's look interesting Posted Image

Thanks to share !


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Jerome,

I registered so I could let you know that I'm really glad you posted this file.

Not needing to install Excel on a web server, but still be able to generate Excel files, has been a huge help.

Thanks!

Hi all,

I'm back with an Excel file generation UDF that can manage multi-sheet workbook but without the need to have Excel installed on the computer...

You can add more functions if you want but keep always my name in the post of greetings.

Share this post


Link to post
Share on other sites

Hi,

It's a long time I have posted this, I even forgot it because of the lack of interrest here...

Nice to see it helps. There is a lot of things to enhance in this module...

Feel free to modify it and share it back here !

Have a nice day

Jerome DERN

Share this post


Link to post
Share on other sites

I also have a need for this functionality, but in another DB langauage I used for a project.....:graduated:

Share this post


Link to post
Share on other sites

jerome, nice work!

I have added a line in your script, 

Func _XLSCreateWorkbook($file, $activesheet=1)
     If IsString($file) Then FileDelete($file)
     $activesheet -= 1
     FileWriteLine($file, '<?xml version="1.0"?>')

FileWriteLine($file, '<?mso-application progid="Excel.Sheet"?>') ;<<<<<<<<<<<< this line
     FileWriteLine($file, '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">')
     FileWriteLine($file, '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Author>XLSXMLGenerator</Author><Created>'&@YEAR&'-'&@MON&'-'&@MDAY&'T'&@HOUR&':'&@MIN&':'&@SEC&'</Created><Company>XLSXML</Company></DocumentProperties>')
     FileWriteLine($file, '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"><ActiveSheet>'&$activesheet&'</ActiveSheet></ExcelWorkbook>')
EndFunc

to avoid the message that appears when using Office2007/2010.

Thanks for sharing.

M.I.

Share this post


Link to post
Share on other sites

Hi,

I am new to AutoIt. I have a requirement of reading excel files through AutoIt where as Excel is not installed on the machine. Please help me.

-murali

Share this post


Link to post
Share on other sites

I belive this is a generation udf, not a reading udf.

try reading the helpfile then post some code in the general help and we will try to help.

Just writing "I have a requirement... Please help me." is probably not gonna get much interest


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

sorry, i wasn't clear earlier.

My intention is to know whether is there any way to read Excel through autoit scripts without excel being installed on that machine.

-murali

Share this post


Link to post
Share on other sites

Open the help file and go to User Defined Functions Refrence > Excel Management and see if you can figure it out.

Right now it looks like yes you can, but i dont know.


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

sorry, i wasn't clear earlier.

My intention is to know whether is there any way to read Excel through autoit scripts without excel being installed on that machine.

-murali

You can't use the Excel UDF because the UDF relies on the application being installed. The first statement in _ExcelBookOpen is:

Local $oExcel = ObjCreate("Excel.Application")

But if I remember correctly you can see an Excel file as a database and access it with ADO.

Edit: Please check thread or

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Okay, then thats cleared up!

I didnt read i just looked at the udf functions.

but what is the reason for not installing Excel on the machine ?

Edit: I cant click any of thoose things...

The link disapeard. :huh2:

Edited by Maffe811

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

I have re-inserted the links. Don't know why the were gone.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

but what is the reason for not installing Excel on the machine ?

Might be a server they don't want Office installed on, or a machine in a corporation that is locked down so it can't be installed, they don't want to/can't afford to pay for Excel/Office but need to generate spreadsheets, company machine that's not licensed to use Excel. Those are just off the top of my head. :huh2:


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Cause i was thinking the problem might have been money and buying MS office, because they could just get open office?


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

Thank you!...i am able to do it with ADO.

About Excel not being installed, we have a test environment wherein it should be a clean system without any software installed except the OS.

Thanks Once again!

Share this post


Link to post
Share on other sites

Great stuff Jerome, thanks for sharing!

5* from me!

Share this post


Link to post
Share on other sites

Hello Jerome,

theres a new excel xml udf ()

Could you combine these 2 UDF to one?

Share this post


Link to post
Share on other sites

jerome, nice work!

I have added a line in your script,

Func _XLSCreateWorkbook($file, $activesheet=1)
If IsString($file) Then FileDelete($file)
$activesheet -= 1
FileWriteLine($file, '<?xml version="1.0"?>')

FileWriteLine($file, '<?mso-application progid="Excel.Sheet"?>') ;<<<<<<<<<<<< this line
FileWriteLine($file, '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">')
FileWriteLine($file, '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Author>XLSXMLGenerator</Author><Created>'&@YEAR&'-'&@MON&'-'&@MDAY&'T'&@HOUR&':'&@MIN&':'&@SEC&'</Created><Company>XLSXML</Company></DocumentProperties>')
FileWriteLine($file, '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"><ActiveSheet>'&$activesheet&'</ActiveSheet></ExcelWorkbook>')
EndFunc

to avoid the message that appears when using Office2007/2010.

Thanks for sharing.

M.I.

even with the line you had added it stills showing up that error, try to change file extension to xml ;)

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0