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 をダウンロードするためのポップアップ ウィンドウを取得できませんでした。