Skip to content

Instantly share code, notes, and snippets.

@firegloves
Last active July 11, 2019 20:49
Show Gist options
  • Save firegloves/32580295a963b6344e572181a954e315 to your computer and use it in GitHub Desktop.
Save firegloves/32580295a963b6344e572181a954e315 to your computer and use it in GitHub Desktop.
MemPOI - Multisheet example
try {
// creates destination file
File file = new File("multisheet.xlsx");
// creates sheet 1 with a PreparedStatement and the sheet name
MempoiSheet dogsSheet = MempoiSheetBuilder.aMempoiSheet()
.withSheetName("Dogs sheet")
.withPrepStmt(conn.prepareStatement("SELECT pet_name AS DOG_NAME, pet_race AS DOG_RACE FROM pets WHERE pet_type = 'dog'"))
.build();
// creates sheet 2 with a PreparedStatement and the sheet name
MempoiSheet catsSheet = MempoiSheetBuilder.aMempoiSheet()
.withSheetName("Cats sheet")
.withPrepStmt(conn.prepareStatement("SELECT pet_name AS CAT_NAME, pet_race AS CAT_RACE FROM pets WHERE pet_type = 'cat'"))
.build();
// creates sheet 3 with a PreparedStatement and the sheet name
MempoiSheet birdsSheet = MempoiSheetBuilder.aMempoiSheet()
.withSheetName("Birds sheet")
.withPrepStmt(conn.prepareStatement("SELECT pet_name AS BIRD_NAME, pet_race AS BIRD_RACE FROM pets WHERE pet_type = 'bird'"))
.build();
// creates MemPOI using its builder
MemPOI memPOI = MempoiBuilder.aMemPOI()
.withDebug(true)
.withFile(file)
.withAdjustColumnWidth(true)
.addMempoiSheet(dogsSheet)
.addMempoiSheet(catsSheet)
.addMempoiSheet(birdsSheet)
.build();
// exports to file and gets the generated report absolute filename
String absFilename = memPOI.prepareMempoiReportToFile().get();
} catch (ExecutionException e) {
// using CallableFuture.get() results in an ExecutionException containing the MempoiException with the real error message
System.out.println(e.getCause().getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment