Import Excel using OpenXML SDK in ASP.NET MVC

This is a good sample to uploading using ASP.NET In order to stream Excel you need few more steps.

var fileName = Path.GetFileName(file.FileName);



constint blockSize = 256;

int bytesNum;

byte[] buffer = newbyte[blockSize];

MemoryStream ms = newMemoryStream();


while ((bytesNum = file.InputStream.Read(buffer, 0, blockSize)) > 0)

  ms.Write(buffer, 0, bytesNum);


Stream ImportStream = ms;



The //file.SaveAs(@”C:\Temp\”+fileName); is remark. The reason is we do not want to save the file but rather open by directly open by Excel using stream. We first read the file.InputStream into MemoryStream the cast it to Stream.

The code below is the key part to import Excel. Do read the comment in between the code.

publicstaticvoid OpenAndAddToSpreadsheetStream(Stream stream)


  System.IO.Packaging.Package sp = System.IO.Packaging.Package.Open(stream, FileMode.Open, FileAccess.ReadWrite);

    // Open a SpreadsheetDocument based on a stream.

    //DocumentFormat.OpenXml.Packaging.SpreadsheetDocument spreadsheetDocument =

    //    DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(stream, true);

    DocumentFormat.OpenXml.Packaging.SpreadsheetDocument spreadsheetDocument =



    WorkbookPart wbPart = spreadsheetDocument.WorkbookPart;


    // Find the sheet with the supplied name, and then use that

    // Sheet object to retrieve a reference to the first worksheet.

    Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().

      Where(s => s.Name == “Sheet1”).FirstOrDefault();


    // Throw an exception if there is no sheet.

    if (theSheet == null)





    // Retrieve a reference to the worksheet part.

    WorksheetPart wsPart =



    // Use its Worksheet property to get a reference to the cell

    // whose address matches the address you supplied.

    Cell theCell = wsPart.Worksheet.Descendants<Cell>().

        Where(c => c.CellReference == “A1”).FirstOrDefault();



    string value;

    string value1;

    // If the cell does not exist, return an empty string.

    if (theCell != null)


    value = theCell.InnerText;

    value1 = theCell.CellValue.Text;

    //value1 = theCell.CellValue.ToString();

    // If the cell represents an integer number, you are done.

    // For dates, this code returns the serialized value that

    // represents the date. The code handles strings and

    // Booleans individually. For shared strings, the code

    // looks up the corresponding value in the shared string

    // table. For Booleans, the code converts the value into

    // the words TRUE or FALSE.

    if (theCell.DataType != null)


      switch (theCell.DataType.Value)




          // For shared strings, look up the value in the

          // shared strings table.

          var stringTable =




          // If the shared string table is missing, something

          // is wrong. Return the index that is in

          // the cell. Otherwise, look up the correct text in

          // the table.

          if (stringTable != null)


            value =







          switch (value)



              value = “FALSE”;



              value = “TRUE”;







    // Code removed here…



You can also download a single solution from The sample file name is FileUploadSampleModi.rar My MSN ID is


About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress ( till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net and tagged , . Bookmark the permalink.

One Response to Import Excel using OpenXML SDK in ASP.NET MVC

  1. js says:

    Untitled에서 이 항목을 퍼감.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s