一、使用NPOI设置背景色
方法一:使用NOPI自带的颜色使用方法,缺点就是自带的样色种类少,不够用
HSSFWorkbook hssfworkbook = new HSSFWorkbook();//创建工作薄
styleCommonTextRedColor = hssfworkbook.CreateCellStyle();//创建单元格
//添加颜色
styleCommonTextRedColor.FillPattern = FillPattern.SolidForeground;
styleCommonTextRedColor.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PaleBlue.Index;
方法二:采用RGB颜色
HSSFWorkbook hssfworkbook = new HSSFWorkbook();//创建工作薄
styleCommonTextRedColor = hssfworkbook.CreateCellStyle();//创建单元格
//自定义颜色
HSSFPalette palette = hssfworkbook.GetCustomPalette();
palette.SetColorAtIndex(40, 250, 208, 121);//参数说明,第一个是指调色板的位置,后三个参数是RGB色值
HSSFColor hssfColorY = palette.FindColor(250, 208, 121);
styleColorRed.FillPattern = FillPattern.SolidForeground;
styleColorRed.FillForegroundColor = hssfColorR.Indexed;
图片传输:
byte[] bytes = System.IO.File.ReadAllBytes(@"....");
int pictureIdex = hssfworkbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.PNG);
HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 25, 158, 62, 0, 0, 0, 1);
HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdex);
说明:
- public HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2,
- int row2);Creates a new client anchor and sets the top-left and bottom-right coordinates of the anchor.
- Parameters:
- dx1 - the x coordinate within the first cell.
- dy1 - the y coordinate within the first cell.
- dx2 - the x coordinate within the second cell.
- dy2 - the y coordinate within the second cell.
dx2:第二个单元格的开始X坐标
dy2:第二个单元格的开始y坐标
- col1 - the column (0 based); of the first cell.
- row1 - the row (0 based); of the first cell.
- col2 - the column (0 based); of the second cell.
- row2 - the row (0 based); of the second cell.
- col1 图片的左上角放在第几个列cell,
row1 图片的左上角放在第几个行cell,
col2 图片的右下角放在第几个列cell,
row2 图片的右下角放在第几个行cell, - 列宽
sheet.setColumnWidth((short)column,(short)width);
行高
row.setHeight((short)height); - 添加多个图片时:多个pic应该share同一个DrawingPatriarch在同一个sheet里面。