Due to lack of time I'll just paste the code bits I currently use. It might be too complex, because we store the generated files on our as400 ifs and add references to the database. Retrieving the file is a separate process.
function createExcelFromArray(arr any[] in, columns ExcelColumnRec[] in, filename string in, omschrijving string in, headerString String? in, laatsteRij String[] in) returns(decimal(8)) wb Workbook = new HSSFWorkbook(); createHelper CreationHelper = wb.getCreationHelper(); sheet Sheet = wb.createSheet("Pag 1"); rowCounter int = 0; row Row = sheet.createRow(rowCounter); if (headerString != null) headerCs CellStyle = wb.createCellStyle(); headerCs.setAlignment(CellStyle.ALIGN_CENTER); c Cell = row.createCell(0); c.setCellStyle(headerCs); c.setCellValue(createHelper.createRichTextString(headerString)); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, columns.getSize() - 1)); rowCounter += 1; row = sheet.createRow(rowCounter); end csDate CellStyle = wb.createCellStyle(); csDate.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy")); csWrap CellStyle = wb.createCellStyle(); csWrap.setWrapText(true); for(i int from 1 to columns.getSize()) c Cell = row.createCell(i - 1); c.setCellValue(createHelper.createRichTextString(columns[i].title)); end for(i int from 1 to arr.getSize()) rowCounter += 1; row = sheet.createRow(rowCounter); for(j int from 1 to columns.getSize()) c Cell = row.createCell(j - 1); waarde any = getCellValue(i, columns[j].name, arr); strval string; try strval = waarde; onException(exception AnyException) end case when(waarde isa date) c.setCellValue((waarde as date) + 1); c.setCellStyle(csDate); when(waarde isa date?) if((waarde as date?) == null) c.setCellValue(createHelper.createRichTextString("")); else c.setCellValue((waarde as date) + 1); c.setCellStyle(csDate); end when(waarde isa String[]) sArr String[] = waarde as String[]; s String; for (z int from 1 to sArr.getSize()) if (z == 1) s += sArr[z]; else s += "\n" + sArr[z]; end end c.setCellValue(createHelper.createRichTextString(s)); c.setCellStyle(csWrap); when(waarde isa string) s string = waarde; c.setCellValue(createHelper.createRichTextString(waarde)); when(strval is numeric) c.setCellValue(waarde as int); otherwise ispunt boolean = false; try q decimal(10,4) = waarde as decimal(10,4) + 1; c.setCellValue(waarde as decimal(10,4)); onException(exception AnyException) end end end end if (laatsteRij.getSize() != 0) rowCounter += 1; row = sheet.createRow(rowCounter); for (i int from 1 to laatsteRij.getSize()) c Cell = row.createCell(i - 1); s string = laatsteRij[i]; c.setCellValue(createHelper.createRichTextString(s)); end end for (i int from 1 to columns.getSize()) sheet.autoSizeColumn(i-1); end f File = new File(filename); SysLib.writeStdout(f.getCanonicalPath()); fos FileOutputStream = new FileOutputStream(f); wb.write(fos); fos.java_close(); pdfFile File = new File(f.getCanonicalPath()); bestandTypeRec UIBestandTypeRec{}; bestandTypeRec = BestandLib.getBestandType(0); // geentype bestand UIBestandExtRec{bestandrec = new UIBestandRec{DatumWijz = datetimelib.currentTimeStamp(), omschr = omschrijving, origBestandNaam = filename, extensie = "xls"}, types =[bestandTypeRec ]}; BestandLib.addBestand(bestand); fDest File; if(GeneralOperations.getHostname() == "AS40071.MOL.LOCAL") fDest = new File(FileBasicOperations.movePathToQNC(bestand.bestandrec.pad)); else fDest = new File(bestand.bestandrec.pad); end FileBasicOperations.copyFile(pdfFile, fDest); FileBasicOperations.removeFile(pdfFile); return(bestand.bestandrec.id); end
This requires the following classes from Apache Poi
- Cell
- CellRangeAddress
- CellStyle
- CreationHelper
- HSSFWorkbook
- Row
- Sheet
- Workbook
to be created as external types like this:
externaltype Cell type JavaObject{packageName = "org.apache.poi.ss.usermodel", javaName = "Cell"} function setCellValue(value boolean in); function setCellValue(text RichTextString in); function setCellValue(value float in); function setCellStyle(style CellStyle in); end
Externaltype CellRangeAddress extends CellRangeAddressBase type JavaObject{packageName = "org.apache.poi.ss.util", javaName = "CellRangeAddress"} constructor(firstRow int in, lastRow int in, firstCol int in, lastCol int in); end
Externaltype CellStyle type JavaObject{packageName = "org.apache.poi.ss.usermodel", javaName = "CellStyle"} static ALIGN_CENTER smallint; function setDataFormat(format smallint in); function setWrapText(wrapped boolean in); function setAlignment(alignment smallint in); end
externaltype CreationHelper type JavaObject{packageName = "org.apache.poi.ss.usermodel ", javaName = "CreationHelper"} function createRichTextString(text String in) returns (RichTextString); function createDataFormat() returns (DataFormat); end
externaltype HSSFWorkbook extends POIDocument, Workbook type JavaObject{packageName = "org.apache.poi.hssf.usermodel", javaName = "HSSFWorkbook"} constructor(); end
externaltype Row type JavaObject{packageName = "org.apache.poi.ss.usermodel", javaName = "Row"} function createCell(column int in) returns (HSSFCell); end
externalType Sheet type JavaObject{packageName = "org.apache.poi.ss.usermodel", javaName = "Sheet"} function createRow(rowNum int in) returns (HSSFRow); function addMergedRegion(region CellRangeAddress in); function autoSizeColumn(column int in); end
externaltype Workbook type JavaObject{packageName = "org.apache.poi.ss.usermodel", javaName = "Workbook"} function getCreationHelper() returns (CreationHelper); function createSheet(name String in) returns (HSSFSheet); function write(outStream OutputStream in); function createCellStyle() returns (CellStyle); end
ExternalType OutputStream extends Object type JavaObject{packageName = "java.io", javaName = "OutputStream"} function flush(); function java_close(){JavaName = "close"};end
ExternalType FileOutputStream extends OutputStream type JavaObject{packageName = "java.io", javaName = "FileOutputStream"} constructor(pad String in); constructor(f File in);end
This is a quick example, just let me know if I can help refine it or translate some of the variable names.
Kind regards,
Bram
Bram_Callewaert