Kindly Ignore the question: Stupid parsing error at my end.
I have an API which allows a file upload and reads it using Apache POI (or throw exception if the filetype is not .xlsx). However, it seems like WorkbookFactory.create(inputstream) is not generating the correct file. When I read the file in code I get null
values when reading cells where data should be present.
Code is as follows:
@POST
@Path("<path>")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response submitFile(@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail verticalId) {
try {
StringBuilder fileNameBuilder = new StringBuilder();
String[] names = fileDetail.getFileName().replace(" ", "_").split("\\.");
Contents contents = fileParser.parse(uploadedInputStream);
return Response.ok().build();
}
public class XlsxParser implements FileParser {
public Content parse(File file) throws IOException InvalidFileFormatException{
Content content = new Content();
Workbook file;
try {
designFile = WorkbookFactory.create(file);
} catch (InvalidFormatException exception) {
throw new InvalidFileFormatException("Invalid file format. Expected file type is .xlsx");
}
Sheet sheet = file.getSheetAt(0);
Iterator<Row> rowIterator = sheet.rowIterator();
Row row = rowIterator.next();
int cellCount = row.getPhysicalNumberOfCells();
return content;
}
}
Same code works fine if I first create a .xlsx
file from inputstream
and then pass a File
object to WorkbookFactory.create()
Anyone ever faced the same issue? Thanks for the help.