下载一个excel文件

我的问题是我有一个数据库中的很多信息,理想情况下,我想把它从我的客户端下载到Excel文件。

我使用的是非常棒的NPOI库,已经在系统中的一个控制台应用程序中实现,但是这不是我写的。

目前发生什么事情,当我点击我的控制器的ActionLink时,一个空白的白色页面显示除了“System.IO.MemoryStream”之外什么都没有。

显然这不是预期的效果。 我想要的方式是当用户点击链接时,报告下载。

这是报告的课程:

public class RepairReporting { public Stream GenerateRepairFile(List<Int64> itemIds) { // Getting the complete workbook... // MemoryStream ms = new MemoryStream(); HSSFWorkbook templateWorkbook = new HSSFWorkbook(); // Create a worksheet by it's name. // HSSFSheet sheet = templateWorkbook.CreateSheet("Repairs Report"); sheet.ForceFormulaRecalculation = true; HSSFRow dataRow = sheet.CreateRow(0); HSSFCell cell = dataRow.CreateCell(0); cell.SetCellValue("Repairs"); cell = dataRow.CreateCell(1); cell.SetCellValue(DateTime.Now); // Build the header row // dataRow = sheet.CreateRow(1); string[] colHeaders = new string[]{ "Product Code", "Product Name", "Customer", "Date Submitted For Repair", "Date Sent For Repair", "Expected Release Date", "Estimated Cost", "Actual Cost", "Total Repair Price (END PRICE)" }; int colPosition = 0; // Write all the headers out. // foreach (string colHeader in colHeaders) { cell = dataRow.CreateCell(colPosition++); cell.SetCellValue(colHeader); } // Build the item rows. // int row = 2; foreach (Int64 itemId in itemIds) { using (ModelContainer ctn = new ModelContainer()) { Item currentItem = (from t in ctn.Items where t.ItemID == itemId && t.RepairSelection == true select t).First(); dataRow = sheet.CreateRow(row++); colPosition = 0; cell = dataRow.CreateCell(colPosition++); cell.SetCellValue(currentItem.ProductCode); cell = dataRow.CreateCell(colPosition++); cell.SetCellValue(currentItem.Product); cell = dataRow.CreateCell(colPosition++); cell.SetCellValue(currentItem.Customer.Name); cell.SetCellValue(currentItem.Repair.SubmissionDate.Value.ToString("MM/dd/yyyy")); if (currentItem.Repair.SentForConversion != null) { cell = dataRow.CreateCell(colPosition++); cell.SetCellValue(currentItem.Repair.SentForRepair.Value.ToString("MM/dd/yyyy")); } else { colPosition++; colPosition++; } if (currentItem.Repair.ReleaseDate != null) { cell = dataRow.CreateCell(colPosition++); cell.SetCellValue(currentItem.Repair.ReleaseDate.Value.ToString("MM/dd/yyyy")); } else { colPosition++; colPosition++; } if (currentItem.Repair.CostEstimation != null) { cell = dataRow.CreateCell(colPosition++); cell.SetCellValue(currentItem.Repair.CostEstimation.Value.ToString()); } else { colPosition++; colPosition++; } if (currentItem.Repair.ActualCost != null) { cell = dataRow.CreateCell(colPosition++); cell.SetCellValue(currentItem.Repair.ActualCost.Value.ToString()); } else { colPosition++; colPosition++; } if (currentTitle.Repair.TotalRepairPrice != null) { cell = dataRow.CreateCell(colPosition++); cell.SetCellValue(currentItem.Repair.TotalRepairPrice.Value.ToString()); } else { colPosition++; colPosition++; } } } templateWorkbook.Write(ms); ms.Position = 0; return ms; } } } 

然后这里是我的控制器,我认为这是我的问题所在:

  public Stream repairReport() { ModelContainer ctn = new ModelContainer(); List<Title> items = null; var itemObjects = ctn.Items.Where(t => t.RepairSelection == true) .Select(t =>t); items = itemObjects.ToList(); RepairReporting rtp = new RepairReporting(); List<long> itemIDs = items.Select(t => t.ItemID).ToList(); Stream repairReport = rtp.GenerateRepairFile(itemIDs); return repairReport; } 

如果这是您的Controller中的操作方法,那么您可以通过返回一个FileStreamResult来返回FileResult ,该FileStreamResult将构造函数中的streamContentType

 public FileResult RepairReport() { ModelContainer ctn = new ModelContainer(); List<Title> items = ctn.Items.Where(t => t.RepairSelection == true) .Select(t =>t).ToList(); RepairReporting rtp = new RepairReporting(); List<long> itemIDs = items.Select(t => t.ItemID).ToList(); Stream repairReport = rtp.GenerateRepairFile(itemIDs); return new FileStreamResult(repairReport, "application/ms-excel") { FileDownloadName = "RepairReport.xls", }; } 

2关注

  1. 太多的行可能会导致内存问题。
  2. 当你声明新的variables,如dataRow.CreateCell。 因为你调用COM互操作,试图处理你使用的每个对象。 obj.Dispose(); 和Marshal.Release(obj); 我不认为NPOIpipe理。