public ActionResult ExportExcel()
{
var wk = new XSSFWorkbook();
string filename = "导出Excel表格测试.xlsx";//Excel表格名称
try
{
string strFileName = string.Empty;
ISheet sheet = wk.CreateSheet("Sheet页名称");//Sheet页名称
IRow rowHeader = sheet.CreateRow(0);//表头行
rowHeader.CreateCell(0, CellType.String).SetCellValue("批次");
rowHeader.CreateCell(1, CellType.String).SetCellValue("志愿序号");
rowHeader.CreateCell(2, CellType.String).SetCellValue("院校代号");
rowHeader.CreateCell(3, CellType.String).SetCellValue("专业代号");
rowHeader.CreateCell(4, CellType.String).SetCellValue("院校名称");
rowHeader.CreateCell(5, CellType.String).SetCellValue("专业名称");
for (int i = 0; i < 10; i++)
{
IRow row = sheet.CreateRow(i + 1);
row.CreateCell(0, CellType.String).SetCellValue("本科第一批");
row.CreateCell(1, CellType.String).SetCellValue(i+1);
row.CreateCell(2, CellType.String).SetCellValue("0001");
row.CreateCell(3, CellType.String).SetCellValue("10");
row.CreateCell(4, CellType.String).SetCellValue("清华大学");
row.CreateCell(5, CellType.String).SetCellValue("临床医学");
sheet.SetColumnWidth(4, 256 * 40);//设置第五列(院校名称)的宽度
sheet.SetColumnWidth(5, 256 * 100);//设置第六列(专业名称)的宽度
}
}
catch (Exception ex)
{
throw ex;
}
//发送到客户端
MemoryStream ms = new MemoryStream();
wk.Write(ms);
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)));
Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();
wk = null;
ms.Close();
ms.Dispose();
return null;
}