0

I am writing a routine to create and fill an Excel sheet in ASP.NET MVC. The object that I want to transfer to the sheet is a List, where T is a structure.

Do you know of any piece of code that does this?

The constructor that do this is:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
… 
 public byte[] ExcelContent;

 public ExportExcel(List<OrdViewModel> ol)
        {
            MemoryStream memstream = new MemoryStream();

            SpreadsheetDocument XlsxDoc = SpreadsheetDocument.Create(memstream, SpreadsheetDocumentType.Workbook);

            WorkbookPart workbookpart = XlsxDoc.AddWorkbookPart();
            workbookpart.Workbook = new Workbook();

            // Add a WorksheetPart to the WorkbookPart.  
            WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

            // Add Sheets to the Workbook.  
            Sheets sheets = XlsxDoc.WorkbookPart.Workbook.
                AppendChild<Sheets>(new Sheets());

            Sheet sheet = new Sheet()
            {
                Id = XlsxDoc.WorkbookPart.GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "Sheet1"
            };
            sheets.Append(sheet);

            // code here…

            XlsxDoc.Close();

            ExcelContent = memstream.ToArray();
}
4
  • 1
    What are you asking, exactly? Does this code not work for you? Does it throw an error? Does it not give you the desired output? Commented Jan 17, 2020 at 17:07
  • This code was taken from a site. Where I say // code here… is where I want to insert the code to fill the excel document. In this moment, I have no code where is the above tag... Commented Jan 17, 2020 at 17:35
  • So write it yourself? Stack Overflow isn't here to do that for you, we're here to assist with problems with code you've written. Commented Jan 17, 2020 at 20:50
  • Problem solved (almost…) in the question stackoverflow.com/questions/59825738/… Commented Jan 20, 2020 at 14:50

1 Answer 1

1

You can use my SwiftExcel library. It was designed to maximize performance and minimize memory usage when writing to Excel:

public ExportExcel(List<OrdViewModel> ol)
{
    using (var ew = new ExcelWriter("C:\\temp\\test.xlsx"))
    {
        for (var row = 1; row <= ol.Count; row++)
        {
            var item = ol[row-1];
            ew.Write(item.YourProperty1, 1, row);
            ew.Write(item.YourProperty2, 2, row);
            ew.Write(item.YourProperty3, 3, row);
            ew.Write(item.YourProperty4, 4, row);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.