0

I am trying to load a file stream into the Infragistics workbook via the Workbook.Load(Filestream stream) function. And i am doing this in a dot net core 3.1 application using the latest package version 20.1.123 of the Infragistics.Web.Documents.Excel nuget package. But i am unable to load .xlsx and .xlsm files into the workbook and i end up getting an exception --> Invalid or unrecognized file format. (Parameter 'stream').

var client = new SftpClient("ConnString");
client.Connect();
using (client)
{
if (client.Exists("filename.xlsm"))
{
var fileContents = client.OpenRead("filename.xlsm");
var workbook = Workbook.Load(fileContents);
}
}

1 Answers1

0

ArgumentException: Invalid or unrecognized file format. (Parameter 'stream')

Exception is from this line: var workbook = Workbook.Load(fileContents);

From the source code we can see,

public static Workbook Load(Stream stream, WorkbookLoadOptions loadOptions = null);

pblic SftpFileStream OpenRead(string path);

SftpFileStream from OpenRead() is not correspond to Stream Workbook.Load().


Solution : Using DownloadFile to get Steam. And it works for me.


Codes of Controllers:

    public string loadExcel()
    {
        StringBuilder sb = new StringBuilder();
        // Path to file on SFTP server
        string pathRemoteFile = "/";

        var client = new SftpClient("127.0.0.1", 23, "admin","123456");
        client.Connect();
        using (client)
        {

            if (client.Exists("filename.xlsm"))
            {
                //var fileContents = client.OpenRead("/filename.xlsm");
                var fileContents = new MemoryStream();
                client.DownloadFile("filename.xlsm", fileContents);

                var workbook = Workbook.Load(fileContents);

                var worksheet = workbook.Worksheets[0];
                sb.AppendLine(String.Format("{0} \t\t {1}",
                        worksheet.Rows[0].Cells[0].GetText(),
                        worksheet.Rows[0].Cells[1].GetText()));

            }
        }

        return sb.ToString();
    }

Test environment

SFTP

enter image description here

filename.xlsm

enter image description here

Screenshot of result

enter image description here

Michael Wang
  • 3,782
  • 1
  • 5
  • 15
  • 1
    Code-only answers are *not* good answers. The screenshots don't seem to be relevant and the code seems to be doing the same thing the OP does, among other, unrelated things. Assuming it works - after writing to a MemoryStream, the stream position is at the end of file. Trying to read from it will throw – Panagiotis Kanavos Aug 27 '20 at 08:05
  • Hi, @Panagiotis Kanavos, tks for your suggestion but that's not my completed answer when you comment. And I spent time and effort to test and answer op. If there is better solution, welcome anyone to post it. – Michael Wang Aug 27 '20 at 08:32