免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
NPOI 在指定單元格導(dǎo)入導(dǎo)出圖片

Intro

我維護(hù)了一個 NPOI 的擴(kuò)展(WeihanLi.Npoi),主要用來導(dǎo)入導(dǎo)出 Excel 數(shù)據(jù),最近有網(wǎng)友提出了導(dǎo)入 Excel 的時候解析圖片的需求,于是就有了本文的探索

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

xlsxlsx 的 API 稍有不同,詳細(xì)可以直接參考以下代碼,實現(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 是一個自定義的結(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ù)單元格位置去找圖片信息的時候就會很方便了

導(dǎo)出 Excel 時設(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.Npoi1.15.0 版本中增加了圖片導(dǎo)入導(dǎo)出的支持,使用示例可以參考下面的單元測試:

[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.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)入時會自動將 byte[] 類型的屬性嘗試獲取對應(yīng)的單元格位置的圖片,如果在對應(yīng)的位置找到了圖片就能夠讀取到圖片的字節(jié)數(shù)組信息映射到 model 里的字節(jié)數(shù)組屬性

除此之外,還支持直接導(dǎo)入的時候?qū)D片信息 Map 到 IPictureData 屬性上,但是覺得還是字節(jié)數(shù)組更通用一些,如果對此感興趣可以參考項目源碼以及單元測試

More

感謝 @ZeguangZhang94 童鞋提出的需求和幫忙測試~

如果你也有類似的讀取指定單元格的圖片或者在指定單元格插入圖片的需求,可以試一下上面的方法,希望對你有幫助,可以直接引用我的類庫或者直接拷貝源碼到自己的項目里使用

References

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
關(guān)于 NPOI 報 Invalid column index (256). Allowable column range for BIFF8 is (0..255) or (''A''..''IV'') 錯誤的解決辦法
比較常用的25條Excel技巧整理放送 - Office辦公應(yīng)用 - 太平洋電腦網(wǎng)軟件論壇...
asp.net Mvc Npoi 導(dǎo)出導(dǎo)入 excel
如何使用JavaScript實現(xiàn)純前端讀取和導(dǎo)出excel文件
使用NPOI導(dǎo)入導(dǎo)出標(biāo)準(zhǔn)Excel
Excel教程:用Excel制作工資條的兩種簡單方法
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服