public void ExportProduct(List<Model> list)
{
HSSFWorkbook hssfWork = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)hssfWork.CreateSheet("sheet名称");
HSSFFont font = (HSSFFont)hssfWork.CreateFont();
font.FontHeightInPoints = 13;
HSSFFont fontB = (HSSFFont)hssfWork.CreateFont();
fontB.FontHeightInPoints = 16;
fontB.Boldweight = short.MaxValue;
#region 单元格居中样式 加粗
HSSFCellStyle styleCenter = (HSSFCellStyle)hssfWork.CreateCellStyle();
styleCenter.Alignment = HorizontalAlignment.Center;//居中
styleCenter.VerticalAlignment = VerticalAlignment.Center;//垂直居中
styleCenter.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
styleCenter.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
styleCenter.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
styleCenter.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
styleCenter.SetFont(fontB);
styleCenter.WrapText = true;
#endregion 单元格居中样式
#region 单元格居中样式 正常
HSSFCellStyle styleCenterNormal = (HSSFCellStyle)hssfWork.CreateCellStyle();
styleCenterNormal.Alignment = HorizontalAlignment.Center; //居中
styleCenterNormal.VerticalAlignment = VerticalAlignment.Center;//垂直居中
styleCenterNormal.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
styleCenterNormal.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
styleCenterNormal.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
styleCenterNormal.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
styleCenterNormal.SetFont(font);
styleCenterNormal.WrapText = true;
#endregion 单元格居中样式
#region 表头
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 7));
HSSFRow row = (HSSFRow)sheet.CreateRow(0);
row.Height = 30 * 20;
HSSFCell cell = (HSSFCell)row.CreateCell(0);
cell.CellStyle = styleCenter;
cell.SetCellValue("第一行表头");
HSSFRow DocReportRow = (HSSFRow)sheet.CreateRow(1);
HSSFCell[] gg = new HSSFCell[8];
gg[0] = (HSSFCell)DocReportRow.CreateCell(0);
gg[0].SetCellValue("序号");
gg[0].CellStyle = styleCenter;
sheet.SetColumnWidth(0, 100 * 20);
gg[1] = (HSSFCell)DocReportRow.CreateCell(1);
gg[1].SetCellValue("姓名");
gg[1].CellStyle = styleCenter;
sheet.SetColumnWidth(1, 356 *20);
gg[2] = (HSSFCell)DocReportRow.CreateCell(2);
gg[2].SetCellValue("身份证号");
gg[2].CellStyle = styleCenter;
sheet.SetColumnWidth(2, 356 * 30);
gg[3] = (HSSFCell)DocReportRow.CreateCell(3);
gg[3].SetCellValue("年龄");
gg[3].CellStyle = styleCenter;
sheet.SetColumnWidth(3, 100 * 20);
gg[4] = (HSSFCell)DocReportRow.CreateCell(4);
gg[4].SetCellValue("户籍地址");
gg[4].CellStyle = styleCenter;
sheet.SetColumnWidth(4, 356 * 40);
gg[5] = (HSSFCell)DocReportRow.CreateCell(5);
gg[5].SetCellValue("户口类型");
gg[5].CellStyle = styleCenter;
sheet.SetColumnWidth(5, 556 * 20);
gg[6] = (HSSFCell)DocReportRow.CreateCell(6);
gg[6].SetCellValue("性别");
gg[6].CellStyle = styleCenter;
sheet.SetColumnWidth(6, 556 * 20);
gg[7] = (HSSFCell)DocReportRow.CreateCell(7);
gg[7].SetCellValue("联系电话");
gg[7].CellStyle = styleCenter;
sheet.SetColumnWidth(7, 456 * 20);
DocReportRow.HeightInPoints = 20;
#endregion
int rowIndex = 2;
foreach (var item in list)
{
HSSFRow rowContent = (HSSFRow)sheet.CreateRow(rowIndex);
HSSFCell[] ggcell = new HSSFCell[8];
ggcell[0] = (HSSFCell)rowContent.CreateCell(0);
ggcell[0].SetCellValue((rowIndex-1).ToString());
ggcell[0].CellStyle = styleCenterNormal;
ggcell[1] = (HSSFCell)rowContent.CreateCell(1);
ggcell[1].SetCellValue(item.Name);
ggcell[1].CellStyle = styleCenterNormal;
ggcell[2] = (HSSFCell)rowContent.CreateCell(2);
ggcell[2].SetCellValue(item.CardID);
ggcell[2].CellStyle = styleCenterNormal;
ggcell[3] = (HSSFCell)rowContent.CreateCell(3);
ggcell[3].SetCellValue(item.Age);
ggcell[3].CellStyle = styleCenterNormal;
ggcell[4] = (HSSFCell)rowContent.CreateCell(4);
ggcell[4].SetCellValue(item.Address);
ggcell[4].CellStyle = styleCenterNormal;
ggcell[5] = (HSSFCell)rowContent.CreateCell(5);
ggcell[5].SetCellValue(item.AddressType);
ggcell[5].CellStyle = styleCenterNormal;
ggcell[6] = (HSSFCell)rowContent.CreateCell(6);
ggcell[6].SetCellValue(item.Sex);
ggcell[6].CellStyle = styleCenterNormal;
ggcell[7] = (HSSFCell)rowContent.CreateCell(7);
ggcell[7].SetCellValue(item.Telephone);
ggcell[7].CellStyle = styleCenterNormal;
rowIndex++;
}
//输出
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//不保存excel在服务器直接输出到客户端进行下载
hssfWork.Write(ms);
string filename = string.Format("attachment; filename={0}.xls", HttpUtility.UrlEncode("导出需要的文件名称") + DateTime.Now.ToString("yyyy-MM-dd"));
Response.AddHeader("Content-Disposition", filename);
Response.BinaryWrite(ms.ToArray());
hssfWork = null;
ms.Close();
ms.Dispose();
}
导出如下图: