前台:
<form id="fmexp" method="post" target="_blank">
</form>
function export() {
var $end = $("#endate").val();
$("#fmexp").form('submit', {
url: '@Url.Action("ApplyExp")/' + $end,
onSubmit: function () {
},
success: function (result) {
$("#exportdate").linkbutton('disable');
$end = "";
}
})
}
后台:
public ContentResult ApplyExp(string id) //这个值是路由配置里的值。
{
ApplylistBLL alb = new ApplylistBLL();
DataTable dt = alb.EndData(id);
dt.TableName = "培训汇总";
if (dt != null && dt.Rows.Count > 0)
{
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet = book.CreateSheet(dt.TableName);
NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);
// //标题头与列宽
for (int i = 0; i < dt.Columns.Count; i++)
{
row.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(i + 1);
for (int j = 0; j < dt.Columns.Count; j++)
{
row2.CreateCell(j).SetCellValue(Convert.ToString(dt.Rows[i][j]));
}
}
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
book.Write(ms);
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
Response.BinaryWrite(ms.ToArray());
book = null;
ms.Close();
ms.Dispose();
}
}
return Content("");
}
引用dll---------NPOI.dll, NPOI.OOXML.dll, NPOI.OpenXml4Net.dll , NPOI.OpenXmlFormats.dll
已上传至文件。