欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在c#中利用NPOI在指定單元格中導(dǎo)入導(dǎo)出圖片-創(chuàng)新互聯(lián)

怎么在c#中利用NPOI 在指定單元格中導(dǎo)入導(dǎo)出圖片?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

成都創(chuàng)新互聯(lián)長期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為宜興企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、成都網(wǎng)站制作宜興網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

導(dǎo)入Excel 時(shí)解析圖片

xls 和 xlsx 的 API 稍有不同,詳細(xì)可以直接參考以下代碼,實(shí)現(xiàn)代碼如下:

public static Dictionary<CellPosition, IPictureData> GetPicturesAndPosition(this ISheet sheet)
{
  var dictionary = new Dictionary<CellPosition, 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 是一個(gè)自定義的結(jié)構(gòu)體,表示當(dāng)前單元格的位置,源碼如下:

public readonly struct CellPosition : IEquatable<CellPosition>
{
  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();
}

根據(jù)上面的代碼,我們就可以獲取到獲取到所有的圖片以及圖片的所在位置,這樣根據(jù)單元格位置去找圖片信息的時(shí)候就會(huì)很方便了

導(dǎo)出 Excel 時(shí)設(shè)置圖片

實(shí)現(xiàn)代碼如下:

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;
}

通過上面的代碼我們就可以在指定的單元格設(shè)置圖片,目前沒有支持單元格合并操作,有需要自己進(jìn)行修改

WeihanLi.Npoi

WeihanLi.Npoi 在 1.15.0 版本中增加了圖片導(dǎo)入導(dǎo)出的支持,使用示例可以參考下面的單元測(cè)試:

[Theory]
[ExcelFormatData]
public async Task ImageImportExportTest(ExcelFormat excelFormat)
{
  using var httpClient = new HttpClient();
  var imageBytes = await httpClient.GetByteArrayAsync("/tupian/20230522/8919.jpg");
  var list = Enumerable.Range(1, 5)
    .Select(x => new ImageTest() { Id = x, Image = imageBytes })
    .ToList();
  var excelBytes = list.ToExcelBytes(excelFormat);
  var importResult = ExcelHelper.ToEntityList<ImageTest>(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!;
}

導(dǎo)入時(shí)會(huì)自動(dòng)將 byte[] 類型的屬性嘗試獲取對(duì)應(yīng)的單元格位置的圖片,如果在對(duì)應(yīng)的位置找到了圖片就能夠讀取到圖片的字節(jié)數(shù)組信息映射到 model 里的字節(jié)數(shù)組屬性

關(guān)于怎么在c#中利用NPOI 在指定單元格中導(dǎo)入導(dǎo)出圖片問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

分享名稱:怎么在c#中利用NPOI在指定單元格中導(dǎo)入導(dǎo)出圖片-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://www.chinadenli.net/article48/dcdjep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)靜態(tài)網(wǎng)站品牌網(wǎng)站制作網(wǎng)站內(nèi)鏈域名注冊(cè)Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)