// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Demo3 {
    private static final int N = 5000; 
    public static void main(String[] args) throws IOException {
        long t1 = System.currentTimeMillis();
        Workbook workbook = null;
        try {
            workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();
            Row headerrow = sheet.createRow(0);
            headerrow.createCell(0).setCellValue("Label");
            headerrow.createCell(1).setCellValue("Value");
            headerrow.createCell(2).setCellValue("Fraction");
            for(int i = 0; i < N; i++) {
                Row datarow = sheet.createRow(1 + i);
                datarow.createCell(0).setCellValue(String.format("Item#%d", i + 1));
                datarow.createCell(1).setCellValue(i + 1);
                datarow.createCell(2).setCellFormula(String.format("B%d/B5002", i + 2));
            }
            Row totalrow = sheet.createRow(1 + N);
            totalrow.createCell(0).setCellValue("Total");
            totalrow.createCell(1).setCellFormula("SUM(B2:B5001)");
            totalrow.createCell(2).setCellFormula("SUM(C2:C5001)");
            try(OutputStream os = new FileOutputStream("/work/bigdemo.xlsx")) {
                workbook.write(os);
            }
        } finally {
            workbook.close();
        }
        long t2 = System.currentTimeMillis();
        System.out.printf("%d ms\n", t2 - t1);
    }
}
