当前位置: 首页 > news >正文

校园网站建设 方案论证深圳关键词推广排名

校园网站建设 方案论证,深圳关键词推广排名,wordpress迁站,阆中市建设局网站首页NPOI 在指定单元格导入导出图片Intro我维护了一个 NPOI 的扩展(WeihanLi.Npoi)#xff0c;主要用来导入导出 Excel 数据#xff0c;最近有网友提出了导入 Excel 的时候解析图片的需求#xff0c;于是就有了本文的探索导入Excel 时解析图片xls 和 xlsx 的 API 稍有不同#… NPOI 在指定单元格导入导出图片Intro我维护了一个 NPOI 的扩展(WeihanLi.Npoi)主要用来导入导出 Excel 数据最近有网友提出了导入 Excel 的时候解析图片的需求于是就有了本文的探索导入Excel 时解析图片xls 和 xlsx 的 API 稍有不同详细可以直接参考以下代码实现代码如下public static DictionaryCellPosition, IPictureData GetPicturesAndPosition(this ISheet sheet) {var dictionary  new DictionaryCellPosition, IPictureData();if (sheet.Workbook is HSSFWorkbook){foreach (var shape in ((HSSFPatriarch)sheet.DrawingPatriarch).Children){if (shape is HSSFPicture picture){var position  new CellPosition(picture.ClientAnchor.Row1, picture.ClientAnchor.Col1);dictionary[position]  picture.PictureData;}}}else if (sheet.Workbook is XSSFWorkbook){foreach (var shape in ((XSSFDrawing)sheet.DrawingPatriarch).GetShapes()){if (shape is XSSFPicture picture){var position  new CellPosition(picture.ClientAnchor.Row1, picture.ClientAnchor.Col1);dictionary[position]  picture.PictureData;}}}return dictionary; } CellPosition 是一个自定义的结构体表示当前单元格的位置源码如下public readonly struct CellPosition : IEquatableCellPosition {public CellPosition(int row, int col){Row  row;Column  col;}public int Row { get; }public int Column { get; }public bool Equals(CellPosition other){return Row  other.Row  Column  other.Column;}public override bool Equals(object? obj)  obj is CellPosition other  Equals(other);public override int GetHashCode()  ${Row}_{Column}.GetHashCode(); } 根据上面的代码我们就可以获取到获取到所有的图片以及图片的所在位置这样根据单元格位置去找图片信息的时候就会很方便了导出 Excel 时设置图片实现代码如下public static bool TryAddPicture(this ISheet sheet, int row, int col, byte[] pictureBytes, PictureType pictureType  PictureType.PNG) {if (sheet is null){throw new ArgumentNullException(nameof(sheet));}try{var pictureIndex  sheet.Workbook.AddPicture(pictureBytes, pictureType);var clientAnchor  sheet.Workbook.GetCreationHelper().CreateClientAnchor();clientAnchor.Row1  row;clientAnchor.Col1  col;var picture  (sheet.DrawingPatriarch ?? sheet.CreateDrawingPatriarch()).CreatePicture(clientAnchor, pictureIndex);picture.Resize();return true;}catch (Exception e){Debug.WriteLine(e);}return false; } 通过上面的代码我们就可以在指定的单元格设置图片目前没有支持单元格合并操作有需要自己进行修改WeihanLi.NpoiWeihanLi.Npoi 在 1.15.0 版本中增加了图片导入导出的支持使用示例可以参考下面的单元测试[Theory] [ExcelFormatData] public async Task ImageImportExportTest(ExcelFormat excelFormat) {using var httpClient  new HttpClient();var imageBytes  await httpClient.GetByteArrayAsync(https://weihanli.xyz/assets/avator.jpg);var list  Enumerable.Range(1, 5).Select(x  new ImageTest() { Id  x, Image  imageBytes }).ToList();var excelBytes  list.ToExcelBytes(excelFormat);var importResult  ExcelHelper.ToEntityListImageTest(excelBytes, excelFormat);Assert.NotNull(importResult);Assert.Equal(list.Count, importResult.Count);for (var i  0; i  list.Count; i){Assert.NotNull(importResult[i]);var result  importResult[i]!;Assert.Equal(list[i].Id, result.Id);Assert.NotNull(result.Image);Assert.True(list[i].Image.SequenceEqual(result.Image));} }private class ImageTest {public int Id { get; set; }public byte[] Image { get; set; }  null!; } 导入时会自动将 byte[] 类型的属性尝试获取对应的单元格位置的图片如果在对应的位置找到了图片就能够读取到图片的字节数组信息映射到 model 里的字节数组属性除此之外还支持直接导入的时候将图片信息 Map 到 IPictureData 属性上但是觉得还是字节数组更通用一些如果对此感兴趣可以参考项目源码以及单元测试More感谢 ZeguangZhang94 童鞋提出的需求和帮忙测试~如果你也有类似的读取指定单元格的图片或者在指定单元格插入图片的需求可以试一下上面的方法希望对你有帮助可以直接引用我的类库或者直接拷贝源码到自己的项目里使用Referenceshttps://github.com/WeihanLi/WeihanLi.Npoihttps://github.com/WeihanLi/WeihanLi.Npoi/blob/dev/src/WeihanLi.Npoi/NpoiExtensions.cs#L1143https://stackoverflow.com/questions/24084129/read-image-from-excel-file-using-npoihttps://stackoverflow.com/questions/41138848/add-image-to-excel-xlsx-using-npoi-c-sharphttps://github.com/WeihanLi/WeihanLi.Npoi/issues/99
http://www.yutouwan.com/news/420859/

相关文章:

  • 女装网站欣赏青海建设厅网站证件查询
  • 做视频网站需要备案网站多少钱
  • 企业网站建设的意义和效果html怎么做商品页面
  • 网站服务器参数查询网站版面
  • 丹东企业网站建设平台做网络平台的网站
  • 百度网站审核期时间搭建平台的成语
  • 站长号查询入口站长工具建站之星网站 seo优化
  • 设计公司画册零感设计网站seo哪家好
  • 网站维护中页面设计福州短视频seo排行
  • 网站开发样板百度资源共享
  • 台州网站设计飞速定制软件app
  • 用html建设网站买完域名接下来怎么弄
  • 网站分栏目怎么做网站发语音功能如何做
  • 自己做网站卖产品怎么样网站建设开发费怎么做账
  • 进网站显示建设中怎么解决卢沟桥网站建设
  • 如何将自己做网站放上网百度学术官网论文查重免费
  • 个人网站做接口可以么金网科技有限公司
  • 金融网站模板素材宁波建设集团股份有限公司
  • 有谁用2008做网站服务器做网站国外网站
  • 做网站的域名是做什么用的高端画册设计
  • 网站制作培训多少钱亚马逊海外购
  • 中太建设集团网站wordpress博客下载
  • 装修公司网站asp源码平台网站建设外包费用
  • 北京高端网站建设服务装饰设计收费标准2020
  • 网站建设与维护实验心得2345网址高级版
  • 贵州网站制作公司电话互联网营销 网站 推荐
  • jsp网站建设项目实战课后dede 网站根目录
  • 一般做网站是用什么程序做的网站添加百度地图
  • 网站副标题济南疾控最新发布
  • 企业网站推广方案设计毕业设计WordPress来应力