Generate Excel
SØAD includes built-in support for Excel file generation using Apache POI. This recipe demonstrates how to create Excel spreadsheets with data from your database, format cells, and serve them as downloadable files—perfect for reports, data exports, or financial statements.
Generate Excel from Database Data
CREATE TABLE sale (
id int NOT NULL AUTO_INCREMENT,
product_name varchar(255),
customer_name varchar(255),
sale_date date,
quantity int,
unit_price decimal(10,2),
PRIMARY KEY (id)
);
INSERT INTO sale (product_name, customer_name, sale_date, quantity, unit_price) VALUES
('Laptop Pro 15"', 'Acme Corporation', '2024-01-15', 5, 2499.99),
('Wireless Mouse', 'Tech Solutions Ltd', '2024-01-16', 25, 29.99),
('USB-C Hub', 'Digital Dynamics', '2024-01-17', 10, 89.99),
('Monitor 4K 27"', 'Creative Agency', '2024-01-18', 3, 599.99),
('Mechanical Keyboard', 'StartupXYZ', '2024-01-19', 15, 149.99),
('Webcam HD', 'Remote Workers Inc', '2024-01-20', 8, 79.99),
('Tablet Pro', 'Design Studio', '2024-01-21', 2, 899.99),
('Smartphone', 'Mobile Solutions', '2024-01-22', 12, 799.99),
('Headphones Pro', 'Audio Experts', '2024-01-23', 6, 299.99),
('Power Bank', 'Travel Co', '2024-01-24', 20, 49.99);
Download the Excel File
The user can download the Excel report via:
The browser will download the formatted Excel file with proper styling and data formatting.
Generate Excel with Charts
For more advanced Excel generation with charts and pivot tables, you can use Apache POI's chart capabilities.
Download the Chart Excel File
The user can download the Excel file with embedded chart via:
Generate Excel Template
Sometimes you need to generate Excel templates with predefined formatting that users can fill out.
Download the Template
The user can download the Excel template via:
Do you know?
- Use
attachment=Trueto force download instead of displaying inline - Apache POI supports both
.xls(older format) and.xlsx(newer format) files - For large datasets, consider using streaming APIs like
SXSSFfor better memory efficiency - You can password-protect Excel files using
workbook.writeProtectWorkbook(password, username)