Excel Spreadsheet getting corrupted after saving

Hi Team,
I have one Excel which have multiple worksheets and when I saved it using aspose then sometime its getting corrupted and at opening time its ask for repair option.
After repair its works.For that I use below code snippest and I also attached excel file.

Aspose cell Information
DLL version 22.12.0.0
Order Id : 230204065922

Here is my code snippest:-

public HttpResponseMessage SaveExcelOnBlob(OfficeData data)
   {
       string docName = "";
       try
       {
           if (!string.IsNullOrEmpty(data.DocumentId) && !string.IsNullOrEmpty(data.ProjectId))
           {
               DMApi.Helper.DMHelper dmHelper = new Helper.DMHelper();
               Document docdata = _docrepo.Find(c => c.DocumentId == data.DocumentId);
               if (docdata != null)
               {
                   var prjdata = _projectrepo.Find(c => c.ProjectId == data.ProjectId);
                   docName = Path.GetFileName(docdata.PhysicalLocalFilePath);
                   var baseuri = ConfigurationManager.AppSettings["BASE_URI"];
                   string Path1 = baseuri + "cc" + docdata.CompanyID + "\\officeexcel\\"; //Blob Path
                   var excelData = _excellockrepo.FindAll(c => c.DocumentId == docdata.DocumentId).ToList();
                   lock (excelData)
                   {
                       if (excelData.Count > 0)
                       {
                            string[] schedule = Directory.GetFiles(Path1, docName); // Excel file path.
                           var lockSheets = excelData.FindAll(c => !string.IsNullOrEmpty(c.LockByUser) && c.LockByUser.ToLower() == data.UserId.ToLower() && c.Status == ShtStatus.Lock).ToList();
                           if (lockSheets.Count > 0)
                           {
                                Aspose.Cells.Workbook wtemp = null;
                               using (MemoryStream ms = new MemoryStream())
                               {
                                   ms.Write(data.bytes, 0, data.bytes.Length);
                                   ms.Position = 0;
                                   wtemp = new Aspose.Cells.Workbook(ms);
                               }
                               Aspose.Cells.Workbook wblob = new Aspose.Cells.Workbook(schedule[0]);
                               foreach (var item in lockSheets)
                               {
                                   try
                                   {
                                       Aspose.Cells.Worksheet sheetTemp = wtemp.Worksheets[item.SheetName];
                                       if (sheetTemp != null)
                                       {
                                           if (sheetTemp.IsProtected) {   sheetTemp.Unprotect(dmHelper.Encrypt(dmHelper.CreateShtPswrd(docdata.DocumentId, data.UserId)));  }
                                           else
                                           {
                                               sheetTemp.Unprotect(docdata.DocumentId);
                                           }
                                           Aspose.Cells.Worksheet sheetBlob = wblob.Worksheets[item.SheetName];
                                           if (sheetBlob != null)
                                           {
                                               if (sheetBlob.IsProtected)
                                               {        sheetBlob.Unprotect(dmHelper.Encrypt(dmHelper.CreateShtPswrd(docdata.DocumentId, data.UserId)));
                                               }
                                               sheetBlob.Copy(sheetTemp);
                                               if (!sheetBlob.IsProtected)
                                               {
                                                   sheetBlob.Protect(Aspose.Cells.ProtectionType.All, dmHelper.Encrypt(dmHelper.CreateShtPswrd(docdata.DocumentId, data.UserId)), "");
                                               }  
                                           }
                                           else
                                           {
                                               int i = wblob.Worksheets.Add();
                                               Aspose.Cells.Worksheet worksheet = wblob.Worksheets[i];
                                               worksheet.Name = sheetTemp.Name;
                                               sheetBlob.Copy(sheetTemp);
                                               if (sheetTemp.IsProtected)
                                               {
                                                   sheetBlob.Protect(Aspose.Cells.ProtectionType.All, dmHelper.Encrypt(dmHelper.CreateShtPswrd(docdata.DocumentId, data.UserId)), "");
                                               }
                                           }
                                       }
                                   }
                                   catch (Exception ex)
                                   {
                                       Console.WriteLine(ex.Message);
                                   }
                               }                                 
                               docdata.LastModified = DateTime.UtcNow;
                               _docrepo.Update(docdata);
                               wblob.Save(Path1 + "\\" + docName);
                               return new HttpResponseMessage()
                               {
                                  Content = new StringContent(
                                   status.ToString(),
                                   System.Text.Encoding.UTF8,
                                   "text/html"
                                    )
                               };
                           }
                       }
                   }
               }
           }
       }
       catch (Exception ex)
       {
           Console.WriteLine(ex.Message);
       }
       return new HttpResponseMessage()
       {
           Content = new StringContent(
        "FileNotUploaded",
        System.Text.Encoding.UTF8,
        "text/html"
    )
       };
   }

This is my code where I Also unlocking sheet for that I copy sheetTemp put it in one by one . As per my observation after that copy function my file getting corrupted.
If I saved Excel byte directly on my blob then its save properly.

  Aspose.Cells.Workbook wtemp = null;
                               using (MemoryStream ms = new MemoryStream())
                               {
                                   ms.Write(data.bytes, 0, data.bytes.Length);
                                   ms.Position = 0;
                                   wtemp = new Aspose.Cells.Workbook(ms);
                               }
wblob = wtemp;
 wblob.Save(Path1 + "\\" + "TempFile.xlsx");

Excel Sheet
ExcelFile.zip (6.9 MB)

In this I have One Source file and courrpted file

Because of this our application getting affected so please provide solution for this.
Thanks.

@Jayshiv,
Through testing, we can obtain the correct results by directly copying the workbook. Your sample code contains many interfaces that cannot be called. Would you like to provide a running Console project? This is very helpful for us to locate the issue. We will check it soon.

@Jayshiv
Through the following code, we can find that the result file crashed.

Workbook source = new Workbook(filePath + "SourceFile.xlsx");
Workbook dest = new Workbook();
foreach (Worksheet sheet in source.Worksheets)
{
    Worksheet tempSheet = dest.Worksheets.Add(sheet.Name);
    tempSheet.Copy(sheet);
}

dest.Save(filePath + "out.xlsx");

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

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.

@Jayshiv,

We have fixed your issue, but it’s better that you can add all worksheets which are exported to copy first, then copy it.
e.g.
Sample code:

Workbook workbook = new Workbook(dir + "SourceFile.xlsx");
Workbook n = new Workbook();
//int i = 0;
foreach (Worksheet sheet in workbook.Worksheets)
{
Worksheet s = n.Worksheets.Add(sheet.Name);
//s.Copy(sheet);

}
foreach (Worksheet sheet in workbook.Worksheets)
{
      Worksheet s = n.Worksheets[sheet.Name];
      CopyOptions copyOptions = new CopyOptions();
      copyOptions.ReferToSheetWithSameName = true;
      s.Copy(sheet, copyOptions);
}
n.Save(dir + "dest.xlsx"); 

Please note, the fix will be included in the planned release version (Aspose.Cells v23.10) scheduled for release in first half of October 2023. We will let you know when the next release is published.

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