我正在使用 Asp Dot Net Mvc3 我想下载 Excel 文件。以下是我的控制器,
public ActionResult DownloadTRUnutilizedOwnershipInvoke(string strGeo, string strVertical, int intMonth, int intFlag,string strType)
{
TRUnutilizedOwnershipModel objTRUnutilizedOwnershipModel = new TRUnutilizedOwnershipModel();
objTRUnutilizedOwnershipModel = objTRUnutilizedOwnershipModel.GetUnutilizedOwnershipExcelEntities(strGeo, strVertical, intMonth, intFlag,strType);
objTRUnutilizedOwnershipModel.ExportUnutilizedOwnership("UnutilizedOwnership", objTRUnutilizedOwnershipModel.lstunutilizedownershipExcelentities);
}
以下是我的模型,
public void ExportUnutilizedOwnership<T>(string fileName, List<T> lstdata)
{
try
{
Table tableData = new Table(); HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName + ".xls"));.
HttpContext.Current.Response.ContentType = "application/ms-excel";= "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
if (typeof(T) == typeof(UnutilizedOwnershipExcelEntities))
{
List<UnutilizedOwnershipExcelEntities> lstNewdata = (List<UnutilizedOwnershipExcelEntities>)(object)lstdata;
tableData = TableUnutilizedOwnershipExcelData(lstNewdata);
} tableData.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
public Table TableUnutilizedOwnershipExcelData(List<UnutilizedOwnershipExcelEntities> lstUnutilizedOwnershipExcelEntities)
{
try
{
Table tableData = new Table();
TableRow rowHeader = new TableRow();
TableHeaderCell hCurrentOwnership = new TableHeaderCell();
TableHeaderCell hCount = new TableHeaderCell();
hCurrentOwnership.Text = "CurrentOwnership";
hCount.Text = "Count";
rowHeader.Cells.Add(hCurrentOwnership);
rowHeader.Cells.Add(hCount);
rowHeader.BackColor = Color.Gray;
ApplyStyle(rowHeader);
tableData.Rows.Add(rowHeader);
foreach (UnutilizedOwnershipExcelEntities excelEntity in lstUnutilizedOwnershipExcelEntities)
{
TableRow rowData = new TableRow();
TableCell cellCurrentOwnership = new TableCell();
TableCell cellCount = new TableCell();
cellCurrentOwnership.Text = excelEntity.CurrentOwnership;
cellCount.Text = Convert.ToString(excelEntity.cnt);
rowData.Cells.Add(cellCurrentOwnership);
rowData.Cells.Add(cellCount);
tableData.Rows.Add(rowData);
ApplyStyle(rowData);
}
return tableData;
}
catch (Exception ex)
{
throw ex;
}
}
public void ApplyStyle(TableRow row)
{
try
{
row.BorderColor = Color.FromName("#CCCCCC");
row.BorderWidth = Unit.Pixel(1);
row.BorderStyle = BorderStyle.Solid;
}
catch (Exception ex)
{
throw ex;
}
}
我无法获取用于下载 Excel 的弹出窗口。我是否错过了此代码中的任何内容以获取用于下载 Excel 弹出的弹出窗口