直接走一個範例
using (var ms = new MemoryStream())
{
var mapper = new Npoi.Mapper.Mapper();
mapper.Put(你自己的東西, "Sheet名稱");
var sheet = mapper.Workbook.GetSheetAt(0);
var rowEnum = sheet.GetRowEnumerator();
while (rowEnum.MoveNext())
{
IRow row = (IRow)rowEnum.Current;
if (row.RowNum == 0)
{
continue;
}
XSSFFont boldFont = (XSSFFont)mapper.Workbook.CreateFont();
boldFont.Boldweight = (short)FontBoldWeight.Bold;
boldFont.Color = NPOI.HSSF.Util.HSSFColor.Red.Index;
boldFont.FontHeightInPoints = 11;
boldFont.FontName = "新細明體";
XSSFCellStyle boldStyle = (XSSFCellStyle)mapper.Workbook.CreateCellStyle();
boldStyle.SetFont(boldFont);
for (int i = 0; i < row.Cells.Count; i++)
{
ICell cell = row.Cells[i];
cell.CellStyle = boldStyle;
}
}
mapper.Save(ms);
string fileName = $"{你的檔名}.xlsx";
return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
}
NPOI中表單(Sheet)的列(Row)列舉器(Enumerator)使用
var rowEnum = sheet.GetRowEnumerator();
while (rowEnum.MoveNext())
{
IRow row = (IRow)rowEnum.Current;
...
}
參考資料