// Code examples from: Read and Write Excel Files in Java with Apache POI
// https://www.atulvij.com/java-read-write-excel-apache-poi
// Review and adapt before running; examples use sample values.

// ---- Example 1 (java) ----
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;

public class ReadExcel {
    public static void main(String[] args) throws Exception {
        DataFormatter fmt = new DataFormatter();   // shows values as Excel displays them
        try (FileInputStream in = new FileInputStream("sales.xlsx");
             Workbook wb = new XSSFWorkbook(in)) {
            Sheet sheet = wb.getSheet("Data");
            for (Row row : sheet) {
                if (row.getRowNum() == 0) continue;          // skip header
                String rep = fmt.formatCellValue(row.getCell(0));
                double qty = row.getCell(1).getNumericCellValue();
                System.out.println(rep + " sold " + qty);
            }
        }
    }
}

// ---- Example 2 (java) ----
try (Workbook wb = new XSSFWorkbook();
     FileOutputStream out = new FileOutputStream("report.xlsx")) {
    Sheet s = wb.createSheet("Report");
    CellStyle bold = wb.createCellStyle();
    Font f = wb.createFont(); f.setBold(true); bold.setFont(f);

    Row header = s.createRow(0);
    String[] cols = {"Region", "Revenue"};
    for (int i = 0; i < cols.length; i++) {
        Cell c = header.createCell(i);
        c.setCellValue(cols[i]);
        c.setCellStyle(bold);
    }
    Row r = s.createRow(1);
    r.createCell(0).setCellValue("North");
    r.createCell(1).setCellValue(265739);
    s.createRow(2).createCell(1).setCellFormula("SUM(B2:B2)");
    s.autoSizeColumn(0);
    wb.write(out);
}

