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:
Image For Failure Response: When asOf field updated by Java Code
Image For Pass Response: When asOf field updated manually.
The image of field which I am updating is below:
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.
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();
}