I have an API that I use to import an Excel spreadsheet into the system and it works correctly although it does not handle missing columns and I am not sure how and at what point I can do that.
I'll give an example first: I have an Excel spreadsheet (.xlsx) with 4 columns A, B, C, and D all containing text data, and what I would like to achieve is
- When the user import the file with A, B, and C (D has been removed), it is correctly updating the row and keep the value in column D untouched
Client side I have an API call that looks like this:
this.service.PostFormData<HttpEvent<any>>("v1/ui/importExcel", formData)
where formData is of type FormData (API FormData)
On the backend I do something like that:
HttpFileCollection files = HttpContext.Current.Request.Files;
foreach (string fileKey in files.AllKeys)
{
HttpPostedFile file = files[fileKey];
ISheet sheet = ImportHelper.GetImport(file);
}
public static ISheet GetImport(HttpPostedFile file)
{
ISheet sheet;
XSSFWorkbook workbook = new XSSFWorkbook(file.InputStream); <--- error
sheet = workbook.GetSheetAt(0);
return sheet;
}
As soon as it tries to convert the InputStream into a workbook it triggers an error (only when a column has been removed from the imported spreadsheet).
The code is within a try & catch (Exception e) although e comes back as blank.
Can you guys help out with that? Also, let me know if there is any better Exception type I can use to get a decent error message