我的问题是我在数据库中有很多信息,理想情况下我想将其从我的客户端下载到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;
}
一起返回一个
FileResult
!谢谢,这工作得很好!还有一件事。当我下载这个文件时,它会以未知文件类型下载,而不是Excel文件。如果我点击“打开”,然后选择excel它打开罚款。无论如何,我可以把它作为一个直接的excel文件下载吗? – 109221793 2011-02-02 16:12:46