这里是导出的Excel表格的操作,就不展示查询操作了
//把要查询的数据给放到List
List<BookVo> listBook = book.ToList();
//创建工作簿
IWorkbook workbook = new HSSFWorkbook();
//创建工作表
ISheet sheet = workbook.CreateSheet("工作表名称");
workbook.SetSheetName(0, "图书信息");
//创建标题(第一行)
IRow rowTitle = sheet.CreateRow(0);
rowTitle.HeightInPoints = 35;
//创建单元格(第一行)
ICell ce110 = rowTitle.CreateCell(0);
//单元格设置值(第一行)
ce110.SetCellValue("图书信息");
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 1));
ICellStyle cellStyle_Title = workbook.CreateCellStyle();
cellStyle_Title.Alignment = HorizontalAlignment.Center;
cellStyle_Title.VerticalAlignment = VerticalAlignment.Center;
IFont font_title = workbook.CreateFont();
font_title.Color = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
font_title.IsBold = true;//新版版本的字体加粗写法
cellStyle_Title.SetFont(font_title);
cellStyle_Title.BorderLeft = BorderStyle.Thin;
cellStyle_Title.BorderTop = BorderStyle.Thin;
cellStyle_Title.BorderRight = BorderStyle.Thin;
cellStyle_Title.BorderBottom = BorderStyle.Thin;
ce110.CellStyle = cellStyle_Title;
//创建表头行(第二行)
IRow rowheader = sheet.CreateRow(1);
rowheader.CreateCell(0).SetCellValue("序号");
rowheader.CreateCell(1).SetCellValue("图书名称");
//创建表头的样式
ICellStyle cellStyle_header = workbook.CreateCellStyle();//声明样式
cellStyle_header.Alignment = HorizontalAlignment.Center;//水平居中
cellStyle_header.VerticalAlignment = VerticalAlignment.Center;//垂直居中
cellStyle_header.FillPattern = FillPattern.SolidForeground;//设置背景填充
cellStyle_header.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Aqua.Index;//背景颜色
//设置边框线为实线
cellStyle_header.BorderLeft = BorderStyle.Thin;
cellStyle_header.BorderTop = BorderStyle.Thin;
cellStyle_header.BorderRight = BorderStyle.Thin;
cellStyle_header.BorderBottom = BorderStyle.Thin;
//设置字体
IFont font_header = workbook.CreateFont();//声明字体
font_header.Boldweight = (short)FontBoldWeight.Bold;//加粗
font_header.IsBold = true;
font_header.FontHeightInPoints = 10;//字体大小
cellStyle_header.SetFont(font_header);//加入单元格
// 给 rowHeader 单元格设置样式 循环
for (int i = 0; i < rowheader.Cells.Count; i++)
{
rowheader.GetCell(i).CellStyle = cellStyle_header;
}
//创建数据单元格的样式
ICellStyle cellStyle_value = workbook.CreateCellStyle();
cellStyle_value.Alignment = HorizontalAlignment.Center;//水平居中
cellStyle_value.VerticalAlignment = VerticalAlignment.Center;//垂直居中
//四周边框线
cellStyle_value.BorderLeft = BorderStyle.Thin;
cellStyle_value.BorderTop = BorderStyle.Thin;
cellStyle_value.BorderRight = BorderStyle.Thin;
cellStyle_value.BorderBottom = BorderStyle.Thin;
for (int i = 0; i < listBook.Count; i++)
{
IRow row = sheet.CreateRow(i + 2);
row.HeightInPoints = 22;
//创建列,并设置值
row.CreateCell(0).SetCellValue(i + 1);//序号
row.CreateCell(1).SetCellValue(listBook[i].bookName);
//给每个单元格添加样式
for (int j = 0; j < row.Cells.Count; j++)
{
row.GetCell(j).CellStyle = cellStyle_value;
}
}
//设置列宽为自动适应
for (int i = 0; i < sheet.GetRow(1).Cells.Count; i++)
{
sheet.AutoSizeColumn(i);//这列自动适应最的数据单元格的宽度
sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) * 12 / 10);
}
//把创建好的Excel输出到浏览器
string fileName = "图书信息" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
//把Excel转化为流输出
MemoryStream BookStream = new MemoryStream();
workbook.Write(BookStream);
BookStream.Seek(0, SeekOrigin.Begin);
return File(BookStream, "application/vnd.ms-excel", fileName);