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.