Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![License](https://img.shields.io/badge/License-Apache%202.0-green.svg)](LICENSE)
[![GitHub stars](https://img.shields.io/github/stars/chitralabs/sheetz?style=social)](https://github.com/chitralabs/sheetz)

**8 runnable examples** demonstrating every feature of the [Sheetz](https://github.com/chitralabs/sheetz) library — read, write, stream, and validate Excel and CSV files in Java with minimal code.
\*\*9 runnable examples\*\* demonstrating every feature of the [Sheetz](https://github.com/chitralabs/sheetz) library — read, write, stream, and validate Excel and CSV files in Java with minimal code.

```java
// This is all it takes to read an Excel file into Java objects
Expand Down Expand Up @@ -191,7 +191,7 @@ Sheetz.register(BigDecimal.class, new MoneyConverter());

### 08 — Format Conversion

Convert between XLSX, XLS, and CSV formats. Sheetz auto-detects the format from the file extension — converting is just read + write.
Convert between XLSX, XLS, CSV, and ODS formats. Sheetz auto-detects the format from the file extension — converting is just read + write.

```java
// XLSX → CSV
Expand All @@ -207,6 +207,37 @@ Sheetz.write(data, "products.xls");

---

### 09 — Cell Styling

Apply cell formatting with `@Style` annotations, build header styles programmatically with `CellStyleBuilder`, and embed clickable hyperlinks with `HyperlinkValue`.

```java
// @Style annotation for per-field styling
@Column("Product Name")
@Style(bold = true, fontColor = "#0000FF")
public String name;

@Column("Website")
@Style(hyperlink = true)
public HyperlinkValue website;

// Programmatic header style
CellStyleDef headerStyle = CellStyleBuilder.create()
.bold(true).backgroundColor("#003366").fontColor("#FFFFFF")
.horizontalAlignment("CENTER").build();

Sheetz.writer(StyledProduct.class)
.data(products)
.file("styled.xlsx")
.headerStyle(headerStyle)
.autoFilter(true)
.write();
```

[View source](src/main/java/io/github/chitralabs/sheetz/examples/E09_CellStyling.java)

---

## Running Examples

```bash
Expand All @@ -217,7 +248,7 @@ mvn compile exec:java -Dexec.mainClass="io.github.chitralabs.sheetz.examples.E01
mvn compile exec:java -Dexec.mainClass="io.github.chitralabs.sheetz.examples.E03_StreamingLargeFiles"

# Run all examples in sequence
for i in 01 02 03 04 05 06 07 08; do
for i in 01 02 03 04 05 06 07 08 09; do
echo "--- Running E${i} ---"
mvn -q compile exec:java -Dexec.mainClass="io.github.chitralabs.sheetz.examples.E${i}_$(ls src/main/java/io/github/chitralabs/sheetz/examples/E${i}_*.java | xargs basename | sed 's/.java//')"
done
Expand Down Expand Up @@ -251,16 +282,16 @@ implementation 'io.github.chitralabs.sheetz:sheetz-core:1.0.2'
Have a use case not covered here? **We want your example!**

Ideas we'd love PRs for:
- [ ] E09 — Spring Boot REST endpoint that exports data to Excel
- [ ] E10 — Database import pipeline (read Excel → save to JPA/Hibernate)
- [ ] E11 — Concurrent multi-file processing with ExecutorService
- [ ] E12 — Error recovery — partial import with validation report
- [ ] E13 — Dynamic headers — read files where column order is unknown
- [ ] E14 — Large file memory benchmark — heap usage comparison
- [ ] E10 — Spring Boot REST endpoint that exports data to Excel
- [ ] E11 — Database import pipeline (read Excel → save to JPA/Hibernate)
- [ ] E12 — Concurrent multi-file processing with ExecutorService
- [ ] E13 — Error recovery — partial import with validation report
- [ ] E14 — Dynamic headers — read files where column order is unknown
- [ ] E15 — Large file memory benchmark — heap usage comparison

**How to contribute an example:**
1. Copy an existing example file (e.g. `E01_BasicReadWrite.java`) as a template
2. Name it `E09_YourExampleName.java`
2. Name it `E10_YourExampleName.java`
3. Add it to `README.md` following the existing format
4. Open a PR — your name goes in the changelog!

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.chitralabs.sheetz</groupId>
<artifactId>sheetz-examples</artifactId>
<version>1.0.0</version>
<version>1.0.2</version>
<packaging>jar</packaging>

<name>Sheetz Examples</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* - XLSX → CSV
* - CSV → XLSX
* - XLSX → XLS (legacy format)
* - XLSX → ODS (OpenDocument format)
* - ODS → XLSX
* - Read and write with format auto-detection from file extension
*/
public class E08_FormatConversion {
Expand Down Expand Up @@ -55,20 +57,36 @@ public static void main(String[] args) {
Sheetz.write(fromXlsx, xlsPath);
System.out.println("Converted " + xlsxPath + " → " + xlsPath);

// --- XLSX → ODS ---
System.out.println("\n--- XLSX → ODS ---");
String odsPath = "output/converted.ods";
Sheetz.write(fromXlsx, odsPath);
System.out.println("Converted " + xlsxPath + " → " + odsPath);

// --- ODS → XLSX ---
System.out.println("\n--- ODS → XLSX ---");
List<Product> fromOds = Sheetz.read(odsPath, Product.class);
String fromOdsXlsx = "output/from_ods.xlsx";
Sheetz.write(fromOds, fromOdsXlsx);
System.out.println("Converted " + odsPath + " → " + fromOdsXlsx);

// --- Verify all formats contain the same data ---
System.out.println("\n--- Verification: all formats contain same data ---");
List<Product> verifyXlsx = Sheetz.read(newXlsxPath, Product.class);
List<Product> verifyCsv = Sheetz.read(csvPath, Product.class);
List<Product> verifyXls = Sheetz.read(xlsPath, Product.class);
List<Product> verifyOds = Sheetz.read(odsPath, Product.class);

System.out.println("XLSX rows: " + verifyXlsx.size());
System.out.println("CSV rows: " + verifyCsv.size());
System.out.println("XLS rows: " + verifyXls.size());
System.out.println("ODS rows: " + verifyOds.size());

System.out.println("\nFirst product from each format:");
System.out.println(" XLSX: " + verifyXlsx.get(0));
System.out.println(" CSV: " + verifyCsv.get(0));
System.out.println(" XLS: " + verifyXls.get(0));
System.out.println(" ODS: " + verifyOds.get(0));

System.out.println("\nDone!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.github.chitralabs.sheetz.examples;

import io.github.chitralabs.sheetz.Sheetz;
import io.github.chitralabs.sheetz.annotation.Column;
import io.github.chitralabs.sheetz.annotation.Style;
import io.github.chitralabs.sheetz.style.CellStyleBuilder;
import io.github.chitralabs.sheetz.style.CellStyleDef;
import io.github.chitralabs.sheetz.style.HyperlinkValue;

import java.util.List;

/**
* E09: Cell Styling — @Style annotation, programmatic styles, and hyperlinks.
*
* Demonstrates the cell formatting API added in Sheetz 1.0.2.
*/
public class E09_CellStyling {

// --- Model with @Style annotation ---
public static class StyledProduct {
@Column("Product Name")
@Style(bold = true, fontColor = "#0000FF")
public String name;

@Column("Price")
@Style(backgroundColor = "#FFFF00", horizontalAlignment = "CENTER", dataFormat = "#,##0.00")
public Double price;

@Column("Website")
@Style(hyperlink = true)
public HyperlinkValue website;

public StyledProduct() {}

public StyledProduct(String name, Double price, String url) {
this.name = name;
this.price = price;
this.website = new HyperlinkValue(name + " site", url);
}
}

public static void main(String[] args) {
System.out.println("=== E09: Cell Styling ===\n");

// 1. Write with @Style annotations
List<StyledProduct> products = List.of(
new StyledProduct("Widget", 29.99, "https://example.com/widget"),
new StyledProduct("Gadget", 49.99, "https://example.com/gadget"),
new StyledProduct("Gizmo", 19.99, "https://example.com/gizmo")
);

Sheetz.write(products, "styled-products.xlsx");
System.out.println("Wrote styled-products.xlsx with @Style annotations");

// 2. Programmatic header style with CellStyleBuilder
CellStyleDef headerStyle = CellStyleBuilder.create()
.bold(true)
.backgroundColor("#003366")
.fontColor("#FFFFFF")
.horizontalAlignment("CENTER")
.build();

Sheetz.writer(StyledProduct.class)
.data(products)
.file("styled-with-header.xlsx")
.headerStyle(headerStyle)
.autoFilter(true)
.write();

System.out.println("Wrote styled-with-header.xlsx with programmatic header style and auto-filter");

// 3. Read back hyperlinks
List<StyledProduct> readBack = Sheetz.read("styled-products.xlsx", StyledProduct.class);
for (StyledProduct p : readBack) {
System.out.printf(" %s — $%.2f%n", p.name, p.price);
}

System.out.println("\nDone!");
}
}
Loading