Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Wed May 26, 2021 11:05 pm

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 client system from the website. Any file transfer must be a 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.


Code: Select all
                    // 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 where I'm returning the file works perfectly. However, I cannot figure out how to load the workbook into a new stream and then return the file for download.


Code: Select all
                    // 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 line, it returns a null. I've stepped through execution and the results seem to be there, but it's not sending anything. Any help on what is wrong here would be greatly appreciated.

Here is the Github link for the full project if you would like to see the entire file . https://github.com/tjones14/BudgetPro

tjones1467
 
Posts: 4
Joined: Sat Nov 16, 2019 8:39 pm

Thu May 27, 2021 4:13 am

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. That's all it took. Working perfectly.

tjones1467
 
Posts: 4
Joined: Sat Nov 16, 2019 8:39 pm

Thu May 27, 2021 9:14 am

Glad to hear your issue has been solved.
If you encounter any issues related to our products in the future, please feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.XLS