views:

222

answers:

1

Several people have asked, in a roundabout way, but I have yet to see a workable solution. Is there any way to open an excel file directly from memory (like a byte[]) ? Likewise is there a way to write a file directly to memory? I am looking for solutions that will not involve the hard disk or juggling temporary files. Thanks in advance for any suggestions.

A: 

Excel does not support saving to memory, at least not that I have been able to find - and I have looked because I could use it.

SpreadsheetGear for .NET can save to and load from a byte array. Here is a simple example:

using System;
using SpreadsheetGear;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a simple Hello World workbook.
            IWorkbook workbook = Factory.GetWorkbook();
            IWorksheet worksheet = workbook.Worksheets[0];
            worksheet.Cells["A1"].Value = "Hello World";
            // Save to memory.
            byte[] data = workbook.SaveToMemory(FileFormat.OpenXMLWorkbook);
            // Load from memory and output the contents of A1.
            workbook = Factory.GetWorkbookSet().Workbooks.OpenFromMemory(data);
            worksheet = workbook.Worksheets[0];
            Console.WriteLine("A1={0}", worksheet.Cells["A1"].Value);
        }
    }
}

You can see live SpreadsheetGear samples here and download the free trial here if you want to try it yourself.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson