asp.netexcelfiledownloadspire.xls

Spire.xls - return a File from a stream for client download


Here is what I'm trying to accomplish.

I am creating an asp.net MVC application. My restrictions are that I cannot programmatically save anything to the file structure of the server, so I can't save it as a physical file on the host, and then grab it for client download.

I am loading a PDF to a stream, extracting information from the PDF, dynamically building an excel file, and then offering the file for download to the client. My code is below.

    // Loads the incoming PDF document to stream
    PdfDocument doc = new PdfDocument();
    using (var stream = model.BudgetPdfFile.OpenReadStream())
    {
        doc.LoadFromStream(stream);
    }
    
    var pageCount = doc.Pages.Count;
    var date = DateTime.Now.ToShortDateString().Replace("/", "-");
    
    // Extracts data from the PDF and separates it by NewLine
    SimpleTextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
    StringBuilder allText = new StringBuilder();
    for (var i = 0; i < pageCount; i++)
    {
        allText.Append(doc.Pages[i].ExtractText(strategy));
    }
    var fullDocText = allText.ToString();
    List<string> linesList = new List<string>(fullDocText.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList());
    
    // generates a comparison list for output data manipulation from static data
    var finalList = linesList.BuildFinalList(budgetItems);
    
    // creates a new Spire.PDF.Workbook for the final output excel file
    var result = new Workbook();
    
    // checks for whether the submitted budget is for a case in litigation or not and builds the correct excel workbook
    if (model.isTrial)
    {
        result = ExportExcelBudget.TrialBudgetSheet(model, finalList);
    }
    else
    {
        result = ExportExcelBudget.PreTrialBudgetSheet(model, finalList);
    }

Absolutely everything up to the last section below works perfectly. However, I cannot figure out how to load the workbook into a new stream and then return the file for download.

    // saves the final workbook to a stream and offers it for download to the client 
    Stream outStream = new MemoryStream();
    var fileName = "Budget Report_" + model.ClaimNumber + "_" + date + ".xlsx";
    var contentType = "application/vnd.ms-excel";
    result.SaveToStream(outStream, Spire.Xls.FileFormat.Version2016);
    
    return File(outStream, contentType, fileName);

I've searched and tried multiple different variations but when the application hits the return File(), it returns a null.

I've stepped through execution and the results seem to be there, but it's not passing anything. Any help on what is wrong here would be greatly appreciated.


Solution

  •     Stream outStream = new MemoryStream();
        var fileName = "Budget Report_" + model.ClaimNumber + "_" + date + ".xlsx";
        var contentType = "application/vnd.ms-excel";
        result.SaveToStream(outStream, Spire.Xls.FileFormat.Version2016);
        **outStream.Position = 0;**
    
        return File(outStream, contentType, fileName);
    

    Had to reset the stream position to 0. Working perfectly now.