当前位置: 代码网 > it编程>编程语言>Asp.net > .NET使用NPOI实现读取带有图片的excel数据

.NET使用NPOI实现读取带有图片的excel数据

2024年05月15日 Asp.net 我要评论
前言在.net使用npoi插件进行批量导入时,获取excel中的图片数据,存到集合中。使用步骤1.定义类picturedata代码如下:public class picturedata{ pub

前言

在.net使用npoi插件进行批量导入时,获取excel中的图片数据,存到集合中。

使用步骤

1.定义类picturedata

代码如下:

public class picturedata
{
    public byte[] data { get; set; }
}

2.数据集引用

using npoi.xssf.usermodel;
using npoi.ss.usermodel;
using npoi.xssf.usermodel.extensions;

npoi插件需要自己下载。

3.定义获取excel图片数据的方法getdtwithimg

    /// <summary>
    /// 获取excel中图片数据
    /// </summary>
    /// <param name="path">文件路径</param>
    /// <returns>list</returns>
 private list<picturedata> getdtwithimg(string path)
    {
        using (filestream file = new filestream(path, filemode.open, fileaccess.read))
        {
            iworkbook workbook = null;
            isheet sheet = null;

            workbook = workbookfactory.create(file);
            sheet = workbook.getsheet("sheet1");
            if (sheet == null)
            {
                sheet = workbook.getsheetat(0);
            }

            // 获取绘图对象
            xssfdrawing drawing = (xssfdrawing)sheet.createdrawingpatriarch();
            list<xssfpicture> picturesincolumna = new list<xssfpicture>();
            list<xssfshape> shapes = drawing.getshapes();
            foreach (xssfshape shape in shapes)
            {
                if (shape is xssfpicture)
                {
                    xssfpicture picture = (xssfpicture)shape;
                    xssfclientanchor anchor = (xssfclientanchor)picture.getpreferredsize();                  
                    picturesincolumna.add(picture);
                }
            }
            list<picturedata> picturedatalist = new list<picturedata>();
            //将图片数据放到集合中
            foreach (xssfpicture picture in picturesincolumna)
            {
                byte[] picturedata = picture.picturedata.data;
                picturedata data = new picturedata
                {                   
                    data = picturedata
                };
                picturedatalist.add(data);
            }
            return picturedatalist;
        }

    }

总结

通过该方法可以获取excel中的图片,将其转换成字节流存到集合中,方便后续对其进行操作。

到此这篇关于.net使用npoi实现读取带有图片的excel数据的文章就介绍到这了,更多相关.net npoi读取带图片excel数据内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com