由于要取 xls 里面的图片名称,使用NPOI获取时错误,于是想了个办法
NPOI 获取 xls 表格图片名称错误解决如下:
HSSFPicture.cs 里面原始 FileName
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public String FileName { get { EscherComplexProperty propFile = (EscherComplexProperty)GetOptRecord().Lookup( EscherProperties.BLIP__BLIPFILENAME); return (null == propFile) ? "" : Trim(StringUtil.GetFromUnicodeLE(propFile.ComplexData)); } set { // TODO: add trailing \u0000? byte[] bytes = StringUtil.GetToUnicodeLE(value); EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, true, bytes); SetPropertyValue(prop); } } |
新增加名称
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /// <summary> /// 图片名称 /// </summary> public String Name { get { EscherComplexProperty propFile = (EscherComplexProperty)GetOptRecord().Lookup( EscherProperties.GROUPSHAPE__SHAPENAME); try { if (null == propFile) { return ""; } return Trim(Encoding.Unicode.GetString(propFile.ComplexData)); } catch (Exception) { return ""; } } set { try { EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.GROUPSHAPE__SHAPENAME, true, Encoding.Unicode.GetBytes(value)); SetPropertyValue(prop); } catch (Exception) { logger.Log(POILogger.ERROR, "Unsupported encoding: UTF-16LE"); } } } |
用 Nmae 即可获取到正确图片名称
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //... var shapeContainer = sheet.DrawingPatriarch as HSSFShapeContainer; if (null != shapeContainer) { var shapeList = shapeContainer.Children; foreach (var shape in shapeList) { if (shape is HSSFPicture && shape.Anchor is HSSFClientAnchor) { var picture = (HSSFPicture)shape; if (picture.Name == "logo")//图片名称 { img = picture; break; } } } } //... |