NPOI随笔——图片在单元格等比缩放且居中显示
生活随笔
收集整理的這篇文章主要介紹了
NPOI随笔——图片在单元格等比缩放且居中显示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
NPOI導出的圖片默認是在單元格左上方,這使得圖片在單元格顯示得很難看。居中,且等比縮放,才是圖片在單元格上的完美展示。
? ? ? ?
/// <summary>/// 圖片在單元格等比縮放居中顯示/// </summary>/// <param name="cell">單元格</param>/// <param name="value">圖片二進制流</param>private void CellImage(ICell cell, byte[] value){if (value.Length == 0) return;//空圖片處理double scalx = 0;//x軸縮放比例double scaly = 0;//y軸縮放比例int Dx1 = 0;//圖片左邊相對excel格的位置(x偏移) 范圍值為:0~1023,超過1023就到右側相鄰的單元格里了int Dy1 = 0;//圖片上方相對excel格的位置(y偏移) 范圍值為:0~256,超過256就到下方的單元格里了bool bOriginalSize = false;//是否顯示圖片原始大小 true表示圖片顯示原始大小 false表示顯示圖片縮放后的大小///計算單元格的長度和寬度double CellWidth = 0;double CellHeight = 0;int RowSpanCount = cell.GetSpan().RowSpan;//合并的單元格行數int ColSpanCount = cell.GetSpan().ColSpan;//合并的單元格列數 int j = 0;for (j = 0; j < RowSpanCount; j++)//根據合并的行數計算出高度 {CellHeight += cell.Sheet.GetRow(cell.RowIndex + j).Height;}for (j = 0; j < ColSpanCount; j++){CellWidth += cell.Row.Sheet.GetColumnWidth(cell.ColumnIndex + j);}//單元格長度和寬度與圖片的長寬單位互換是根據實例得出CellWidth = CellWidth / 35;CellHeight = CellHeight / 15;///計算圖片的長度和寬度MemoryStream ms = new MemoryStream(value);Image Img = Bitmap.FromStream(ms, true);double ImageOriginalWidth = Img.Width;//原始圖片的長度double ImageOriginalHeight = Img.Height;//原始圖片的寬度double ImageScalWidth = 0;//縮放后顯示在單元格上的圖片長度double ImageScalHeight = 0;//縮放后顯示在單元格上的圖片寬度if (CellWidth > ImageOriginalWidth && CellHeight > ImageOriginalHeight)//單元格的長度和寬度比圖片的大,說明單元格能放下整張圖片,不縮放 {ImageScalWidth = ImageOriginalWidth;ImageScalHeight = ImageOriginalHeight;bOriginalSize = true;}else//需要縮放,根據單元格和圖片的長寬計算縮放比例 {bOriginalSize = false;if (ImageOriginalWidth > CellWidth && ImageOriginalHeight > CellHeight)//圖片的長和寬都比單元格的大的情況 {double WidthSub = ImageOriginalWidth - CellWidth;//圖片長與單元格長的差距double HeightSub = ImageOriginalHeight - CellHeight;//圖片寬與單元格寬的差距if (WidthSub > HeightSub)//長的差距比寬的差距大時,長度x軸的縮放比為1,表示長度就用單元格的長度大小,寬度y軸的縮放比例需要根據x軸的比例來計算 {scalx = 1;scaly = (CellWidth / ImageOriginalWidth) * ImageOriginalHeight / CellHeight;//計算y軸的縮放比例,CellWidth / ImageWidth計算出圖片整體的縮放比例,然后 * ImageHeight計算出單元格應該顯示的圖片高度,然后/ CellHeight就是高度的縮放比例 }else{scaly = 1;scalx = (CellHeight / ImageOriginalHeight) * ImageOriginalWidth / CellWidth;}}else if (ImageOriginalWidth > CellWidth && ImageOriginalHeight < CellHeight)//圖片長度大于單元格長度但圖片高度小于單元格高度,此時長度不需要縮放,直接取單元格的,因此scalx=1,但圖片高度需要等比縮放 {scalx = 1;scaly = (CellWidth / ImageOriginalWidth) * ImageOriginalHeight / CellHeight;}else if (ImageOriginalWidth < CellWidth && ImageOriginalHeight > CellHeight)//圖片長度小于單元格長度但圖片高度大于單元格高度,此時單元格高度直接取單元格的,scaly = 1,長度需要等比縮放 {scaly = 1;scalx = (CellHeight / ImageOriginalHeight) * ImageOriginalWidth / CellWidth;}ImageScalWidth = scalx * CellWidth;ImageScalHeight = scaly * CellHeight;}Dx1 = Convert.ToInt32((CellWidth - ImageScalWidth) / CellWidth * 1023 / 2);Dy1 = Convert.ToInt32((CellHeight - ImageScalHeight) / CellHeight * 256 / 2);int pictureIdx = cell.Sheet.Workbook.AddPicture((Byte[])value, PictureType.PNG);IClientAnchor anchor = cell.Sheet.Workbook.GetCreationHelper().CreateClientAnchor();anchor.AnchorType = AnchorType.MoveDontResize;anchor.Col1 = cell.ColumnIndex;anchor.Col2 = cell.ColumnIndex + cell.GetSpan().ColSpan;anchor.Row1 = cell.RowIndex;anchor.Row2 = cell.RowIndex + cell.GetSpan().RowSpan;anchor.Dy1 = Dy1;//圖片下移量anchor.Dx1 = Dx1;//圖片右移量,通過圖片下移和右移,使得圖片能居中顯示,因為圖片不同文字,圖片是浮在單元格上的,文字是鉗在單元格里的IDrawing patriarch = cell.Sheet.CreateDrawingPatriarch();IPicture pic = patriarch.CreatePicture(anchor, pictureIdx);if (bOriginalSize){pic.Resize();//顯示圖片原始大小 }else{pic.Resize(scalx, scaly);//等比縮放 }}?
?
轉載于:https://www.cnblogs.com/xbzsz/p/8185266.html
總結
以上是生活随笔為你收集整理的NPOI随笔——图片在单元格等比缩放且居中显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装python的第一个曲折
- 下一篇: pycharm short-cut