Excel File of Type Xlsx. Cannot convert. The file is corrupt or damaged

Hi Team,
I am getting the error “Cannot convert. The file is corrupt or damaged.” when I try to convert my excel file (Xlsx) into the images.

The same file works fine when I try to read the file from local and convert it into images.
Sample Code:
using (Converter converter = new Converter(GetFileStream))
{
try
{
var info = converter.GetDocumentInfo();
PdfConvertOptions options = new PdfConvertOptions();
converter.Convert(“converted.pdf”, options);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
static Stream GetFileStream() => File.OpenRead(filepath);

Now, as per my business use case, I am reading the same file from SharePoint using SharePoint REST API and getting the file content in the form of “Stream”. Passing the Stream to Convert class. It fails with the error “Cannot convert. The file is corrupt or damaged.”

Sample Code:
var byteArray = response.Content.ReadAsByteArrayAsync().Result;
using (var ms = new MemoryStream(byteArray))
{
var streams = new Dictionary<object, MemoryStream>();
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
loadOptions.HideComments = true;
loadOptions.ShowHiddenSheets = false;
loadOptions.OnePagePerSheet = true;
loadOptions.Format = SpreadsheetFileType.Xlsx;

using (var converter = new Converter(() => ms, () => loadOptions))
{
    var options = new ImageConvertOptions
    {
        Format = ImageFileType.Png
    };
    converter.Convert(page => new MemoryStream(), (page, convertedStream, fileAnme) =>
    {
        var memoryStream = new MemoryStream();
        convertedStream.CopyTo(memoryStream);            
        streams.Add(page, memoryStream);
    }, options);
}

}

Note: The same code (without SpreadsheetLoadOptions) works fine for pptx,docx files reading from SharePoint.

Please help me with the excel file issue.

@kamal.guntuku

Could you please share the probelmatic/source XLSX as well? Also specify the GroupDocs.Conversion API version that you are using.

Thank you for the reply. Please find the details below.
GroupDocs.Conversion Version=22.12.0
The issue is occurring for any xlsx file.
I am not able to upload xlsx/png files. Getting some errors while uploading.
It is just a simple file(xlsx) with some text.

@kamal.guntuku

That is an expected behavior. PPTX and DOCX have their own loading options.

Please compress the XLSX in a ZIP format and upload here.

create an excel file with some simple text. issue occurring when I get the file content from SharePoint as a stream and pass it to Converter() as I showed in the code.

Note: The same process is working fine for Docx and PowerPoint files.sample_excel.zip (25.9 KB)

@kamal.guntuku

Thanks for the details. We are investigating this scenario. Your investigation ticket ID is CONVERSIONNET-5741.

@kamal.guntuku

There’s an update on CONVERSIONNET-5741. We couldn’t reproduce the issue at our end. Created a sample project using Customer.xlsx (that you shared) from SharePoint and converted it using the code snippet that you shared. Please check SharePointSample.zip (1.9 KB).
In order to reproduce this issue at our end, you have to provide a fully working sample, containing also the part with downloading from SharePoint. It seems there is something wrong with the SharePoint response, but we have to check it.
If you can’t provide SharePoint link, you can try to save the response to a file and then try to convert it. If the error is there, you have to provide the downloaded file instead.

Thank you for the update.
I am getting file content by calling SharePoint REST API. The same is working fine for DOCX and PPT, failing for Excel.
Please find the code: ( I cannot be able provide the exact SharePoint URL and other details due to confidentiality)

var siteUrl = configuration[“SharePoint:siteUrl”];
var username = configuration[“SharePoint:username”];
var password = configuration[“SharePoint:password”];
var securePassword = new SecureString();
password.ToCharArray().ToList().ForEach(c => securePassword.AppendChar©);
var credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(username, securePassword);
var handler = new HttpClientHandler();
handler.Credentials = credentials;
var uri = new Uri(siteUrl);
handler.CookieContainer.SetCookies(uri, credentials.GetAuthenticationCookie(uri));
var filePath = string.Format(@"/_api/Web/GetFileByServerRelativePath(decodedurl=’{0}’)/$value", sharepointPath);
using (var client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add(“Accept”, “application/json;odata=verbose”);
var response = client.GetAsync(siteUrl + filePath).Result;
response.EnsureSuccessStatusCode();
var byteArray = response.Content.ReadAsByteArrayAsync().Result;
using (var ms = new MemoryStream(byteArray))
{
var streams = new Dictionary<object, MemoryStream>();

    SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
    loadOptions.HideComments = true;
    loadOptions.ShowHiddenSheets = false;
    loadOptions.OnePagePerSheet = true;
    loadOptions.Format = SpreadsheetFileType.Xlsx;
    using (var converter = new Converter(() => ms))
    {           
        var options = new ImageConvertOptions
        {
            Format = ImageFileType.Png
        };
        converter.Convert(page => new MemoryStream(), (page, convertedStream, fileAnme) =>
        {
            var memoryStream = new MemoryStream();
            convertedStream.CopyTo(memoryStream);
            streams.Add(page, memoryStream);
        }, options);
    }
}

}

@kamal.guntuku

If you cannot provide the SharePoint link, please follow the above mentioned steps.
Please find attached the updated sample project SharePointSampleREST.zip (13.1 KB). It’s a fully working application, still cannot reproduce the issue.

Still getting the same issue “Cannot convert. The file is corrupt or damaged.”
Code:
var response = client.GetAsync(siteUrl + filePath).Result;
response.EnsureSuccessStatusCode();
var byteArray = await response.Content.ReadAsByteArrayAsync();
using (var ms = new MemoryStream(byteArray))
{
using (Stream streamToWriteTo = File.Open(pathfilenamewithextension, FileMode.Create))
{
await ms.CopyToAsync(streamToWriteTo);
}
}
using (FileStream stream = new FileStream(pathfilenamewithextension, FileMode.OpenOrCreate))
{
var streams = new Dictionary<object, MemoryStream>();
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
loadOptions.HideComments = true;
loadOptions.ShowHiddenSheets = false;
loadOptions.OnePagePerSheet = true;
loadOptions.Format = SpreadsheetFileType.Xlsx;

                    using (var converter = new Converter(() => stream, () => loadOptions))
                    {
                        
                        var options = new ImageConvertOptions
                        {
                            Format = ImageFileType.Png
                        };
                        **//Error in the below statement**
                        converter.Convert(page => new MemoryStream(), (page, convertedStream, fileAnme) =>
                        {

@kamal.guntuku

Could you please share a screencast/video explaining the steps to reproduce the issue. Since, we cannot reproduce it at our end. We already shared sample projects with you.