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
- Create and read Excel xlsx/xlsm files
- Read and write cell data (string, numeric, formula, boolean, date, etc.)
- Multiple worksheet support
- Cell styles (font, fill, border, alignment, number format)
- Merged cells
- Row/column width/height settings
- Column insert/delete (automatically updates cells, column widths, merged cells)
- Data validation (dropdown lists, integer validation, decimal validation, etc.)
- Conditional formatting (cell value comparison, formula conditions, color scales)
- Chart support (bar chart, line chart, pie chart, etc.)
- Shared strings table optimization
- Complete ZIP compression support
Workbook Operations
PbXls_CreateWorkbook()
Create a new Excel workbook.
Returns: Workbook ID (integer). Returns -1 on failure.
wbId = PbXls_CreateWorkbook()
If wbId >= 0
EndIf
PbXls_LoadWorkbook(filename.s)
Load an existing Excel workbook file.
| Parameter | Type | Description |
| filename | String | Full 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.
| Parameter | Type | Description |
| wbId | Integer | Workbook ID (returned by CreateWorkbook or LoadWorkbook) |
| filename | String | Full path for the output file (.xlsx or .xlsm extension) |
Returns: #True on success, #False on failure.
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.
| Parameter | Type | Description |
| wbId | Integer | Workbook ID |
Returns: Number of worksheets (integer).
Worksheet Operations
PbXls_AddWorksheet(wbId.i, title.s = "")
Add a new worksheet to the workbook.
| Parameter | Type | Description |
| wbId | Integer | Workbook ID |
| title | String | Worksheet name. If empty, a name in "SheetN" format will be auto-generated |
Returns: Worksheet ID (integer). Returns -1 on failure.
wsId1 = PbXls_AddWorksheet(wbId)
wsId2 = PbXls_AddWorksheet(wbId, "Statistics")
PbXls_DeleteWorksheet(wbId.i, index.i)
Delete the worksheet at the specified index in the workbook.
| Parameter | Type | Description |
| wbId | Integer | Workbook ID |
| index | Integer | Index 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.
| Parameter | Type | Description |
| wbId | Integer | Workbook ID |
| index | Integer | Worksheet index (0-based) |
Returns: Worksheet pointer. Returns 0 if the index is invalid.
wsId = PbXls_GetSheetByIndex(wbId, 0)
If wsId > 0
EndIf
PbXls_GetSheetTitle(wsId.i)
Get the name of the worksheet.
| Parameter | Type | Description |
| wsId | Integer | Worksheet ID |
Returns: Worksheet name (string).
PbXls_SetColumnWidth(wsId.i, col.i, width.f)
Set the width of the specified column.
| Parameter | Type | Description |
| wsId | Integer | Worksheet ID |
| col | Integer | Column number (1-based) |
| width | Float | Column width value |
Returns: #True on success, #False on failure.
PbXls_SetColumnWidth(wsId, 1, 20.0)
PbXls_SetColumnWidth(wsId, 3, 15.5)
PbXls_SetRowHeight(wsId.i, row.i, height.f)
Set the height of the specified row.
| Parameter | Type | Description |
| wsId | Integer | Worksheet ID |
| row | Integer | Row number (1-based) |
| height | Float | Row 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".
| Parameter | Type | Description |
| wsId | Integer | Worksheet ID |
| rangeString | String | Merge range, e.g. "A1:B2" |
Returns: #True on success, #False on failure.
PbXls_MergeCells(wsId, "A1:B2")
PbXls_MergeCells(wsId, "A1:D4")
PbXls_UnmergeCells(wsId.i, rangeString.s)
Unmerge cells in the specified range.
| Parameter | Type | Description |
| wsId | Integer | Worksheet ID |
| rangeString | String | Range 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.
| Parameter | Type | Description |
| wsId | Integer | Worksheet ID |
| values | List String | List 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.).
| Parameter | Type | Description |
| wsId | Integer | Worksheet ID |
| row | Integer | Row number (1-based) |
| col | Integer | Column number (1-based) |
| value | String | Cell value |
Returns: #True on success, #False on failure.
PbXls_SetCell(wsId, 1, 1, "Hello PbXls!")
PbXls_SetCell(wsId, 1, 2, "123.45")
PbXls_SetCell(wsId, 1, 3, "true")
PbXls_SetCell(wsId, 1, 4, "2026-04-20")
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.
| Parameter | Type | Description |
| wsId | Integer | Worksheet ID |
| colIdx | Integer | Column number at insertion position (1-based) |
| count | Integer | Number of columns to insert, default is 1 |
Returns: #True on success, #False on failure.
PbXls_InsertColumns(wsId, 3, 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.
| Parameter | Type | Description |
| wsId | Integer | Worksheet ID |
| colIdx | Integer | Starting column number to delete (1-based) |
| count | Integer | Number of columns to delete, default is 1 |
Returns: #True on success, #False on failure.
PbXls_DeleteColumns(wsId, 2, 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).
| Parameter | Type | Description |
| type | String | Validation type, e.g. "list", "whole", "decimal", etc. |
| sqref | String | Application range, e.g. "A1:A10" |
| formula1 | String | Formula or value (comma-separated options for list type) |
| formula2 | String | Second formula (required for some operators) |
| operator | String | Operator, e.g. "between", "greaterThan", etc. |
Returns: Validation rule ID (integer).
dv1 = PbXls_CreateDataValidation("list", "B2:B10", "Option1,Option2,Option3")
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.
| Parameter | Type | Description |
| validationId | Integer | Validation rule ID |
| title | String | Prompt title |
| message | String | Prompt 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.
| Parameter | Type | Description |
| validationId | Integer | Validation rule ID |
| title | String | Error title |
| message | String | Error message |
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).
| Parameter | Type | Description |
| type | String | Chart type |
| title | String | Chart title |
| anchorRef | String | Anchor range, e.g. "A1:F20" |
Returns: Chart ID (integer).
PbXls_AddChartSeries(chartId.i, name.s, values.s, categories.s = "")
Add a chart data series.
| Parameter | Type | Description |
| chartId | Integer | Chart ID |
| name | String | Series name |
| values | String | Value range, e.g. "A2:A10" |
| categories | String | Category 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".
| Parameter | Type | Description |
| colNum | Integer | Column number (1-based) |
Returns: Column letter (string).
letter = PbXls_GetColumnLetter(1)
letter = PbXls_GetColumnLetter(27)
letter = PbXls_GetColumnLetter(702)
PbXls_ColumnIndexFromString(colLetter.s)
Convert a column letter to a column number. For example, "A" converts to 1, "AA" converts to 27.
| Parameter | Type | Description |
| colLetter | String | Column letter (e.g. "A", "AA", "ZZ") |
Returns: Column number (integer).
num = PbXls_ColumnIndexFromString("A")
num = PbXls_ColumnIndexFromString("AA")
num = PbXls_ColumnIndexFromString("ZZ")
PbXls_EscapeXML(str.s)
Escape XML special characters in a string. Converts &, <, >, " and ' to XML entities.
| Parameter | Type | Description |
| str | String | String to escape |
Returns: Escaped string.
escaped = PbXls_EscapeXML("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.
PbXls_Free()
Example Code
Example 1: Create a Basic Excel File
XIncludeFile "PbXls.pb"
wbId = PbXls_CreateWorkbook()
wsId = PbXls_GetSheetByIndex(wbId, 0)
PbXls_SetCell(wsId, 1, 1, "Name")
PbXls_SetCell(wsId, 1, 2, "Age")
PbXls_SetCell(wsId, 1, 3, "City")
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")
PbXls_SaveWorkbook(wbId, "example1.xlsx")
PbXls_Free()
Example 2: Create an Excel File with Formulas
XIncludeFile "PbXls.pb"
wbId = PbXls_CreateWorkbook()
wsId = PbXls_GetSheetByIndex(wbId, 0)
PbXls_SetCell(wsId, 1, 1, "Item")
PbXls_SetCell(wsId, 1, 2, "Amount")
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")
PbXls_SetCell(wsId, 5, 1, "Total")
PbXls_SetCellFormula(wsId, 5, 2, "=SUM(B2:B4)")
PbXls_SaveWorkbook(wbId, "example2_formula.xlsx")
PbXls_Free()
Example 3: Create an Excel File with Styles
XIncludeFile "PbXls.pb"
wbId = PbXls_CreateWorkbook()
wsId = PbXls_GetSheetByIndex(wbId, 0)
PbXls_SetCell(wsId, 1, 1, "Report Title")
PbXls_SetColumnWidth(wsId, 1, 30.0)
PbXls_MergeCells(wsId, "A1:C1")
NewList row1.s()
AddElement(row1())
row1() = "Data1"
AddElement(row1())
row1() = "Data2"
AddElement(row1())
row1() = "Data3"
PbXls_AppendRow(wsId, row1())
PbXls_SaveWorkbook(wbId, "example3_style.xlsx")
PbXls_Free()