.NetCore NPOI 读取excel内容及单元格内图片

由于数据方提供的数据在excel文件中不止有文字内容还包含图片信息,于是编写相关测试代码,读取excel文件内容及图片信息.

本文使用的是 NPOI-2.6.2 版本,此版本持.Net4.7.2;.NetStandard2.0;.NetStandard2.1;.Net6.0+。

测试文档内容,如下图:

保存后的图片:

打开图片显示正常:

编写读取数据方法,代码如:

cs 复制代码
static public DataTable ReadExcel(string filePath, string _sDirImg)
{
    using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
        IWorkbook workbook = null;
        string sExt = Path.GetExtension(filePath).ToLower();

        if (sExt == ".xlsx")
        {
            workbook = new XSSFWorkbook(file);//新版本的Excel(.xlsx) 
        }
        else
        {
            workbook = new HSSFWorkbook(file);//老版本的Excel(.xls) 

        }

        // 读取第一个 Sheet
        ISheet sheet = workbook.GetSheetAt(0);

        if (sExt == ".xlsx")
        {
            fnReadImageXSSF(sheet, _sDirImg);//新版本的Excel(.xlsx) 

        }
        else
        {
            fnReadImageHSSF(sheet, _sDirImg);//老版本的Excel(.xls) 
        }

        // 创建 DataTable
        DataTable dataTable = new DataTable(sheet.SheetName);

        // 读取表头
        IRow headerRow = sheet.GetRow(0);
        for (int i = 0; i < headerRow.LastCellNum; i++)
        {
            ICell cell = headerRow.GetCell(i);
            dataTable.Columns.Add(cell.ToString(), typeof(string));
        }

        // 读取数据行
        for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++)
        {
            IRow row = sheet.GetRow(rowNum);
            DataRow dataRow = dataTable.NewRow();

            for (int i = 0; i < row.LastCellNum; i++)
            {
                ICell cell = row.GetCell(i);

                if (null == cell) continue;

                dataRow[i] = cell.ToString();
            }

            dataTable.Rows.Add(dataRow);
        }

        return dataTable;
    }
}

编写对应不同版本的excel文件,读取图片方法,读取excel .xlsx文件内图片:

cs 复制代码
/// <summary>
/// 读取excel .xlsx文件内图片
/// </summary>
/// <param name="sheet"></param>
/// <param name="_sDirImg"></param>
static public void fnReadImageXSSF(ISheet sheet, string _sDirImg)
{
    // 读取图像信息
    foreach (XSSFShape shape in ((XSSFDrawing)sheet.DrawingPatriarch).GetShapes())
    {
        if (shape is XSSFPicture)
        {
            XSSFPicture picture = (XSSFPicture)shape;

            // 获取图片所在单元格的行号和列号
            int rowIndex = picture.GetPreferredSize().Row1;
            int colIndex = picture.GetPreferredSize().Col1;

            // 获取图像文件格式
            string imageFormat = picture.PictureData.MimeType switch
            {
                "image/jpeg" => "jpeg",
                "image/png" => "png",
                "image/gif" => "gif",
                "image/bmp" => "bmp",
                _ => "jpg"
            };

            // 保存图像文件
            string outputFileName = _sDirImg + $"{rowIndex}-{colIndex}-{Guid.NewGuid()}.{imageFormat}";
            using (FileStream imageFile = new FileStream(outputFileName, FileMode.Create))
            {
                imageFile.Write(picture.PictureData.Data, 0, picture.PictureData.Data.Length);
            }

            Console.WriteLine($"Saved image: {outputFileName}");
        }
       
    }
}

读取excel .xls文件内图片,代码如下:

cs 复制代码
/// <summary>
/// 读取excel .xls文件内图片
/// </summary>
/// <param name="sheet"></param>
/// <param name="_sDirImg"></param>
static public void fnReadImageHSSF(ISheet sheet, string _sDirImg)
{
    // 读取图像信息
    foreach (HSSFShape shape in ((HSSFPatriarch)sheet.DrawingPatriarch).Children)
    {
        if (shape is HSSFPicture)
        {
            HSSFPicture picture = (HSSFPicture)shape;

            // 获取图片所在单元格的行号和列号
            int rowIndex = picture.GetPreferredSize().Row1;
            int colIndex = picture.GetPreferredSize().Col1;

            // 获取图像文件格式
            string imageFormat = picture.PictureData.MimeType switch
            {
                "image/jpeg" => "jpeg",
                "image/png" => "png",
                "image/gif" => "gif",
                "image/bmp" => "bmp",
                _ => "jpg"
            };

            // 保存图像文件
            string outputFileName = _sDirImg + $"{rowIndex}-{colIndex}-{Guid.NewGuid()}.{imageFormat}";
            using (FileStream imageFile = new FileStream(outputFileName, FileMode.Create))
            {
                imageFile.Write(picture.PictureData.Data, 0, picture.PictureData.Data.Length);
            }

            Console.WriteLine($"Saved image: {outputFileName}");
        }
    }
}

正常处理应该是读取到图片保存成功后,处理datatable图片列的相关地址,如:uploads/xxx/xx.jpg ,返回保存在服务器上的地址,以便前端访问或保存到数据库等,本文并没有处理,有需要的伙伴自行处理吧,希望本文对你有帮助。

相关推荐
van久4 天前
Day19:Service 业务层(企业架构核心)
.netcore
武藤一雄5 天前
WPF中逻辑树(Logical Tree)与可视化树(Visual Tree)到底是什么
microsoft·c#·.net·wpf·.netcore
武藤一雄8 天前
19个核心算法(C#版)
数据结构·windows·算法·c#·排序算法·.net·.netcore
火星papa8 天前
C# 【通过NPIO读写Excel表】
c#·excel·npoi
van久10 天前
Day17:EF Core 增删改 + 事务
.netcore
MoFe111 天前
【.net core】【watercloud】处理rabbitmq类初始化时获取系统已注入的数据库连接问题(调用已注入服务)
数据库·rabbitmq·.netcore
MoFe115 天前
【.net core】【RabbitMq】rabbitmq在.net core中的简单使用
分布式·rabbitmq·.netcore
van久16 天前
Day15:EF Core 入门 + CodeFirst(就业核心版)
.netcore
叫我黎大侠16 天前
.NET 实战:调用千问视觉模型实现 OCR(车票识别完整教程)
阿里云·ai·c#·ocr·asp.net·.net·.netcore
van久16 天前
Day15-4:【日志】中间件和过滤器 的对比选择
.netcore