Exception when saving an xlsx file

Hi There,
I am using Aspose.Cell version 26.1.0.
I am trying to save the attached file (xlsx is inside the zip file) using the following code:

var stream = new FileStream(
filePath == null ? Path.GetTempFileName() : filePath,
FileMode.Create,
FileAccess.ReadWrite,
FileShare.ReadWrite,
bufferSize: 4096,
FileOptions.DeleteOnClose
);
var saveFormat = FileFormatUtil.FileFormatToSaveFormat(workbook.FileFormat);
await workbook.SaveAsync(stream, saveFormat);

When I do so, I get the following exception:

Object reference not set to an instance of an object.
at Aspose.Cells.Workbook.Save(Stream stream, SaveOptions saveOptions)
at Aspose.Cells.Workbook.Save(Stream stream, SaveFormat saveFormat)
at Aspose.Cells.Workbook..()
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
— End of stack trace from previous location —
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)

Saving the same workbook as SaveFormat.Excel97To2003 succeeds, though I do not want to loose any information, and I do not want a general fallback in my code.

problematic_pivot.zip (64.7 KB)

Please advise,
Thanks Shlomi

@shlomi.z

You are getting a NullReferenceException when you call Workbook.SaveAsync on a FileStream that is created with FileOptions.DeleteOnClose. The same workbook can be saved as the older Excel 97‑2003 format, so the problem is specific to saving the XLSX format with that stream configuration.

What may be happening
SaveAsync writes to the stream asynchronously. If the stream is opened with DeleteOnClose, the underlying file can be closed or become unavailable while the asynchronous write is still in progress, which can cause the internal null‑reference crash you see. This behavior has been observed when combining DeleteOnClose with async save operations.

Work‑around

  1. Use a regular FileStream (no DeleteOnClose)

    using var stream = new FileStream(
        filePath ?? Path.GetTempFileName(),
        FileMode.Create,
        FileAccess.ReadWrite,
        FileShare.ReadWrite,
        bufferSize: 4096);   // no FileOptions.DeleteOnClose
    
    var saveFormat = FileFormatUtil.FileFormatToSaveFormat(workbook.FileFormat);
    await workbook.SaveAsync(stream, saveFormat);
    // the stream is disposed after the await completes
    
  2. Or save to a MemoryStream first, then write the bytes to a file that you delete manually:

    using var ms = new MemoryStream();
    var saveFormat = FileFormatUtil.FileFormatToSaveFormat(workbook.FileFormat);
    await workbook.SaveAsync(ms, saveFormat);
    ms.Position = 0;
    File.WriteAllBytes(targetPath, ms.ToArray());
    // delete the temporary file yourself if needed
    

Both approaches avoid the DeleteOnClose flag while still giving you a temporary file that can be removed after the save succeeds. The general “save to stream” pattern is described in the Aspose.Cells documentation [5] and the Workbook.Save API reference [6].

Next step
Please try one of the above alternatives and let us know whether the exception is resolved. If the problem persists, providing a minimal reproducible sample (e.g., code that creates the workbook and calls SaveAsync) will help us investigate further.

Hey,

  1. I tried using memory stream as a mediator. Did not work.
  2. Tried using Save instead of SaveAsync. Did not work.
  3. Tried creating the stream using:

return new FileStream(
filePath,
FileMode.Create,
FileAccess.ReadWrite,
FileShare.ReadWrite
);
Did not work.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-59823

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@shlomi.z,

We are pleased to inform you that your issue (Ticket ID: “CELLSNET-59823”) has been resolved. The fix will be included in the upcoming release (Aspose.Cells v26.3) that we plan to release in the first half of March 2026. You will be notified when the next version is released.