NPOI导出Excel中修改格式,传输图片

本文详细介绍了使用NPOI库在Excel中设置单元格背景色的两种方法,包括使用内置颜色和自定义RGB颜色,并提供了图片插入的具体步骤,包括图片位置与大小的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、使用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);

说明:

  1. public HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2,
  2. int row2);Creates a new client anchor and sets the top-left and bottom-right coordinates of the anchor.
  3. Parameters:
  4. dx1 - the x coordinate within the first cell.
  5. dy1 - the y coordinate within the first cell.
  6. dx2 - the x coordinate within the second cell.
  7. dy2 - the y coordinate within the second cell.

dx2:第二个单元格的开始X坐标

dy2:第二个单元格的开始y坐标

  1. col1 - the column (0 based); of the first cell.
  2. row1 - the row (0 based); of the first cell.
  3. col2 - the column (0 based); of the second cell.
  4. row2 - the row (0 based); of the second cell.
  5. col1 图片的左上角放在第几个列cell,
    row1 图片的左上角放在第几个行cell,

    col2 图片的右下角放在第几个列cell,
    row2 图片的右下角放在第几个行cell,
  6. 列宽
    sheet.setColumnWidth((short)column,(short)width);
    行高
    row.setHeight((short)height);
  7. 添加多个图片时:多个pic应该share同一个DrawingPatriarch在同一个sheet里面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值