Usina BRAdvPL Guide
Sign in
← All topics
CLASSPublished

FWMsExcelEx

FWMsExcelEx():New()

Generates Excel-compatible XML spreadsheets by writing data directly to the file and using less memory.

SpreadsheetsExcelXMLFramework
01 · OVERVIEW

Overview

FWMsExcelEx creates XML files compatible with Microsoft Excel 2003 and later versions, as well as alternative applications that can read the format. Because it writes directly to the file, it is suitable for larger data volumes. Build the output in worksheet, table, columns, rows, activation and XML-writing order.

02 · SYNTAX

Syntax

FWMsExcelEx():New()
03 · PRACTICAL EXAMPLE

Minimum creation flow

User Function ExExcelXML()
    Local oExcel := FWMsExcelEx():New()

    oExcel:AddWorkSheet("Customers")
    oExcel:AddTable("Customers", "Customer registry")
    oExcel:AddColumn("Customers", "Customer registry", "Code", 1, 1, .F.)
    oExcel:AddColumn("Customers", "Customer registry", "Name", 1, 1, .F.)
    oExcel:AddRow("Customers", "Customer registry", {"000001", "Sample customer"})
    oExcel:Activate()
    oExcel:GetXMLFile("customers.xml")
Return
Expected result

Creates a worksheet, adds a two-column table and one row, then writes customers.xml.

04 · PRACTICAL EXAMPLE

Formatting applied to the next row

oExcel:SetCelFont("Arial")
oExcel:SetCelSizeFont(10)
oExcel:SetCelBold(.T.)
oExcel:SetCelFrColor("#FFFFFF")
oExcel:SetCelBgColor("#003366")

oExcel:AddRow("Clientes", "Cadastro de clientes", ;
    {"000002", "Outro cliente"}, {1, 2})
Expected result

The configured style is applied to the cells listed in AddRow()’s fourth argument. Configure it immediately before the corresponding row.

05 · PRACTICAL EXAMPLE

Alignment, height and encoding

oExcel:SetUTF8Encode(.T.)
oExcel:SetTitleHAlign(2)
oExcel:SetHeaderHAlign(2)
oExcel:SetTitleVAlign(2)
oExcel:SetHeaderVAlign(2)
oExcel:SetLineVAlign(3)
oExcel:SetTitleHeight(24)
oExcel:SetHeadHeight(20)
oExcel:SetLineHeight(18)
Expected result

Configures encoding, horizontal and vertical alignment, and title, header and data-row heights.

06 · PRACTICAL EXAMPLE

Main methods by stage

// Ciclo de vida
New()  // ClassName()  // Activate()  // DeActivate()

// Estrutura e dados
AddWorkSheet()  // AddTable()  // AddColumn()  // AddRow()
IsWorkSheet()   // GetXMLFile()

// Estilos gerais e específicos
SetFont()       // SetFontSize()   // SetBold()
SetItalic()     // SetUnderLine()  // SetFrGeneralColor()
SetBgGeneralColor()
SetTitleFont()  // SetHeaderFont() // SetLineFont()
Set2LineFont()  // SetCelFont()    // SetCelBold()
Expected result

The class separates lifecycle, spreadsheet structure, data insertion and visual configuration. Title, Header, Line, 2Line and Cel families follow similar patterns.

BEST PRACTICES
  • Use one instance to build the file sequentially: AddWorkSheet(), AddTable(), AddColumn(), AddRow(), Activate() and GetXMLFile().
  • According to the consulted documentation, each worksheet accepts one table.
  • The SetFont*, SetTitle*, SetHeader*, SetLine*, Set2Line* and SetCel* method families configure fonts, sizes, bold, italic, underline and colors.
  • SetUTF8Encode() controls UTF-8 conversion; submitted content is encoded by default.
  • For new projects requiring native XLSX output, compare this class with FWMsExcelXlsx and FwPrinterXlsx.
COMMON PITFALLS
  • Writing only moves forward: after starting another worksheet, do not return to the previous one. Breaking the sequence may generate an invalid file.
  • Style settings must be defined before the AddRow() call to which they apply.
  • AddColumn() numeric codes represent alignment (1 left, 2 center, 3 right) and format (1 general, 2 number, 3 currency, 4 date and time).
  • Check parameter and method availability for your LIB version; lPrintHead is documented from LIB 20220613 onward.
  • The historical documentation contains a few inconsistent descriptions and spellings; validate signatures in your environment.

Related content

REFERENCES
  1. TOTVS. FWMsExcelEx — class for generating XML spreadsheets. TDN.
  2. TOTVS. Spreadsheet generation — related class index. TDN.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR