C#获取excel中的图片,保存到本地文件夹


最近开发一个功能,从excel中批量导入数据到数据库中,而excel中的内容中包含图片。如果没有包含图片的话,只是纯文本信息,网上已经有很多例子,我就不写了。我想写的是,读取excel中每行特定列的的图片。

Excel 2007文件 如下:

把该excel文件的后缀名(xlsx)改成zip 或者rnr,我把它改成zip,解压后你就会发现,excel也就是多个xml文件组成的,所以,很显然,我们只解析xml文件,就能找到图片了。

解压后的excel文件如下:

Excel中的图片信息都在文件夹xl里面,点击进去就可以看到以下内容:

图片就保存在media这个文件夹里面,而获取图片要解析的xml,在drawings文件夹里面,所以当要解析xml获取图片信息时,可以通过判断drawings文件夹或者media 文件夹存不存在,来控制程序的执行。如果excel中不存在其中的一个,表明excel中是不存在图片的,都是文本信息。

                  图1

media文件夹中存放着excel的图片,只是名字按添加的顺序依次改为image1、image2……,如果添加的图片相同的话,meida只保存一个,excel会判断名字添加的图片名字相同不。这里强调一下,excel中的图片信息的位置关系,是按照在excel中插入图片的顺序排列的,并不是按照行号来排列,以为图片和单元格没有附属关系、依附关系。

 

3.解析drawings文件夹的xml。drawings文件夹中包含drawing1.xml 和_rels文件夹。

drawings.xml 在浏览器中的xml代码如下(直接把这个文件拖到浏览器中就可以看到),我们只看一张图片的,每一张图片以xdr:twoCellAnchor标签分隔开。

只截取有用的xml信息,如图所示:

                     图2

然后在看看_rels文件夹中的drawing1.xml.rels的xml信息。如下图所示:

                                 图3

从上面三张图中,我们不难发现一些规则,以下一一解释:

图1是media文件夹,是存图片的。

图2是drawings.xml,是存图片在excel中的位置等信息。<xdr:from>这个标签和<xdr:to> 是对应的,这两个标签中都有 <xdr:col>和<xdr:row>,col也就是colum(列),而row(行);<xdr:from>表示图片从第几行第几列 <xdr:to>到 第几行第几列,也就是图片在excel中的覆盖范围。这个例子是图片只能保存在一个单元格内,所以只取from里面的row就知道该图片属于哪行了,列都是固定的(Picutre)。在看看row和col的数字,跟excel中的对比一下,在xml中,row和col索引都是从0开始的。

 

再看<xdr:blipFill>下的子节点<xdr:blipFill>中的<a:blip r:embed="rId1"/>,和图3对比一下,就可以知道图片存在哪个文件夹下的哪种图片。

 

以上是思路,实现代码如下:

 

把excel文件复制一份,并把后缀名改成zip,可以通过File.Copy()函数实现

 

string oriFile = @"D:\picture.xlsx";

string destFile = @"D:\picture.zip";

File.Copy(oriFile,destFile,true);

 

 

解压picture.zip文件

using ICSharpCode.SharpZipLib.Zip; //这个类库可以去下载。

 

string err = "";
        /// <summary>  
        /// 功能:解压zip格式的文件。  
        /// </summary>  
        /// <param name="zipFilePath">压缩文件路径</param>  
        /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>  
        /// <param name="err">出错信息</param>  
        /// <returns>解压是否成功</returns>  
        public static bool UnZipFile(string zipFilePath, string unZipDir, out string err)
        {
            err = "";
            if (zipFilePath == string.Empty)
            {
                err = "压缩文件不能为空!";
                return false;
            }
            if (!File.Exists(zipFilePath))
            {
                err = "压缩文件不存在!";
                return false;
            }
            //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹  
            if (unZipDir == string.Empty)
                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
            if (!unZipDir.EndsWith("//"))
                unZipDir += "//";
            if (!Directory.Exists(unZipDir))
                Directory.CreateDirectory(unZipDir);

            try
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
                {

                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);
                        if (directoryName.Length > 0)
                        {
                            Directory.CreateDirectory(unZipDir + directoryName);
                        }
                        if (!directoryName.EndsWith("//"))
                            directoryName += "//";
                        if (fileName != String.Empty)
                        {
                            using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
                            {

                                int size = 2048;
                                byte[] data = new byte[2048];
                                while (true)
                                {
                                    size = s.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        streamWriter.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }//while  
                }
            }
            catch (Exception ex)
            {
                err = ex.Message;
                return false;
            }
            
            return true;
        }

 

 

 

解析Xml

 

string XMLExcelPath = @"D:\picture\xl\drawings\drawing1.xml";Dictionary<string, string> picRowDictionary = new Dictionary<string, string>();//保存行号和图片的属性rId

        private void FromXMLBtn_Click(object sender, EventArgs e)

        {

            XmlDocument xdoc = new XmlDocument();

            xdoc.Load(XMLExcelPath);

            //获取根节点

            XmlNode root = xdoc.DocumentElement;

            //获取根节点下的所有子节点

            XmlNodeList nodeList = root.ChildNodes;

            //循环遍历每一个子节点

            foreach (XmlNode nl in nodeList)

            {

                XmlElement sondNode = (XmlElement)nl;

                XmlNodeList descendDodeList = sondNode.ChildNodes;

                XmlNodeList fromNodeList = descendDodeList[0].ChildNodes;

                //取得行号

                string row = fromNodeList.Item(2).InnerText.Trim();

 

                XmlNodeList picNodeList = descendDodeList[2].ChildNodes;

                XmlNodeList blipFillNodeList = picNodeList[1].ChildNodes;

                XmlElement picElement =(XmlElement)blipFillNodeList.Item(0);

                string picturePath = picElement.GetAttribute("r:embed").ToString();

                picRowDictionary.Add(row,picturePath);

               

            }

 

            GetPicture(picRowDictionary);

        }

 

//获取图片,并把图片copy出来

        string pictPath = @"D:\picture\xl\drawings\_rels\drawing1.xml.rels";

        string dir = @"D:\picture\xl";

        string pathOfPicture = string.Empty;

        Dictionary<string,string> rowAndNewPicPath=new Dictionary<string,string>();

        public Dictionary<string,string>  GetPicture (Dictionary<string,string> dic)

        {

            if (dic.Count <= 0) return null;

            XmlDocument doc = new XmlDocument();

            doc.Load(pictPath);

 

            XmlNode root = doc.DocumentElement;

            XmlNodeList nlist = root.ChildNodes;

            foreach (KeyValuePair<string, string> kvp in dic)

            {

                foreach(XmlNode xn in nlist)

                {

                    XmlElement xe = (XmlElement)xn;

                    if (xe.GetAttribute("Id").ToString() == kvp.Value)

                    {

                        pathOfPicture = xe.GetAttribute("Target").ToString().Replace("..","").Replace("/",@"\");

                        pathOfPicture = dir + pathOfPicture;

                       CopyPicture(kvp.Key,pathOfPicture,rowAndNewPicPath);

                        break;

                    }

                }

            }

            return rowAndNewPicPath;

        }

 

//根据路径获取图片

        public void CopyPicture(string row,string path,Dictionary<string, string> d)

        {

           string name=Path.GetFileNameWithoutExtension(path);

           if (!Directory.Exists(@"D:\resource"))

           {

               Directory.CreateDirectory(@"D:\resource");

           }

            File.Copy(path,@"D:\resource\"+name+".jpg",true);

            d.Add(row,@"D:\rosource\"+name+".jpg");

        }

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM