convert a Workbook to a Resource

Viewed 243

I want to convert a org.apache.poi.ss.usermodel.Workbook to a org.springframework.core.io.Resource. Currently, I do this like so

public Resource toResource(Workbook workbook) {
  var outputStream = new ByteArrayOutputStream()
  workbook.write(outputStream);
  return new ByteArrayResource(outputStream.toByteArray());
}

It seems like there ought to be a more straightforward way, that doesn't require the intermediate ByteArrayOutputStream. Can this code be simplified?

1 Answers

Workbook.write() requires a parameter of type OutputStream, so there is no way around using that ByteArrayOutputStream.

Related