Search code examples
javaexcelapache-poixlsx

After updating existing cell value in .xlsx file using POI, the file is giving HTTP 403 if used by an API


I have an excel file which contains many columns and one set of data row in it. After changing the value of the asOf field, as I am using it in API which is taking excel file, gives 403 Forbidden error. I have checked the following things:

  1. The field is updated correctly. (I checked the formatting and cell style of excel after updating it manually.)
  2. If I change the cell value manually then API is giving 200.

Image For Failure Response: When asOf field updated by Java Code

enter image description here

Image For Pass Response: When asOf field updated manually.

enter image description here

The image of field which I am updating is below:

enter image description here

Below is the java code that I am using:

   public static void excelFileReadAndUpdate() throws IOException {
    XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("./src/test/resources/testdata/HoldingDataWeekendDate.xlsx"));
    FileOutputStream fileOut = new FileOutputStream("./src/test/resources/testdata/HoldingDataWeekendDate.xlsx");
    XSSFSheet sheet1 = wb.getSheetAt(0);
    XSSFRow row = sheet1.getRow(1);
    XSSFCell cell = row.getCell(0);
    cell.setCellValue("2018-10-31");
    wb.write(fileOut);
    fileOut.close();
}

Note: I tried other approaches also like using the workbook and preserve the formatting but it is not working. If I update the field manually after opening the excel then it works.

Any suggestion/advice would be great help. Thanks in advance.


Solution

  • After searching and applying many approaches, finally, it has been resolved by applying some code giving read/write permission to that folder/file in java.

     public static void excelFileReadAndUpdate() throws IOException {
    
       final File file = new File("location of your excel file directory");
       file.setReadable(true, false);
       file.setExecutable(true, false);
       file.setWritable(true, false);
    
    XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("./src/test/resources/testdata/HoldingDataWeekendDate.xlsx"));
    FileOutputStream fileOut = new FileOutputStream("./src/test/resources/testdata/HoldingDataWeekendDate.xlsx");
    XSSFSheet sheet1 = wb.getSheetAt(0);
    XSSFRow row = sheet1.getRow(1);
    XSSFCell cell = row.getCell(0);
    cell.setCellValue("2018-10-31");
    wb.write(fileOut);
    fileOut.close();
    }