.hi, i am trying to create a temporary .xlsx file and write to it using apache poi. i am getting EmptyFileException for creating workbook. this is the code:
public class Writer{
File file;
public File Write(List<FormData> l, Class elementClass)
{
try {//create temp fiele here
file = File.createTempFile(elementClass.getSimpleName(),".xlsx");
} catch (IOException ex) {
Logger.getLogger(Writer.class.getName()).log(Level.SEVERE, null, ex);
}
XSSFWorkbook workbook;
XSSFSheet sheet;
if (file.exists()) {
FileInputStream inputStream;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException ex) {
throw new AspirinException(AspirinException.Type.INTERNAL_ERROR);
}
try {// this line gets error//
workbook = (XSSFWorkbook) WorkbookFactory.create(inputStream);
} catch (IOException | InvalidFormatException | EncryptedDocumentException ex) {
throw new AspirinException(AspirinException.Type.INTERNAL_ERROR);
}
//...
it works fine if i use a real file. but for temp file it doesn't work. please help me with this. thanks
file
be else than an empty file?WorkbookFactory.create(inputStream)
tries creating aWorkbook
object containing data read from theinputStream
. This fails if there is no data to read. – Axel Richter