0

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

  1. 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

4
  • What is the error? Commented Aug 5, 2021 at 13:11
  • And - you could try to copy the stream to a memory stream first instead of using it directly. HttpBased streams sometimes lacks a "seek" implementation, which can be required in some cases. Commented Aug 5, 2021 at 13:13
  • 1
    I don't get any useful error - as I mentioned I am using the general error type handle (Exception e) and 'e' comes back as '{""}'. I'll look into copying the stream first, before trying the conversion. Thanks Commented Aug 5, 2021 at 14:13
  • Good luck... It is a bit of a long shot though... Commented Aug 5, 2021 at 14:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.