简体   繁体   中英

Read Excel file from memory stream in C# Console application

I am fetching an Excel file from ftp and getting that file in a memory stream. I have to read that file from memory stream. I tried through Excel Interop but it is not accepting memory stream as a parameter in

xlWorkBook = xlApp.Workbooks.Open(strm, 0, true, 5, "", "", true, 
   Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

According to system requirement that I cannot save that file temporary; because I am using Azure web jobs for Console application deployment. Is there any way to read file from memory stream or can I convert that memory stream into an array of string?

I can suggest you to use ExcelDataReader 3.1.0 to read data from an Excel file.
Now you can use that MemoryStream in ExcelReader like this:
Note that reader of old Excel files - .xls - is different form newer files - .xlsx -.

var excelReader = originalFileName.EndsWith(".xls")
                ? ExcelReaderFactory.CreateBinaryReader(stream)
                : ExcelReaderFactory.CreateOpenXmlReader(stream);

If you want to extract a string from your MemoryStream you can use a StreamReader :

var streamReader = new StreamReader(memoryStream);
var stringResult = streamReader.ReadToEnd();

If you want to work over a FileStream you can copy your MemoryStream to it like this:

memoryStream.CopyTo(fileStream);

Also EasyXLS accepts streams, including MemoryStream. I don't know if you need only data in cells from Excel, or other information, but the code bellow is only for data:

ExcelDocument excelWorkbook = new ExcelDocument();
DataSet ds = excelWorkbook.easy_ReadXLSActiveSheet_AsDataSet(memoryStream);

More details about reading Excels, you can find at this location: https://www.easyxls.com/manual/FAQ/read-excel-file-in-dot-net.html

There is no MS Office present in the Azure Webjob, so we cannot use Microsoft.Office.Interop Dll in the Azure Webjob. Please have a try to use DocumentFormat.OpenXml to do that. The following is the demo code from the official document . I also find another tutorials about how to Read and Write Microsoft Excel with Open XML SDK .

public static void OpenAndAddToSpreadsheetStream(Stream stream)
{
    // Open a SpreadsheetDocument based on a stream.
    SpreadsheetDocument spreadsheetDocument =
        SpreadsheetDocument.Open(stream, true);

    // Add a new worksheet.
    WorksheetPart newWorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart<WorksheetPart>();
    newWorksheetPart.Worksheet = new Worksheet(new SheetData());
    newWorksheetPart.Worksheet.Save();

    Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>();
    string relationshipId = spreadsheetDocument.WorkbookPart.GetIdOfPart(newWorksheetPart);

    // Get a unique ID for the new worksheet.
    uint sheetId = 1;
    if (sheets.Elements<Sheet>().Count() > 0)
    {
        sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
    }

    // Give the new worksheet a name.
    string sheetName = "Sheet" + sheetId;

    // Append the new worksheet and associate it with the workbook.
    Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
    sheets.Append(sheet);
    spreadsheetDocument.WorkbookPart.Workbook.Save();

    // Close the document handle.
    spreadsheetDocument.Close();

    // Caller must close the stream.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM