Código do meu post anterior com erros, agora corrigido, testado e aprovado
Código: Selecionar todos
#include "hbmxml.ch"
Static xml_sError__ := ''
Static xml_bError__ := .F.
/* ----------------------------------- */
/* Main()
/* ----------------------------------- */
Proc Main()
SetMode( 32, 80 )
cls
ssheet_Create()
// Verifica XML
If xml_bError__
hb_Alert( 'ERRO: ' + xml_sError__ )
else
hb_Alert( 'Arquivo XML Criado ' )
End
Return
/* ----------------------------------- */
/* Cria Planilha em XML
/* ----------------------------------- */
Proc ssheet_Create()
Local i, n
Local oXML
Local hXML := hb_Hash()
hXML[ "Cols" ] := 16
hXML[ "Rows" ] := 10
// Log erros XML
mxmlSetErrorCallback( @ssheet_XmlError() )
// Sem quebra de linha
mxmlSetWrapMargin( 0 )
// Manipulação XML
oXml := mxmlLoadString( NIL, ssheet_XmlBase(), MXML_OPAQUE_CALLBACK )
// Verifica XML
If xml_bError__
hb_Alert( 'ERRO: ' + xml_sError__ )
End
// Localiza TAG/Node
hXML[ "Table" ] := mxmlFindElement( oXml, oXml, "Table", NIL, NIL, MXML_DESCEND )
If Empty( hXML[ "Table" ] )
hb_Alert( 'Erro: Tag ' + "Table" )
Else
// Formata Colunas
For i := 1 To hXML[ "Cols" ]
hXML[ 'Column' ] := mxmlNewElement( hXML[ "Table" ], 'Column' )
mxmlElementSetAttr( hXML[ "Column" ], 'ss:Index', hb_NTos( i ) )
mxmlElementSetAttr( hXML[ "Column" ], 'ss:AutoFitWidth', '0' )
mxmlElementSetAttr( hXML[ "Column" ], 'ss:Width', '64' )
Next
// Linha Cabeçalho
hXML[ 'Row' ] := mxmlNewElement( hXML[ "Table" ], 'Row' )
mxmlElementSetAttr( hXML[ "Row" ], 'ss:AutoFitHeight', '0' )
For i := 1 To hXML[ "Cols" ]
hXML[ 'Cell' ] := mxmlNewElement( hXML[ "Row" ], 'Cell' )
mxmlElementSetAttr( hXML[ "Cell" ], 'ss:StyleID', 'Header' )
hXML[ 'Data' ] := mxmlNewElement( hXML[ "Cell" ], 'Data' )
mxmlElementSetAttr( hXML[ "Data" ], 'ss:Type', 'String' )
mxmlNewText( hXML[ 'Data' ], 0, StrZero( i, 9 ) )
Next
// Dados Tabela
For i := 1 To hXML[ "Rows" ]
hXML[ 'Row' ] := mxmlNewElement( hXML[ "Table" ], 'Row' )
mxmlElementSetAttr( hXML[ "Row" ], 'ss:AutoFitHeight', '0' )
For n := 1 To hXML[ "Cols" ]
hXML[ 'Cell' ] := mxmlNewElement( hXML[ "Row" ], 'Cell' )
hXML[ 'Data' ] := mxmlNewElement( hXML[ "Cell" ], 'Data' )
mxmlElementSetAttr( hXML[ "Data" ], 'ss:Type', 'String' )
mxmlNewText( hXML[ 'Data' ], 0, StrZero( i, 9 ) )
Next
Next
// Ajusta dados da planilha (Linhas e Colunas)
mxmlElementSetAttr( hXML[ "Table" ], 'ss:ExpandedColumnCount', hb_NToS( hXML[ "Cols" ] ) )
mxmlElementSetAttr( hXML[ "Table" ], 'ss:ExpandedRowCount', hb_NToS( hXML[ "Rows" ] + 1 ) )
Endif
mxmlSaveFile( oXml, 'c:\temp\planilha.xls' )
mxmlDelete( oXml )
Return
/* ----------------------------------- */
/* Log erros XML
/* ----------------------------------- */
Static Proc ssheet_XmlError( s_Erro )
xml_sError__ += s_Erro + hb_Eol()
xml_bError__ := .T.
Return
/* ----------------------------------- */
/* Xml_Base INTO <v>
/* ----------------------------------- */
#xcommand Xml_Base INTO <v> => #pragma __text|<v>+=%s;<v>:=""
/* ----------------------------------- */
/* XML Base para Planilha em XML
/* ----------------------------------- */
Func ssheet_XmlBase()
Local cXml
Xml_Base into cXml
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<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">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Ana Mitoooo</Author>
<Version>12.00</Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>12600</WindowHeight>
<WindowWidth>28755</WindowWidth>
<WindowTopX>0</WindowTopX>
<WindowTopY>75</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="Header">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"
ss:Bold="1"/>
</Style>
<Style ss:ID="ShortDate">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
<NumberFormat ss:Format="Short Date"/>
</Style>
<Style ss:ID="Standard">
<NumberFormat ss:Format="Standard"/>
</Style>
</Styles>
<Worksheet ss:Name="XML SpreadSheet">
<Table ss:ExpandedColumnCount="0" ss:ExpandedRowCount="0" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="15">
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.31496062000000002"/>
<Footer x:Margin="0.31496062000000002"/>
<PageMargins x:Bottom="0.78740157499999996" x:Left="0.511811024" x:Right="0.511811024" x:Top="0.78740157499999996"/>
</PageSetup>
<Unsynced/>
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>9</PaperSizeIndex>
<HorizontalResolution>360</HorizontalResolution>
<VerticalResolution>360</VerticalResolution>
</Print>
<Selected/>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
EndText
cXml := StrTran( cXml, hb_Eol(), '' )
cXml := StrTran( cXml, ' </', '</' )
cXml := StrTran( cXml, '/> ', '/>' )
Return cXml