PbXls Library

Version 2.6
Author: lcode.cn | License: Apache 2.0

Overview

PbXls is a PureBasic library for manipulating Excel files, requiring no Microsoft Office or any third-party dependencies to create and read Excel xlsx/xlsm files.

This library is ported from the Python openpyxl project, implemented using PureBasic's built-in XML and Packer libraries.

Note: Before using the PbXls library, you need to add XIncludeFile "PbXls.pb" at the beginning of your code to include the library file. The library initializes automatically; no manual initialization function call is required.

Features

Function Index

Workbook Operations

PbXls_CreateWorkbook()
Create a new Excel workbook.
Returns: Workbook ID (integer). Returns -1 on failure.
; Create a new workbook wbId = PbXls_CreateWorkbook() If wbId >= 0 ; Workbook created successfully EndIf
PbXls_LoadWorkbook(filename.s)
Load an existing Excel workbook file.
ParameterTypeDescription
filenameStringFull path to the Excel file
Returns: Workbook ID (integer). Returns -1 on failure.
Note: The read functionality is currently a placeholder implementation. Full read functionality will be implemented in future versions.
PbXls_SaveWorkbook(wbId.i, filename.s)
Save the workbook as an Excel file. This function generates an xlsx/xlsm file compliant with the Office Open XML standard.
ParameterTypeDescription
wbIdIntegerWorkbook ID (returned by CreateWorkbook or LoadWorkbook)
filenameStringFull path for the output file (.xlsx or .xlsm extension)
Returns: #True on success, #False on failure.
; Save workbook to file If PbXls_SaveWorkbook(wbId, "output.xlsx") Debug "Save successful!" Else Debug "Save failed!" EndIf
PbXls_GetSheetCount(wbId.i)
Get the number of worksheets in the workbook.
ParameterTypeDescription
wbIdIntegerWorkbook ID
Returns: Number of worksheets (integer).

Worksheet Operations

PbXls_AddWorksheet(wbId.i, title.s = "")
Add a new worksheet to the workbook.
ParameterTypeDescription
wbIdIntegerWorkbook ID
titleStringWorksheet name. If empty, a name in "SheetN" format will be auto-generated
Returns: Worksheet ID (integer). Returns -1 on failure.
; Add a worksheet with default name wsId1 = PbXls_AddWorksheet(wbId) ; Result: Sheet2 ; Add a worksheet with specified name wsId2 = PbXls_AddWorksheet(wbId, "Statistics") ; Result: Statistics
PbXls_DeleteWorksheet(wbId.i, index.i)
Delete the worksheet at the specified index in the workbook.
ParameterTypeDescription
wbIdIntegerWorkbook ID
indexIntegerIndex of the worksheet to delete (0-based)
Returns: #True on success, #False on failure.
Note: A workbook must have at least one worksheet. If only one worksheet remains, it cannot be deleted.
PbXls_GetSheetByIndex(wbId.i, index.i)
Get a worksheet by index. Returns a worksheet pointer for subsequent operations.
ParameterTypeDescription
wbIdIntegerWorkbook ID
indexIntegerWorksheet index (0-based)
Returns: Worksheet pointer. Returns 0 if the index is invalid.
; Get the first worksheet wsId = PbXls_GetSheetByIndex(wbId, 0) If wsId > 0 ; Ready to operate on the worksheet EndIf
PbXls_GetSheetTitle(wsId.i)
Get the name of the worksheet.
ParameterTypeDescription
wsIdIntegerWorksheet ID
Returns: Worksheet name (string).
PbXls_SetColumnWidth(wsId.i, col.i, width.f)
Set the width of the specified column.
ParameterTypeDescription
wsIdIntegerWorksheet ID
colIntegerColumn number (1-based)
widthFloatColumn width value
Returns: #True on success, #False on failure.
; Set column 1 width to 20 PbXls_SetColumnWidth(wsId, 1, 20.0) ; Set column 3 width to 15.5 PbXls_SetColumnWidth(wsId, 3, 15.5)
PbXls_SetRowHeight(wsId.i, row.i, height.f)
Set the height of the specified row.
ParameterTypeDescription
wsIdIntegerWorksheet ID
rowIntegerRow number (1-based)
heightFloatRow height value
Returns: #True on success, #False on failure.
PbXls_MergeCells(wsId.i, rangeString.s)
Merge cells in the specified range. Range format is "A1:B2" or "A1:D4".
ParameterTypeDescription
wsIdIntegerWorksheet ID
rangeStringStringMerge range, e.g. "A1:B2"
Returns: #True on success, #False on failure.
; Merge range A1 to B2 PbXls_MergeCells(wsId, "A1:B2") ; Merge range A1 to D4 PbXls_MergeCells(wsId, "A1:D4")
PbXls_UnmergeCells(wsId.i, rangeString.s)
Unmerge cells in the specified range.
ParameterTypeDescription
wsIdIntegerWorksheet ID
rangeStringStringRange to unmerge, e.g. "A1:B2"
Returns: #True on success, #False on failure.
PbXls_AppendRow(wsId.i, List values.s())
Append a row of data at the end of the worksheet. Data will be written starting from the next row after the current row, filling each column in order.
ParameterTypeDescription
wsIdIntegerWorksheet ID
valuesList StringList of strings to write, each element corresponds to a column
Returns: #True on success, #False on failure.
NewList data.s() AddElement(data()) data() = "Name" AddElement(data()) data() = "Age" AddElement(data()) data() = "City" PbXls_AppendRow(wsId, data())

Cell Operations

PbXls_SetCell(wsId.i, row.i, col.i, value.s)
Set the value of the specified cell. The cell data type is automatically inferred from the value (string, numeric, boolean, etc.).
ParameterTypeDescription
wsIdIntegerWorksheet ID
rowIntegerRow number (1-based)
colIntegerColumn number (1-based)
valueStringCell value
Returns: #True on success, #False on failure.
; Write string PbXls_SetCell(wsId, 1, 1, "Hello PbXls!") ; Write numeric value PbXls_SetCell(wsId, 1, 2, "123.45") ; Write boolean PbXls_SetCell(wsId, 1, 3, "true") ; Write date PbXls_SetCell(wsId, 1, 4, "2026-04-20")
PbXls_SetCellFormula(wsId.i, row.i, col.i, formula.s)
Set the formula for the specified cell. The formula must start with "=".
ParameterTypeDescription
wsIdIntegerWorksheet ID
rowIntegerRow number (1-based)
colIntegerColumn number (1-based)
formulaStringCell formula, e.g. "=SUM(A1:A10)"
Returns: #True on success, #False on failure.
; Set sum formula PbXls_SetCellFormula(wsId, 11, 1, "=SUM(A1:A10)") ; Set average formula PbXls_SetCellFormula(wsId, 11, 2, "=AVERAGE(B1:B10)")

Style Operations

PbXls_CreateFont(name.s = "Calibri", size.f = 11.0, color.s = "000000", bold.b = #False, italic.b = #False, underline.b = #False)
Create a font style.
Returns: Font style ID (integer).
PbXls_CreateFill(patternFill.s = "none", fgColor.s = "", bgColor.s = "")
Create a fill style.
Returns: Fill style ID (integer).
PbXls_CreateBorderStyle(top.s = "none", right.s = "none", bottom.s = "none", left.s = "none", color.s = "000000")
Create a border style.
Returns: Border style ID (integer).
PbXls_CreateAlignment(horizontal.s = "", vertical.s = "", wrapText.b = #False)
Create an alignment style.
Returns: Alignment style ID (integer).

Column Operations

PbXls_InsertColumns(wsId.i, colIdx.i, count.i = 1)
Insert columns at the specified position. After insertion, the column and all cells to its right will shift right. Column widths and merged cell ranges will be automatically updated.
ParameterTypeDescription
wsIdIntegerWorksheet ID
colIdxIntegerColumn number at insertion position (1-based)
countIntegerNumber of columns to insert, default is 1
Returns: #True on success, #False on failure.
; Insert 2 columns before column 3 PbXls_InsertColumns(wsId, 3, 2) ; Original column C and all columns to the right shift right by 2
PbXls_DeleteColumns(wsId.i, colIdx.i, count.i = 1)
Delete columns at the specified position. After deletion, cells to the right will shift left. Column widths and merged cell ranges will be automatically updated.
ParameterTypeDescription
wsIdIntegerWorksheet ID
colIdxIntegerStarting column number to delete (1-based)
countIntegerNumber of columns to delete, default is 1
Returns: #True on success, #False on failure.
; Delete column 2 (column B) PbXls_DeleteColumns(wsId, 2, 1) ; Original column C and all columns to the right shift left by 1

Data Validation

PbXls_CreateDataValidation(type.s, sqref.s, formula1.s = "", formula2.s = "", operator.s = "between")
Create a data validation rule. Supports multiple validation types: list (dropdown), whole (integer), decimal, textLength, date, time, custom (custom formula).
ParameterTypeDescription
typeStringValidation type, e.g. "list", "whole", "decimal", etc.
sqrefStringApplication range, e.g. "A1:A10"
formula1StringFormula or value (comma-separated options for list type)
formula2StringSecond formula (required for some operators)
operatorStringOperator, e.g. "between", "greaterThan", etc.
Returns: Validation rule ID (integer).
; Create dropdown list validation dv1 = PbXls_CreateDataValidation("list", "B2:B10", "Option1,Option2,Option3") ; Create integer range validation dv2 = PbXls_CreateDataValidation("whole", "C2:C10", "18", "65", "between")
PbXls_SetValidationPrompt(validationId.i, title.s, message.s)
Set the input prompt for data validation. Displayed when the cell receives focus.
ParameterTypeDescription
validationIdIntegerValidation rule ID
titleStringPrompt title
messageStringPrompt message
Returns: #True on success, #False on failure.
PbXls_SetValidationError(validationId.i, title.s, message.s)
Set the error prompt for data validation. Displayed when an invalid value is entered.
ParameterTypeDescription
validationIdIntegerValidation rule ID
titleStringError title
messageStringError message
Returns: #True on success, #False on failure.

Conditional Formatting

PbXls_CreateConditionalFormat(type.s, sqref.s, formula1.s = "", formula2.s = "", operator.s = "greaterThan")
Create a conditional formatting rule. Supported types: cellIs (cell value comparison), expression (formula), colorScale (color scale), dataBar (data bar), iconSet (icon set).
ParameterTypeDescription
typeStringRule type, e.g. "cellIs", "expression", "colorScale"
sqrefStringApplication range, e.g. "A1:A10"
formula1StringFormula or comparison value
formula2StringSecond formula
operatorStringOperator, e.g. "greaterThan", "lessThan", "between", etc.
Returns: Rule ID (integer).
; Red bold when cell value > 85 cf1 = PbXls_CreateConditionalFormat("cellIs", "E2:E10", "85", "", "greaterThan") PbXls_SetConditionalFormatDxf(cf1, "FF0000", "", #True, #False) ; Color scale (red-yellow-green) cf2 = PbXls_CreateConditionalFormat("colorScale", "D2:D10") PbXls_SetConditionalFormatColorScale(cf2, "FF0000", "FFFF00", "00FF00")
PbXls_SetConditionalFormatDxf(ruleId.i, fontColor.s = "", fillColor.s = "", fontBold.b = -1, fontItalic.b = -1)
Set the differential style (Dxf) for conditional formatting, including font color, fill color, bold, and italic.
ParameterTypeDescription
ruleIdIntegerRule ID
fontColorStringFont color, e.g. "FF0000" (red)
fillColorStringFill color, e.g. "FFFF00" (yellow)
fontBoldBooleanBold (-1 means unchanged)
fontItalicBooleanItalic (-1 means unchanged)
Returns: #True on success, #False on failure.
PbXls_SetConditionalFormatColorScale(ruleId.i, minColor.s = "", midColor.s = "", maxColor.s = "", minType.s = "", midType.s = "", maxType.s = "")
Set color scale parameters, including min/mid/max colors and their types.
ParameterTypeDescription
ruleIdIntegerRule ID
minColorStringMinimum value color
midColorStringMiddle value color
maxColorStringMaximum value color
minTypeStringMinimum value type, e.g. "min", "num", "percentile"
midTypeStringMiddle value type
maxTypeStringMaximum value type, e.g. "max", "num", "percentile"
Returns: #True on success, #False on failure.

Charts

Note: The chart feature is currently an interface implementation. Complete XML generation logic will be refined in future versions.
PbXls_CreateChart(type.s, title.s = "", anchorRef.s = "A1:F20")
Create a chart. Supported types: barChart (bar chart), lineChart (line chart), pieChart (pie chart), scatterChart (scatter chart), areaChart (area chart).
ParameterTypeDescription
typeStringChart type
titleStringChart title
anchorRefStringAnchor range, e.g. "A1:F20"
Returns: Chart ID (integer).
PbXls_AddChartSeries(chartId.i, name.s, values.s, categories.s = "")
Add a chart data series.
ParameterTypeDescription
chartIdIntegerChart ID
nameStringSeries name
valuesStringValue range, e.g. "A2:A10"
categoriesStringCategory range, e.g. "B2:B10"
Returns: #True on success, #False on failure.

Utility Functions

PbXls_GetColumnLetter(colNum.i)
Convert a column number to a column letter. For example, 1 converts to "A", 27 converts to "AA".
ParameterTypeDescription
colNumIntegerColumn number (1-based)
Returns: Column letter (string).
letter = PbXls_GetColumnLetter(1) ; Returns "A" letter = PbXls_GetColumnLetter(27) ; Returns "AA" letter = PbXls_GetColumnLetter(702) ; Returns "ZZ"
PbXls_ColumnIndexFromString(colLetter.s)
Convert a column letter to a column number. For example, "A" converts to 1, "AA" converts to 27.
ParameterTypeDescription
colLetterStringColumn letter (e.g. "A", "AA", "ZZ")
Returns: Column number (integer).
num = PbXls_ColumnIndexFromString("A") ; Returns 1 num = PbXls_ColumnIndexFromString("AA") ; Returns 27 num = PbXls_ColumnIndexFromString("ZZ") ; Returns 702
PbXls_EscapeXML(str.s)
Escape XML special characters in a string. Converts &, <, >, " and ' to XML entities.
ParameterTypeDescription
strStringString to escape
Returns: Escaped string.
escaped = PbXls_EscapeXML("A < B & C > D") ; Returns "A < B & C > D"
PbXls_Free()
Release all resources used by the PbXls library. This function should be called before the program exits.
Returns: #True on success.
; Clean up resources before program exit PbXls_Free()

Example Code

Example 1: Create a Basic Excel File

; PbXls - Create a basic Excel file example XIncludeFile "PbXls.pb" ; Create workbook wbId = PbXls_CreateWorkbook() ; Get the first worksheet wsId = PbXls_GetSheetByIndex(wbId, 0) ; Write headers PbXls_SetCell(wsId, 1, 1, "Name") PbXls_SetCell(wsId, 1, 2, "Age") PbXls_SetCell(wsId, 1, 3, "City") ; Write data rows PbXls_SetCell(wsId, 2, 1, "Alice") PbXls_SetCell(wsId, 2, 2, "25") PbXls_SetCell(wsId, 2, 3, "New York") PbXls_SetCell(wsId, 3, 1, "Bob") PbXls_SetCell(wsId, 3, 2, "30") PbXls_SetCell(wsId, 3, 3, "London") ; Save file PbXls_SaveWorkbook(wbId, "example1.xlsx") ; Clean up resources PbXls_Free()

Example 2: Create an Excel File with Formulas

; PbXls - Create an Excel file with formulas example XIncludeFile "PbXls.pb" ; Create workbook wbId = PbXls_CreateWorkbook() wsId = PbXls_GetSheetByIndex(wbId, 0) ; Write headers PbXls_SetCell(wsId, 1, 1, "Item") PbXls_SetCell(wsId, 1, 2, "Amount") ; Write data PbXls_SetCell(wsId, 2, 1, "Salary") PbXls_SetCell(wsId, 2, 2, "10000") PbXls_SetCell(wsId, 3, 1, "Bonus") PbXls_SetCell(wsId, 3, 2, "5000") PbXls_SetCell(wsId, 4, 1, "Other") PbXls_SetCell(wsId, 4, 2, "2000") ; Add sum formula PbXls_SetCell(wsId, 5, 1, "Total") PbXls_SetCellFormula(wsId, 5, 2, "=SUM(B2:B4)") ; Save file PbXls_SaveWorkbook(wbId, "example2_formula.xlsx") ; Clean up resources PbXls_Free()

Example 3: Create an Excel File with Styles

; PbXls - Create an Excel file with styles example XIncludeFile "PbXls.pb" ; Create workbook wbId = PbXls_CreateWorkbook() wsId = PbXls_GetSheetByIndex(wbId, 0) ; Write data and set column width PbXls_SetCell(wsId, 1, 1, "Report Title") PbXls_SetColumnWidth(wsId, 1, 30.0) ; Merge cells PbXls_MergeCells(wsId, "A1:C1") ; Write multiple rows of data NewList row1.s() AddElement(row1()) row1() = "Data1" AddElement(row1()) row1() = "Data2" AddElement(row1()) row1() = "Data3" PbXls_AppendRow(wsId, row1()) ; Save file PbXls_SaveWorkbook(wbId, "example3_style.xlsx") ; Clean up resources PbXls_Free()

Version History

v2.6 (2026-04-22)

v2.5 (2026-04-22)

v2.4 (2026-04-21)

v2.3 (2026-04-20)

v2.2 (2026-04-14)

v2.1 (2026-04-04)

v2.0 (2026-03-25)

v1.9 (2026-03-15)

v1.8 (2026-03-05)

v1.7 (2026-02-23)

v1.6 (2026-02-13)

v1.5 (2026-02-03)

v1.4 (2026-01-24)

v1.3 (2026-01-14)

v1.2 (2026-01-04)

v1.1 (2025-12-25)

v1.0 (2025-12-15)