1.设置单元格上边框为黑色实线
ICellStyle cellStyleTail = sheet.Workbook.CreateCellStyle();
cellStyleTail.BorderTop = BorderStyle.Thin;
https://blog.csdn.net/ezreal_pan/article/details/81134701
2.插入图片方法
#region 插入图片
var path = AppDomain.CurrentDomain.BaseDirectory + "File/Images/logo1.png";
byte[] bytes = System.IO.File.ReadAllBytes(path);
int pictureIdx = workbook.AddPicture(bytes, PictureType.PNG);
IDrawing patriarch = sheet.CreateDrawingPatriarch();
//HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 100, 50, col, row, col + 1, row + 1);
//##处理照片位置,【图片左上角为(col, row)第row+1行col+1列,右下角为( col +1, row +1)第 col +1+1行row +1+1列,宽为100,高为50
//public XSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2);
IClientAnchor anchor = new XSSFClientAnchor(0, 0, 250, 167, 7, 0, 9, 0);
IPicture pict = (XSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
pict.Resize();
#endregion
3.合并单元格
// 指从0行到0行,从0列到第4列合并
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 4));
4.居中
ICellStyle cellStyleCenter = sheet.Workbook.CreateCellStyle();
cellStyleCenter.Alignment = HorizontalAlignment.Center;
5. 字体加粗、字号大小
//字体加粗
ICellStyle cellStyleTitle = sheet.Workbook.CreateCellStyle();
var font = workbook.CreateFont();
font.Boldweight = (short)FontBoldWeight.Bold;
font.FontHeightInPoints = 12;
//font.FontHeightInPoints = (short)8.5;字号为小数时要转为short
cellStyleTitle.SetFont(font);
6.将样式作用在哪个单元格中
//將樣式cellStyleTitle作用在行rowTitle2列Cells[0]上
rowTitle2.Cells[0].CellStyle = cellStyleTitle;
7.创建excel、样式、sheet、行等
//新建一个excel
XSSFWorkbook workbook = new XSSFWorkbook();
//excel样式
XSSFCellStyle style = (XSSFCellStyle)workbook.CreateCellStyle();
//创建一个sheet
ISheet sheet = workbook.CreateSheet(Table.TableName);
//创建一个样式
ICellStyle cellStyleTitle = sheet.Workbook.CreateCellStyle();
//创建一个行
NPOI.SS.UserModel.IRow rowTitle2 = sheet.CreateRow(0);
8.单元格赋值
rowTitle2.CreateCell(0).SetCellValue("STATEMENT OF ACCOUNT");
9. 给指定sheet的内容设置每列宽度
List<int> widthlist = new List<int>();
for (var i = 0; i < 9; i++)
{
widthlist.Add(3000);
}
int[] widths = widthlist.ToArray()
//给指定sheet的内容设置每列宽度(index从0开始,width1000相当于excel设置的列宽3.81)
for (int i = 0; i < widths.Length; i++)
{
sheet.SetColumnWidth(i, widths[i]);
}
10.设置无边距
//创建一个sheet
ISheet sheet = workbook.CreateSheet(dt.TableName);
sheet.SetMargin(MarginType.LeftMargin, 0);
sheet.SetMargin(MarginType.RightMargin, 0);
sheet.SetMargin(MarginType.BottomMargin, 0);
sheet.SetMargin(MarginType.TopMargin, 0);
sheet.FitToPage = false;
11.单元格背景颜色
ICellStyle cellStyleBold = sheet.Workbook.CreateCellStyle();
cellStyleBold.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index;
cellStyleBold.FillPattern = FillPattern.SolidForeground;
rowEnd3.Cells[0].CellStyle = cellStyleBold;
颜色对照表
https://www.cnblogs.com/byxxw/p/5265127.html