Skip to content

Instantly share code, notes, and snippets.

// Note this method uses Consumer rather than Function since it only mutates the notebook and does not return anything.
public Workbook workbook(Consumer<Workbook> block) {
Workbook wb = new HSSFWorkbook();
block.accept(wb);
return wb;
}
// We can call the method like this:
public void tryItOut() {
workbook((Workbook wb) -> {
@ciferkey
ciferkey / merge.kt
Last active October 14, 2018 14:29
fun Sheet.merge(firstRow: Int, lastRow: Int,firstCol: Int, lastCol: Int) {
addMergedRegion(CellRangeAddress(firstRow, lastRow, firstCol, lastCol))
}
sheet.merge(
1, //first row (0-based)
1, //last row (0-based)
1, //first column (0-based)
2 //last column (0-based)
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
1, //last row (0-based)
1, //first column (0-based)
2 //last column (0-based)
));
@ciferkey
ciferkey / DifferentCellTypes.kt
Last active October 15, 2018 00:16
Different Cell Types Example in Kotlin
val wb = workbook {
sheet("new sheet") {
row(2) {
cell(1.1)
cell(date1)
cell(calendar1)
cell("a string")
cell(true)
cell(5) {
cellType = ERROR
@ciferkey
ciferkey / DifferentCellTypes.java
Created October 13, 2018 19:37
POI Different Cell Types Examples in Java
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow(2);
row.createCell(0).setCellValue(1.1);
row.createCell(1).setCellValue(date1);
row.createCell(2).setCellValue(calendar1);
row.createCell(3).setCellValue("a string");
row.createCell(4).setCellValue(true);
row.createCell(5).setCellType(CellType.ERROR);
{
"title": "Introduction to Spring Reactor",
"thumbnailUrl": "/wp-content/themes/baeldung/icon/logo.svg",
"description": "The canonical reference for building a production grade API with Spring."
}
class UrlHandler(private val urlDecoder: URLDecoder) {
val charSet = StandardCharsets.UTF_8.toString()
val client = WebClient.create("")
fun unfurl(request: ServerRequest): Mono<ServerResponse> {
val url = URLDecoder.decode(request.pathVariable("url"), charSet)
val response = client.get().uri(url).retrieve().bodyToMono(String::class.java)
.map {
fun routes(urlHandler: UrlHandler) = router {
GET("/unfurl/{url}", urlHandler::unfurl)
}
val app = application {
bean<UrlHandler>()
bean<URLDecoder>()
logging {
level(LogLevel.INFO)
logback {
consoleAppender()
}
}
webflux {
fun CharIterator.readWhile(predicate: (Char) -> Boolean): Result<String, Exception> {
return Result.of<String, Exception> {
val readString = StringBuilder()
while (predicate.invoke(this.peek())) {
readString.append(this.next())
}
readString.toString()
}.mapError {
DecodeException("Failed reading based on given predicate", it)
}