导出Excel

–注意一定要添加NPOI引用

1.创建Excel对象工作簿
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();

2.创建Excel对象工作表
NPOI.SS.UserModel.ISheet sheet = book.CreateSheet();

注:也可以修改工作表名称
book.SetSheetName(0, “学生信息表”);

3.设置标题
(1)创建行
IRow row0 = sheet.CreateRow(0);//创建行
row0.HeightInPoints = 35;//行高
(2)创建单元格
ICell cell0 = row0.CreateCell(0);//创建单元格
string strTitle = “学生信息表”;//标题
cell0.SetCellValue(strTitle);//赋值 SetCellValue

(3)创建一个格样式
ICellStyle cellStyle = workbook.CreateCellStyle();//声明并创建样式
cellStyle.Alignment = HorizontalAlignment.Center;//水平居中
cellStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中
IFont font = workbook.CreateFont();//声明字体
font.Color = NPOI.HSSF.Util.HSSFColor.Blue.Index;//设置字体颜色
font.Boldweight = (Int16)FontBoldWeight.Bold;//加粗
font.FontHeightInPoints = 18;//字体大小
cellStyle.SetFont(font);//设置单元格字体
(4)合并单元格
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 5));
//(第几行开始,到第几行结束,第几列开始,到第几列结束)
(5)设置单元格样式
cell0.CellStyle = cellStyle;

4.设置表头
(1)创建表头
NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(1);//给sheet添加表头
row1.Height = 22 * 20;//行高

(2)创建一行单元格,并设置值
row1.CreateCell(0).SetCellValue(“序号”);
row1.CreateCell(1).SetCellValue(“姓名”);
row1.CreateCell(2).SetCellValue(“性别”);
row1.CreateCell(4).SetCellValue(“年龄”);
row1.CreateCell(5).SetCellValue(“联系地址”);
row1.CreateCell(6).SetCellValue(“联系电话”);

(3)设置表头的样式
ICellStyle cellStyle_header = workbook.CreateCellStyle();//声明样式
cellStyle_header.Alignment = HorizontalAlignment.Center;//水平居中
cellStyle_header.VerticalAlignment = VerticalAlignment.Center;//垂直居中

IFont font_header = workbook.CreateFont();//声明字体
font_header.Boldweight = (Int16)FontBoldWeight.Bold;//加粗
font_header.FontHeightInPoints = 10;//字体大小

–设置背景颜色
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;

cellStyle_header.SetFont(font_header);//加入单元格

(4)循环,给每个单元格添加样式
for (int i = 0; i < row1.Cells.Count; i++)
{
row1.GetCell(i).CellStyle = cellStyle_header; //GetCell:获取单元格
}

5.导出Excel设置数据
(注:首先要先查询表格数据
var list = from tbTable in myModel.Table
orderby tbTable.TableID
Select tbTable ;
(1)设置单元格样式
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;

(2)循环,给导出的Excel设置数据
for (int i = 0; i < list.Count; i++)
{
NPOI.SS.UserModel.IRow row = sheet.CreateRow(i + 2);//给sheet添加一行
row.Height = 22 * 20;
row.CreateCell(0).SetCellValue(i + 1);//序号
row.CreateCell(1).SetCellValue(list[i].passengerName);
row.CreateCell(2).SetCellValue(list[i].passengerType);
row.CreateCell(3).SetCellValue(list[i].certificatesType);
row.CreateCell(4).SetCellValue(list[i].certificatesCode);
row.CreateCell(5).SetCellValue(list[i].contactName);
row.CreateCell(6).SetCellValue(list[i].contactPhone);

(2)给每个单元格添加样式
for (int j = 0; j < row.Cells.Count; j++)
{
row.GetCell(j).CellStyle = cellstyle_value;
}
}

6.设置列宽为自动适应
for (int i = 0; i < sheet.GetRow(0).Cells.Count; i++)
{
sheet.AutoSizeColumn(i);
sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) * 17 / 10);
}

7.输出的文件名称
string fileName = “学生信息表” + “.xls”;
(注:当前时间格式:DateTime.Now.ToString(“yyyy-MM-dd-HH-mm-ss-ffff”))

8.把Excel转化为文件流,输出
MemoryStream BookStream = new MemoryStream();//定义文件流
book.Write(BookStream);//将工作薄写入文件流
BookStream.Seek(0, SeekOrigin.Begin);
(注:输出之前调用Seek(偏移量,游标位置)方法:获取文件流的长度)

9.以文件形式返回
return File(BookStream, “application/vnd.ms-excel”, fileName);
(注:文件类型:“application/vnd.ms-excel”;文件名称:fileName)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值