NPOI操作

HSSFWorkbook workbook = new HSSFWorkbook();
ICellStyle cellStyle = workbook.CreateCellStyle();
//设置字体
 IFont font = workbook.CreateFont();
font.Color = HSSFColor.Red.Index;
cellStyle.SetFont(font);
//设置背景色
//cellStyle.FillForegroundColor = HSSFColor.Red.Index;
//cellStyle.FillPattern = FillPattern.SolidForeground;
ISheet sheet = workbook.CreateSheet("sheet1");
//设置标题行
IRow headRow = sheet.CreateRow(0);
 //创建单元格 
ICell headCell = headRow.CreateCell(i);  
//设置值
headCell.SetCellValue("姓名") 
//设置之前的样式
bodyRow.GetCell(i).CellStyle = cellStyle;i
string newPath = $"/ExcelReport/{year}-{month}/";
string filename = $"{year}-{month}.xls";
if (!Directory.Exists(HttpContext.Current.Server.MapPath(newPath)))
{
    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(newPath));
}
using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(newPath) + filename, FileMode.Create, FileAccess.Write))
{
    workbook.Write(fs);
}

写入文件流中并保存文件

猜你喜欢

转载自www.cnblogs.com/litianfeng-net/p/12112561.html