Document corruption after changing it

Hi Aspose team,

in my scenario I need to open Excel documents and to make changes in them.
In attach project I am using FileStream to read document, I'll make hyperlink change and then using FileStream save changed document.

Now when I try to open document I am notified by Excel "We found a problem in some content...".

When I open document using only path variable this works ok but unfortunately I need to use FileStream.

Can you please have urgently look?

Thanks,
Oliver

Hi Oliver,


Thank you for providing the test samples.

I have checked this case against the latest version of Aspose.Cells for .NET 8.6.1 with following piece of code without observing the said issue, that is; the resultant spreadsheet can be loaded fine in Excel application.

C#

string OldURL;
string NewURL;

string dataDir = “D:/Nouveau Feuille de calcul Microsoft Excel.xlsx”;
System.IO.FileStream fs = System.IO.File.Open(dataDir, System.IO.FileMode.OpenOrCreate);
Workbook workbook = new Workbook(fs);
foreach (Worksheet worksheet in workbook.Worksheets)
{
foreach (Hyperlink hpl in worksheet.Hyperlinks)
{
OldURL = hpl.Address;
NewURL = OldURL.Replace(“TEST”, “_ASPOSE”);
hpl.Address = NewURL;
}
}
fs.Dispose();
fs = null;

Aspose.Cells.SaveFormat savFormat = SaveFormat.Excel97To2003;
String extension;
extension = System.IO.Path.GetExtension(dataDir + “D:/Nouveau Feuille de calcul Microsoft Excel.xlsx”);
switch (extension.ToLower())
{
case “.xls”:
savFormat = Aspose.Cells.SaveFormat.Excel97To2003;
break;
case “.xlam”:
savFormat = SaveFormat.Xlam;
break;
case “.xlsb”:
savFormat = SaveFormat.Xlsb;
break;
case “.xlsm”:
savFormat = SaveFormat.Xlsm;
break;
case “.xlsx”:
savFormat = SaveFormat.Xlsx;
break;
case “.xltm”:
savFormat = SaveFormat.Xltm;
break;
case “.xltx”:
savFormat = SaveFormat.Xltx;
break;
}

System.IO.FileStream fs2 = System.IO.File.Open(“D:/output” + extension, System.IO.FileMode.OpenOrCreate);
workbook.Save(fs2, savFormat);
fs2.Dispose();
fs2 = null;

Hi,

the way how you are doing will work.
Try to overwrite existing document Nouveau Feuille de calcul Microsoft Excel.xlsx.
This will corrupt it.

When you create new document everything is fine but my scenario is that I need to open document (D:/Nouveau Feuille de calcul Microsoft Excel.xlsx), make document change (hyperlink address in this case) and save changes to same D:/Nouveau Feuille de calcul Microsoft Excel.xlsx document.

BR,
Oliver
Try this code:

string OldURL;
string NewURL;

string dataDir = "D:/Nouveau Feuille de calcul Microsoft Excel.xlsx";
System.IO.FileStream fs = System.IO.File.Open(dataDir, System.IO.FileMode.OpenOrCreate);
Workbook workbook = new Workbook(fs);
foreach (Worksheet worksheet in workbook.Worksheets)
{
foreach (Hyperlink hpl in worksheet.Hyperlinks)
{
OldURL = hpl.Address;
NewURL = OldURL.Replace("TEST", "_ASPOSE");
hpl.Address = NewURL;
}
}
fs.Dispose();
fs = null;

Aspose.Cells.SaveFormat savFormat = SaveFormat.Excel97To2003;
String extension;
extension = System.IO.Path.GetExtension(dataDir + "D:/Nouveau Feuille de calcul Microsoft Excel.xlsx");
switch (extension.ToLower())
{
case ".xls":
savFormat = Aspose.Cells.SaveFormat.Excel97To2003;
break;
case ".xlam":
savFormat = SaveFormat.Xlam;
break;
case ".xlsb":
savFormat = SaveFormat.Xlsb;
break;
case ".xlsm":
savFormat = SaveFormat.Xlsm;
break;
case ".xlsx":
savFormat = SaveFormat.Xlsx;
break;
case ".xltm":
savFormat = SaveFormat.Xltm;
break;
case ".xltx":
savFormat = SaveFormat.Xltx;
break;
}
System.IO.FileStream fs2 = System.IO.File.Open(dataDir , System.IO.FileMode.OpenOrCreate);
workbook.Save(fs2, savFormat);
fs2.Dispose();
fs2 = null;

One more comment - I tried similar approach also for Word, Powerpoint, Visio, OneNote, PDF and Project documents and only Excel documents are corrupted :frowning:

Hi,

Thanks for your nice sample code and using Aspose.Cells.

We were able to replicate this issue with your sample code. However, we were able to rectify it by using the FileStream.SetLength() method.

Please check the rectified code. We have highlighted the changes in red for your convenience. Have a good day. :slight_smile:

C#
string OldURL;
string NewURL;

string dataDir = “F:/Shak-Data-RW/Downloads/Nouveau Feuille de calcul Microsoft Excel.xlsx”;
System.IO.FileStream fs = System.IO.File.Open(dataDir, System.IO.FileMode.OpenOrCreate);
Workbook workbook = new Workbook(fs);
foreach (Worksheet worksheet in workbook.Worksheets)
{
foreach (Hyperlink hpl in worksheet.Hyperlinks)
{
OldURL = hpl.Address;
NewURL = OldURL.Replace(“TEST”, “_ASPOSE”);
hpl.Address = NewURL;
}
}
fs.Dispose();
fs = null;

Aspose.Cells.SaveFormat savFormat = SaveFormat.Excel97To2003;
String extension;
extension = System.IO.Path.GetExtension(dataDir + “D:/Nouveau Feuille de calcul Microsoft Excel.xlsx”);
switch (extension.ToLower())
{
case “.xls”:
savFormat = Aspose.Cells.SaveFormat.Excel97To2003;
break;
case “.xlam”:
savFormat = SaveFormat.Xlam;
break;
case “.xlsb”:
savFormat = SaveFormat.Xlsb;
break;
case “.xlsm”:
savFormat = SaveFormat.Xlsm;
break;
case “.xlsx”:
savFormat = SaveFormat.Xlsx;
break;
case “.xltm”:
savFormat = SaveFormat.Xltm;
break;
case “.xltx”:
savFormat = SaveFormat.Xltx;
break;
}
System.IO.FileStream fs2 = System.IO.File.Open(dataDir, System.IO.FileMode.OpenOrCreate);
fs2.SetLength(0);
workbook.Save(fs2, savFormat);
fs2.Dispose();
fs2 = null;

Hi Shakeel,

that helped.
Does it make sense to apply this always when using FileStream and Aspose components (other ones)?

Thanks,
Oliver

Hi Oliver,

Thanks for your feedback and using Aspose.Cells.

I think, this is only the problem with Aspose.Cells as you have mentioned that other Aspose components are working fine without setting SetLength. So you will have to deal with as I have shown. Some other users were having this problem and we proposed the same solution and it worked for them too. Could you please use SetLength for this purpose for the time being? You only need to use it when you are using FileStream and you are changing the same source file. If you want us to log this as an issue in the database, then please provide us a runnable console application using Aspose.Words (or any other component) where it is working fine and Aspose.Cells where it is not working so that we could present this issue to product team for a fix. Thanks for your favor and cooperation in this regard.

Hi Shakeel,

I can live with workaround but this should be fixed also for Excel.

Can you create issue based on attached project? Basically I guess that you already have examples for other components as this is only open, make change, save document but as no issue will happen what value will it bring to have it, right :-)?

Thanks,
Oliver

Hi Oliver,

Thanks for your posting, sharing your good insight and using Aspose.Cells.

We will investigate this issue further with other Aspose components like Aspose.Words and then create an issue for product team to fix this issue. Please spare us some time. Once, we will have some update for you, we will let you know asap.

Hi Oliver,

Thanks for using Aspose.Cells.

I have attached the simple console application that replicates your issue. I found, when license is not set, the issue does not occur, it occurs only when the license is set. I have also attached the copy of sample excel file which is same as yours. I tried to add Aspose.Words code, but I found issue is not replicable with Aspose.Cells with simple lines of code like load or save. So there must be some additional code in Aspose.Words manipulating the word document just like you are doing with Aspose.Cells and justify this case.

Please download it and add few lines of Aspose.Words (or any other API) code that manipulates the document but works fine inside it so that we could submit this application to product team and they could fix the issue. Thanks for your cooperation