public FileResult DaochuExcel()
{
//创建Excel文件的对象
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
//添加一个sheet(一个book可以有多个sheet)
NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1")
//sheet1.Rows.RowHeight = 25;
//worksheet.Columns.ColumnWidth = 24; 设置行高宽度
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
row1.Height = 3 * 265;
IFont cfont = book.CreateFont();
cfont.FontName = "宋体";
cfont.FontHeight = 1 * 256;
row1.CreateCell(0).SetCellValue("标题");
sheet1.SetColumnWidth(0, 20*256);
//sheet1.VerticallyCenter = false;
//sheet1.HorizontallyCenter = false; 设置水平垂直居中
//row1.Cells[0].CellStyle.GetFont(book).FontName = "黑体"; //单独设置第一行第一个单元格的字体
//row1.Cells[0].CellStyle.GetFont(book).FontHeight = 2 * 256; //单独设置第一行
//获取list数据
List<table> ListInfo= ;
for (int i = 0; i < ListInfo.Count; i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
rowtemp.CreateCell(0).SetCellValue(ListInfo[i].columnName);
}
Response.ContentType = "application/vnd.ms-excel;charset=UTF-8";
// 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
ms.Flush();
ms.Position = 0;
return File(ms, "application/vnd.ms-excel", "ExcelName.xls");
}
打开App,阅读手记