Saving an .XLSM creates a corrupted workbook. Aspose.Cells.DLL, v.24.2.0; .NET 4.8; Windows 64-bit

Aspose.Cells.DLL, v.24.2.0; .NET 4.8; Windows 64-bit, Office 32-bit

private void button1_Click(object sender, EventArgs e)
{
Aspose.Cells.License license = new Aspose.Cells.License();
license.SetLicense(“Aspose.Cells.NET.lic”);
FileStream sourceFileStream = null;
Workbook workbook = OpenWorkbook(out sourceFileStream);
ModifyWorkbook(workbook, sourceFileStream);
MessageBox.Show(“Done”);
}

public void ModifyWorkbook(Workbook workbook, FileStream sourceFileStream)
{
CloseSourceStream(sourceFileStream); sourceFileStream = null;
DeleteOutputFile();

OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.Auto);

if (Environment.Is64BitProcess)
    saveOptions.EnableZip64 = true;

workbook.Save(OUTPUT, saveOptions);

}
public Workbook OpenWorkbook(out FileStream fileStream)
{
string workbookPath = SOURCE;
fileStream = new FileStream(workbookPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);

LoadOptions lo = new LoadOptions(LoadFormat.Auto);
lo.MemorySetting = MemorySetting.MemoryPreference;
lo.LoadFilter = new LoadFilter(LoadDataFilterOptions.All);

return new Workbook(fileStream, lo);

}
private void DeleteOutputFile()
{
if (File.Exists(OUTPUT))
{
try { File.Delete(OUTPUT); }
catch (Exception) { }
}
}
private static void CloseSourceStream(FileStream fileStream)
{
if (fileStream != null) fileStream.Dispose();
}

@AndreiSmolin
Would you like to provide your sample file? We will check it soon.

This is a newly-created XLSM: https://temp.add-in-express.com/support/Book1.xlsm

@AndreiSmolin
By testing with the following sample code, we can obtain the correct file. Please refer to the attachment. out_net.zip (14.8 KB)

Please note that the following statement:

OoxmlSaveOptions saveOptions=new OoxmlSaveOptions (SaveFormat.Auto); 

The specified save format is xlsx. If you need to save as xlsm, please use the following statement.

OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.Xlsm);

The sample code as follows:

string filePath;
FileStream sourceFileStream = null;
Workbook workbook = OpenWorkbook(out sourceFileStream, filePath + "Book1.xlsm");
ModifyWorkbook(workbook, sourceFileStream, filePath);

public static void ModifyWorkbook(Workbook workbook, FileStream sourceFileStream, string filePath)
{
    CloseSourceStream(sourceFileStream); sourceFileStream = null;

    string OUTPUT = filePath + "out_net.xlsm";
    DeleteOutputFile(OUTPUT);

    OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.Xlsm);

    if (Environment.Is64BitProcess)
        saveOptions.EnableZip64 = true;

    workbook.Save(OUTPUT, saveOptions);
}
public static Workbook OpenWorkbook(out FileStream fileStream, string SOURCE)
{
    string workbookPath = SOURCE;
    fileStream = new FileStream(workbookPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);

    LoadOptions lo = new LoadOptions(LoadFormat.Auto);
    lo.MemorySetting = MemorySetting.MemoryPreference;
    lo.LoadFilter = new LoadFilter(LoadDataFilterOptions.All);

    return new Workbook(fileStream, lo);
}
private static void DeleteOutputFile(string OUTPUT)
{
    if (File.Exists(OUTPUT))
    {
        try { File.Delete(OUTPUT); }
        catch (Exception) { }
    }
}

private static void CloseSourceStream(FileStream fileStream)
{
    if (fileStream != null) fileStream.Dispose();
}

Hope helps a bit.

This contradicts to the description of the SaveFormat.Auto member: “If saving the file to the disk, the file format accords to the extension of the file name. If saving the file to the stream, the file format is xlsx.”

I save the file to disk and the file extension is .XLSM. Accordingly, I expect that the resulting file is a valid XLSM. Moreover, the code uses SaveFormat.Auto for quite a while and I see that 18.2.4 works exactly as described.

@AndreiSmolin
Thank you for your feedback. We also noticed the API instructions for SaveFormat.Auto. Through testing, we can reproduce the issue and found that when using SaveFormat.Auto, the result file crashes after resaving xlsm file.
The sample code as follows:

Workbook workbook = new Workbook(filePath + "Book1.xlsm");
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.Auto);
workbook.Save(filePath + "out_net.xlsm", saveOptions);

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-55218

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.

Thank you. Waiting for a fix.

@AndreiSmolin
You are welcome. Once there is an update, we will notify you immediately.

@AndreiSmolin,

This is to inform you that your issue (logged earlier as “CELLSNET-55218”) has been resolved. The fix/enhancement will be included in the next release (Aspose.Cells v24.3) scheduled for release this week or next week in March. You will be notified when the next version is published.

The issues you have found earlier (filed as CELLSNET-55218) have been fixed in this update. This message was posted using Bugs notification tool by johnson.shi