xlsx-handlebars
English | 中文文档
A Rust library for processing XLSX files with Handlebars templates, supporting multiple platforms:
- 🦀 Rust Native
- 🌐 WebAssembly (WASM)
- 📦 npm Package
- 🟢 Node.js
- 🦕 Deno
- 🌍 Browser
- 📋 JSR (JavaScript Registry)
Features
- ✅ Smart Merge: Automatically handles Handlebars syntax split by XML tags
- ✅ XLSX Validation: Built-in file format validation to ensure valid input files
- ✅ Handlebars Support: Full template engine with variables, conditions, loops, and Helper functions
- ✅ Cross-Platform: Rust native + WASM support for multiple runtimes
- ✅ TypeScript: Complete type definitions and IntelliSense
- ✅ Zero Dependencies: WASM binary with no external dependencies
- ✅ Error Handling: Detailed error messages and type-safe error handling
Installation
Rust
npm
Deno
import { render, init } from "jsr:@sail/xlsx-handlebars";
Usage Examples
Rust
use render_handlebars;
use json;
JavaScript/TypeScript (Node.js)
import from 'xlsx-handlebars';
import fs from 'fs';
.;
Deno
import { render, init } from "https://deno.land/x/xlsx_handlebars/mod.ts";
async function processTemplate() {
// Initialize WASM module
await init();
// Read template file
const templateBytes = await Deno.readFile("template.xlsx");
// Prepare data
const data = {
name: "Alice Johnson",
department: "R&D",
projects: [
{ name: "AI Customer Service", status: "Live" },
{ name: "Data Visualization Platform", status: "In Development" }
]
};
// Render template
const result = render(templateBytes, JSON.stringify(data));
// Save result
await Deno.writeFile("output.xlsx", new Uint8Array(result));
}
if (import.meta.main) {
await processTemplate();
}
Browser
XLSX Handlebars Example
Process Template
Template Syntax
Basic Variable Substitution
Employee Name: {{name}}
Company: {{company}}
Position: {{position}}
Conditional Rendering
{{#if has_bonus}}
Bonus: ${{bonus_amount}}
{{else}}
No Bonus
{{/if}}
{{#unless is_intern}}
Full-time Employee
{{/unless}}
Loop Rendering
Project Experience:
{{#each projects}}
- {{name}}: {{description}} ({{status}})
{{/each}}
Skills:
{{#each skills}}
{{@index}}. {{this}}
{{/each}}
Helper Functions
Built-in Helper functions:
<!-- Basic helpers -->
{{upper name}} <!-- Convert to uppercase -->
{{lower company}} <!-- Convert to lowercase -->
{{len projects}} <!-- Array length -->
{{#if (eq status "completed")}}Completed{{/if}} <!-- Equality comparison -->
{{#if (gt score 90)}}Excellent{{/if}} <!-- Greater than comparison -->
{{#if (lt age 30)}}Young{{/if}} <!-- Less than comparison -->
<!-- String concatenation -->
{{concat "Hello" " " "World"}} <!-- String concatenation -->
{{concat "Total: " count}} <!-- Mix strings and variables -->
<!-- Excel-specific helpers -->
{{num employee.salary}} <!-- Mark cell as number type -->
{{formula "=SUM(A1:B1)"}} <!-- Static Excel formula -->
{{formula (concat "=SUM(" (_c) "1:" (_c) "10)")}} <!-- Dynamic formula with current column -->
Excel Formula Helpers
Static Formula:
<!-- In Excel cell -->
{{formula "=SUM(A1:B1)"}}
{{formula "=AVERAGE(C2:C10)"}}
{{formula "=IF(D1>100,\"High\",\"Low\")"}}
Dynamic Formula with concat:
<!-- Dynamic row reference -->
{{formula (concat "=A" (_r) "*B" (_r))}}
<!-- Dynamic column reference -->
{{formula (concat "=SUM(" (_c) "2:" (_c) "10)")}}
<!-- Complex dynamic formula -->
{{formula (concat "=IF(" (_cr) ">100,\"High\",\"Low\")")}}
Available position helpers:
(_c)- Current column letter (A, B, C, ...)(_r)- Current row number (1, 2, 3, ...)(_cr)- Current cell reference (A1, B2, C3, ...)
Number Type Helper
Use {{num value}} to ensure a cell is treated as a number in Excel:
<!-- Without num: treated as text -->
{{employee.salary}}
<!-- With num: treated as number -->
{{num employee.salary}}
This is especially useful when:
- The value might be a string but should be treated as a number
- You want to ensure proper number formatting in Excel
- You need the value to work in formulas
Complex Example
=== Employee Report ===
Basic Information:
Name: {{employee.name}}
Department: {{employee.department}}
Position: {{employee.position}}
Hire Date: {{employee.hire_date}}
{{#if employee.has_bonus}}
💰 Bonus: ${{employee.bonus_amount}}
{{/if}}
Project Experience (Total {{len projects}}):
{{#each projects}}
{{@index}}. {{name}}
Description: {{description}}
Status: {{status}}
Team Size: {{team_size}} people
{{/each}}
Skills Assessment:
{{#each skills}}
- {{name}}: {{level}}/10 ({{years}} years of experience)
{{/each}}
To remove an entire row in a table, simply add to any cell:
{{removeRow}}
{{#if (gt performance.score 90)}}
🎉 Performance Rating: Excellent
{{else if (gt performance.score 80)}}
👍 Performance Rating: Good
{{else}}
📈 Performance Rating: Needs Improvement
{{/if}}
Error Handling
The library provides detailed error types and messages:
Rust
use ;
match render_handlebars
JavaScript/TypeScript
try catch
Build and Development
Build WASM Package
# Build all targets
# Or build separately
Run Examples
# Rust example
# Node.js example
# Deno example
# Browser example
# Then open http://localhost:8080 in your browser
# Select examples/template.xlsx file to test
Technical Features
Smart Merge Algorithm
The core innovation of this library is the smart merge of Handlebars syntax split by XML tags. In XLSX files, when users input template syntax, Excel may split it into multiple XML tags.
Performance and Compatibility
- Zero-Copy: Efficient memory management between Rust and WASM
- Streaming: Suitable for processing large XLSX files
- Cross-Platform: Supports Windows, macOS, Linux, Web
- Modern Browsers: Supports all modern browsers with WASM support
License
This project is licensed under the MIT License - see the LICENSE-MIT file for details.
Support
- 📚 Documentation
- 🐛 Issues
- 💬 Discussions
Support this project with a donation via Alipay:
