这就是我想要达到的目标。
我正在创建一个asp.net MVC应用程序。我的限制是不能以编程方式将任何内容保存到服务器的文件结构中,因此不能将其保存为主机上的物理文件,然后抓取它供客户端下载。
我正在将PDF加载到流中,从PDF中提取信息,动态构建excel文件,然后将该文件提供给客户端。我的密码在下面。
// 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);
}当然,直到下面最后一节,一切都是完美的。但是,我不知道如何将工作簿加载到一个新的流中,然后返回该文件供下载。
// 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);我搜索并尝试了多个不同的变体,但是当应用程序命中返回File()时,它返回一个null。
我已经通过了执行,结果似乎在那里,但它没有通过任何东西。如能对这里的问题提供任何帮助,我们将不胜感激。
发布于 2021-05-27 04:16:12
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);必须将流位置重置为0。现在工作得很好。
https://stackoverflow.com/questions/67713934
复制相似问题