![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System.Collections.Generic; 2 using NPOI.HSSF.UserModel; 3 using System.IO; 4 using NPOI.SS.UserModel; 5 6 namespace NPOIDemo 7 { 8 public class Excel 9 { 10 public HSSFWorkbook hssfWorkbook 11 { 12 get { 13 FileStream file = new FileStream(@"D:SourceNPOIDemoNPOIDemoFiles emp.xls", FileMode.Open, FileAccess.Read); 14 15 return new HSSFWorkbook(file); 16 } 17 } 18 19 /// <summary> 20 /// 获取Sheet中所有数据有效性 21 /// </summary> 22 /// <returns></returns> 23 public List<IDataValidation> GetDataValidations() 24 { 25 ISheet ws = hssfWorkbook.GetSheet("1"); 26 return ws.GetDataValidations(); 27 } 28 29 /// <summary> 30 /// 获取单元格数据有效性 31 /// </summary> 32 /// <param name="range">单元格范围 F71:F93或E5等</param> 33 /// <returns></returns> 34 public string[] GetExplicitListValuesByRange(string range) 35 { 36 ISheet ws = hssfWorkbook.GetSheet("1"); 37 var dataValidations = ws.GetDataValidations(); 38 foreach (var item in dataValidations) 39 { 40 foreach (var rangeAddresses in item.Regions.CellRangeAddresses) 41 { 42 if (rangeAddresses.FormatAsString() == range) 43 { 44 return item.ValidationConstraint.ExplicitListValues; 45 } 46 } 47 } 48 49 return new string[0]; 50 } 51 52 /// <summary> 53 /// 获取批注 54 /// </summary> 55 /// <returns></returns> 56 public string GetCommont() 57 { 58 ISheet ws = hssfWorkbook.GetSheet("1"); 59 IComment commont=ws.GetCellComment(88, 55); 60 61 return commont.String.String; 62 } 63 64 /// <summary> 65 /// 提取图片及图片信息 66 /// </summary> 67 /// <returns></returns> 68 public IList<PicturesInfo> GetPictures() 69 { 70 ISheet ws = hssfWorkbook.GetSheet("1"); 71 //return ws.Workbook.GetAllPictures(); 72 73 return ws.GetAllPictureInfos(365,370,9,20); 74 } 75 76 } 77 78 79 }
提取图片扩展方法来源:使用NPOI从Excel中提取图片及图片位置信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class PicturesInfo 2 { 3 public string ext { get; set; } 4 public int MinRow { get; set; } 5 public int MaxRow { get; set; } 6 public int MinCol { get; set; } 7 public int MaxCol { get; set; } 8 public Byte[] PictureData { get; private set; } 9 10 public PicturesInfo(int minRow, int maxRow, int minCol, int maxCol, Byte[] pictureData, string ext) 11 { 12 this.MinRow = minRow; 13 this.MaxRow = maxRow; 14 this.MinCol = minCol; 15 this.MaxCol = maxCol; 16 this.PictureData = pictureData; 17 this.ext = ext; 18 } 19 } 20 public static class NpoiExtend 21 { 22 public static List<PicturesInfo> GetAllPictureInfos(this ISheet sheet) 23 { 24 return sheet.GetAllPictureInfos(null, null, null, null); 25 } 26 27 public static List<PicturesInfo> GetAllPictureInfos(this ISheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal = true) 28 { 29 if (sheet is HSSFSheet) 30 { 31 return GetAllPictureInfos((HSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal); 32 } 33 else if (sheet is XSSFSheet) 34 { 35 return GetAllPictureInfos((XSSFSheet)sheet, minRow, maxRow, minCol, maxCol, onlyInternal); 36 } 37 else 38 { 39 throw new Exception("未处理类型,没有为该类型添加:GetAllPicturesInfos()扩展方法!"); 40 } 41 } 42 43 private static List<PicturesInfo> GetAllPictureInfos(HSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal) 44 { 45 List<PicturesInfo> picturesInfoList = new List<PicturesInfo>(); 46 47 var shapeContainer = sheet.DrawingPatriarch as HSSFShapeContainer; 48 if (null != shapeContainer) 49 { 50 var shapeList = shapeContainer.Children; 51 foreach (var shape in shapeList) 52 { 53 if (shape is HSSFPicture && shape.Anchor is HSSFClientAnchor) 54 { 55 var picture = (HSSFPicture)shape; 56 var anchor = (HSSFClientAnchor)shape.Anchor; 57 58 if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal)) 59 { 60 61 picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data,picture.PictureData.MimeType)); 62 } 63 } 64 } 65 } 66 67 return picturesInfoList; 68 } 69 70 private static List<PicturesInfo> GetAllPictureInfos(XSSFSheet sheet, int? minRow, int? maxRow, int? minCol, int? maxCol, bool onlyInternal) 71 { 72 List<PicturesInfo> picturesInfoList = new List<PicturesInfo>(); 73 74 var documentPartList = sheet.GetRelations(); 75 foreach (var documentPart in documentPartList) 76 { 77 if (documentPart is XSSFDrawing) 78 { 79 var drawing = (XSSFDrawing)documentPart; 80 var shapeList = drawing.GetShapes(); 81 foreach (var shape in shapeList) 82 { 83 if (shape is XSSFPicture) 84 { 85 var picture = (XSSFPicture)shape; 86 var anchor = picture.GetPreferredSize(); 87 88 if (IsInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, onlyInternal)) 89 { 90 picturesInfoList.Add(new PicturesInfo(anchor.Row1, anchor.Row2, anchor.Col1, anchor.Col2, picture.PictureData.Data,picture.PictureData.MimeType)); 91 } 92 } 93 } 94 } 95 } 96 97 return picturesInfoList; 98 } 99 100 private static bool IsInternalOrIntersect(int? rangeMinRow, int? rangeMaxRow, int? rangeMinCol, int? rangeMaxCol, 101 int pictureMinRow, int pictureMaxRow, int pictureMinCol, int pictureMaxCol, bool onlyInternal) 102 { 103 int _rangeMinRow = rangeMinRow ?? pictureMinRow; 104 int _rangeMaxRow = rangeMaxRow ?? pictureMaxRow; 105 int _rangeMinCol = rangeMinCol ?? pictureMinCol; 106 int _rangeMaxCol = rangeMaxCol ?? pictureMaxCol; 107 108 if (onlyInternal) 109 { 110 return (_rangeMinRow <= pictureMinRow && _rangeMaxRow >= pictureMaxRow && 111 _rangeMinCol <= pictureMinCol && _rangeMaxCol >= pictureMaxCol); 112 } 113 else 114 { 115 return ((Math.Abs(_rangeMaxRow - _rangeMinRow) + Math.Abs(pictureMaxRow - pictureMinRow) >= Math.Abs(_rangeMaxRow + _rangeMinRow - pictureMaxRow - pictureMinRow)) && 116 (Math.Abs(_rangeMaxCol - _rangeMinCol) + Math.Abs(pictureMaxCol - pictureMinCol) >= Math.Abs(_rangeMaxCol + _rangeMinCol - pictureMaxCol - pictureMinCol))); 117 } 118 } 119 }