{"record":{"id":"050ccddc712a2419","repo":"pentaho/pentaho-kettle","slug":"excelinput-log-unsafefileexception-localized-message-e","errorCode":"ExcelInput.Log.UnsafeFileException","errorMessage":"ExcelInput.Log.UnsafeFileException (localized message) + e","messagePattern":"ExcelInput\\.Log\\.UnsafeFileException \\(localized message\\) \\+ e","errorType":"exception","errorClass":"KettleException","httpStatus":null,"severity":"error","filePath":"plugins/excel/core/src/main/java/org/pentaho/di/trans/steps/excelwriter/ExcelWriterStep.java","lineNumber":778,"sourceCode":"      // may have to write a header here\n      if ( meta.isHeaderEnabled() && !( !data.createNewSheet && meta.isAppendOmitHeader() && appendingToSheet ) ) {\n        writeHeader();\n      }\n\n      // If it's to use streaming, initialize it now as we already made all necessary initial calculations.\n      if ( data.wb instanceof XSSFWorkbook && meta.isStreamingData() ) {\n        data.innerSheet = Optional.of( data.sheet );\n        data.wb = new SXSSFWorkbook( (XSSFWorkbook) data.wb, STREAMING_WINDOW_SIZE );\n        data.sheet = data.wb.getSheet( data.realSheetname );\n      }\n\n      if ( log.isDebug() ) {\n        logDebug( BaseMessages.getString( PKG, \"ExcelWriterStep.Log.FileOpened\", buildFilename ) );\n      }\n      // this is the number of the new output file\n      data.splitnr++;\n    }  catch ( IOException | POIXMLException e ) {\n        throw new KettleException( BaseMessages.getString( PKG, \"ExcelInput.Log.UnsafeFileException\" ) +\n                e\n        );\n    }\n    catch ( Exception e ) {\n      logError( \"Error opening new file\", e );\n      setErrors( 1 );\n      throw new KettleException( e );\n    }\n  }\n\n  /** Sets data.startingRow, data.startingCol, data.posX, data.posY */\n  private void setSheetPosition( boolean appendingToSheet ) {\n    // starting cell support\n    if ( !Utils.isEmpty( data.realStartingCell ) ) {\n      CellReference cellRef = new CellReference( data.realStartingCell );\n      data.startingRow = cellRef.getRow();\n      data.startingCol = cellRef.getCol();\n    } else {","sourceCodeStart":760,"sourceCodeEnd":796,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/plugins/excel/core/src/main/java/org/pentaho/di/trans/steps/excelwriter/ExcelWriterStep.java#L760-L796","documentation":"prepareNextOutputFile() specifically catches IOException and POIXMLException when opening the new Excel output file and throws a KettleException containing the localized 'ExcelInput.Log.UnsafeFileException' message concatenated with the exception. This signals the output file could not be safely opened/written by POI.","triggerScenarios":"IOException creating the output stream or file; POIXMLException from POI when constructing the workbook (corrupt template, mismatched OLE2/OOXML content, zip errors in an xlsx).","commonSituations":"Appending to a file whose content is not a valid workbook (e.g. it is actually HTML or a renamed file); template copied in place is corrupt; output path is a directory or locked by Excel having the file open; extension says xlsx but content is xls.","solutions":["Ensure the target file is not open/locked in Excel or another process","If appending, confirm the existing file is a genuine workbook matching its extension","Delete/replace a corrupt output file and rerun","Check disk space and write permission on the output directory","Read the appended exception text in the message for the exact POI/IO cause"],"exampleFix":"// before: file renamed to .xlsx but actually old .xls content\n// after: re-save the file in the correct format (xlsx) or change the step's Extension setting to xls","handlingStrategy":"try-catch","validationCode":"File f = new File(filename);\nif (f.exists() && f.length() == 0) { /* safe to overwrite */ }\nelse if (f.exists()) {\n  try (InputStream in = new FileInputStream(f)) {\n    byte[] magic = new byte[4]; in.read(magic);\n    boolean isZip = (magic[0] == 'P' && magic[1] == 'K'); // xlsx\n    boolean isOle2 = (magic[0] == (byte)0xD0 && magic[1] == (byte)0xCF); // xls\n    // ensure magic matches the configured extension\n  }\n}","typeGuard":null,"tryCatchPattern":"try { writer.prepareNextOutputFile(); }\ncatch (KettleException e) {\n  if (e.getCause() instanceof IOException || e.getCause() instanceof POIXMLException) {\n    logError(\"Unsafe/locked/corrupt output file: \" + filename, e);\n  }\n  setErrors(1);\n  throw e;\n}","preventionTips":["Never append to files not created by this step","Keep extension and actual file format consistent","Ensure the file is not open in Excel during execution","Close stale handles (antivirus, prior runs) before rerunning"],"tags":["excel","poi","ioexception","file-open","kettle"],"backgroundTag":"file-open-failed","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}