Because the project requirement needs to import two images in the same cell, relying on the methods of the following two links
NPOI export multiple images to Excel
c# Use NPOI to Excel Application 3 (Insert Picture)
The main body of the code above is the same, but inserting multiple images needs to be in the same patriarch. The blog post mentioned above mentioned that for loop cannot be used, so I created two fixed image paths directly for the first time and found that the creation failed. Then I put it in the for loop and found that it can be created. Maybe it is a bug or my parameter assignment error.
This is the main code. I use a for loop to create. Creating together can refer to the content of blog 2. But also based on one Sheet can only have one patriarch, create a spare one on the loop
byte[] bytes = File.ReadAllBytes(rePath);
int pictureIdx = _workbook.AddPicture(bytes, PictureType.JPEG);
HSSFClientAnchor anchor = new HSSFClientAnchor(0 + m*500, 0, 500 + m*500 , 200, j, rowIndex, j, rowIndex);
HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
The functions of the following function HSSFClientAnchor are image anchor 1, image anchor 2, row/column of anchor 1, row/column of anchor 2.
Two anchors represent the top-left and bottom-right positions respectively, where x<1023, y<255. If you want two images to be arranged in the same Cell, you can divide eqally x = 1023/2, then it can fill the entire cell, but this effect ends naturally after stretching.
Cell surrounding fill lines:
colorStyle.BorderTop = BorderStyle.Thin;
colorStyle.BorderRight = BorderStyle.Thin;
colorStyle.BorderBottom = BorderStyle.Thin;
colorStyle.BorderLeft = BorderStyle.Thin;
Cell text center alignment vertical alignment and automatic line wrap
colorStyle.WrapText = true;
colorStyle.Alignment = HorizontalAlignment.Center;
colorStyle.VerticalAlignment = VerticalAlignment.Center;
Cell background color setting
colorStyle.FillPattern = FillPattern.SolidForeground;
colorStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;