我的代码,其中从DB重新获取的值集合以及在datatable中逐个存储的值。并将该dataTable加载到Excel的工作表中
var dataTable = new DataTable();
var sb = new StringBuilder();
var resultvalues=methodtogetvalues() ;
if (resultvalues!= null && resultvalues.Count > 0)
{
var icount = 1;
foreach (var values in resultvalues)
{
if (icount == 1)
{
sb.Append(values.Id);
icount += 1;
}
dataTable.Columns.Add("ID");
dataTable.Rows.Add(values.Id);
}
}
var firstName = context.Request.Params["FirstName"];
var lastName = context.Request.Params["LastName"];
var fileName = firstName+lastName+"_ProgramStatusHistory_"+DateTime.Now;
var tempText = Convert.ToString(sb);
var workBook = new ExpertXls.ExcelLib.ExcelWorkbook(ExpertXls.ExcelLib.ExcelWorkbookFormat.Xlsx_2007);
var accessedRangeStyle = workBook.Styles.AddStyle("ΑccessedRangeStyle");
accessedRangeStyle.Font.Size = 10;
accessedRangeStyle.Font.Bold = true;
accessedRangeStyle.Alignment.VerticalAlignment = ExpertXls.ExcelLib.ExcelCellVerticalAlignmentType.Center;
accessedRangeStyle.Alignment.HorizontalAlignment = ExpertXls.ExcelLib.ExcelCellHorizontalAlignmentType.Left;
workBook.Worksheets.AddWorksheet();
var workSheet = workBook.Worksheets[0];
workSheet.LoadDataTable(dataTable, 1, 1, true);
workSheet.AutofitColumns();
workBook.Worksheets.RemoveWorksheet("Sheet2");
workBook.Worksheets.RemoveWorksheet("Sheet3");
workBook.Worksheets.RemoveWorksheet("Sheet4");
workBook.LicenseKey = "gqmworCworOysrWitKyyorGzrLOwrLu7u7s=";
context.Response.Clear();
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
context.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
using (var MyMemoryStream = new System.IO.MemoryStream())
{
workBook.Save(MyMemoryStream);
MyMemoryStream.WriteTo(context.Response.OutputStream);
context.Response.Flush();
context.Response.End();
}
此代码只显示一个警告框,其中包含错误消息"错误"。
我不明白什么是错的。任何人都可以用正确的方式重定向我。
答案 0 :(得分:0)
我会将这行代码移到using语句之外:
context.Response.End();
还会显示你的阻止块。它不太可能仅仅给出“错误”的消息我建议在catch块中放置一个断点并查看错误对象。