Minimal excel publication method w/ file naming

public byte[] pubExcel(PrintTemplate pt){
	pt.setFilename(pt.getFilename().replace("[attr]", getFieldValue("attr")));
	try{
		ExcelPOITool xls = new ExcelPOITool(true);
		ExcelPOITool.ExcelSheet sheet = xls.newSheet("Feuille 1");
		sheet.addFullRow(0, new String[]{"A", "B", "C"}); // at line 0
		xls.add(sheet);
		return xls.generateToByteArray();	
	}
	catch(Exception e){
		AppLog.error(getClass(), "pubExcel", "Excel generation error", e, getGrant());
		return null;
	}
}
1 Like

Same example with template filling from SQL query:

public byte[] pubExcel(PrintTemplate pt){
	//Simple Example of SQL query 
	String sqlQuery = "select prd_code, prd_name, prd_stock, prd_unitprice from demo_product where prd_sup_id="+getRowId();
	int line = 0;
	
	pt.setFilename(pt.getFilename().replace("[attr]", getFieldValue("attr")));
	try{
		ExcelPOITool xls = new ExcelPOITool(true);
		ExcelPOITool.ExcelSheet sheet = xls.newSheet("Simple sheet");
		for(String[] row : getGrant().query(sqlQuery))
			sheet.addFullRow(line++, row);
		xls.add(sheet);
		return xls.generateToByteArray();	
	}
	catch(Exception e){
		AppLog.error(getClass(), "pubExcel", "Excel generation error", e, getGrant());
		return null;
	}
}