大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
需求:在Winform使用NOPI做导入时候,需要导入数据的同时导入图片。
专注于为中小企业提供成都网站设计、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业福田免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
虽然代码方面不适用(我好像也没仔细看过代码),但是感谢大佬给了灵感http://www.wjhsh.net/IT-Ramon-p-13100039.html,将excel后缀修改成Zip,解压,在其中找到图片的xml位置信息,以及对应的xml图片
效果图:
1、核心读取代码
System.Data.DataTable dt; private void btnImport_Click(object sender, EventArgs e) { dt= null; string copyAfterFileName = DateTime.Now.ToString("yyyyMMddHHssmm");//文件名 以及解压后的文件夹名 string copyAfterFileNameExt = ".zip";//压缩后缀 string modelExlPath = Environment.CurrentDirectory + "\\Temp\\Cache";//缓存文件 if (!Directory.Exists(modelExlPath)) { Directory.CreateDirectory(modelExlPath); }//创建缓存文件夹 string copyAfterFullName = Path.Combine(modelExlPath, copyAfterFileName + copyAfterFileNameExt);//移动到该目录并改为压缩包 string copyDirFullName = Path.Combine(modelExlPath, copyAfterFileName);//解压后的文件夹位置 OpenFileDialog openfile= new OpenFileDialog(); openfile.Filter= "导入Excel(*.xls,*.xlsx)|*.xls;*.xlsx"; openfile.FilterIndex= 0; openfile.RestoreDirectory= true; openfile.Title= "导入文件路径"; //openfile.ShowDialog(); if (openfile.ShowDialog() != DialogResult.OK) { return; } Common.ShowWaitForm();//正在加载..窗口 FileInfo fi1 = new FileInfo(openfile.FileName); fi1.CopyTo(copyAfterFullName);//移动文件,并修改称为yyyyMMddHHssmm.zip try { wsDr wsDrModel= null; if (!Directory.Exists(copyDirFullName)) { Directory.CreateDirectory(copyDirFullName); } //解压到当前文件夹 if (SharpZip.UnpackFiles(copyAfterFullName, copyDirFullName) == false)//yyyyMMddHHssmm.zip为文件夹yyyyMMddHHssmm { Common.ShowErrorDialog("导入失败!"); LogHelper.Instance.Error("产品导入失败," + copyDirFullName + "自解压失败!"); return; } else { wsDrModel= GetImgLoaction(copyDirFullName);//读取excel图片信息,对应位置,对应的绝对路径,对应的图片实体 } string msg = ""; dt= ExcelUtil.ExcelToTable(openfile.FileName, wsDrModel, ref msg);//获得Excel if (!string.IsNullOrEmpty(msg)) { Common.ShowInfoDialog(msg); } if (dt == null || dt.Rows.Count <= 0) { Common.ShowSuccessTip("导入失败!"); return; } LoadList();//刷新表格 } catch (Exception ex) { Common.ShowErrorDialog("导入错误!" + ex.Message); } finally { Common.HideWaitForm();//隐藏正在加载..窗口 } } private wsDr GetImgLoaction(string copydirfullname) { //copydirfullname = "E:\\administrator\\Desktop\\test\\yyyyMMddHHssmm"; string pathMap = Path.Combine(copydirfullname, "xl\\drawings\\drawing1.xml"); var doc = XDocument.Load(pathMap); //清理大部分命名空间,blip属性中的情况只能手动指定了 doc.Descendants().Attributes().Where(x => x.IsNamespaceDeclaration).Remove(); foreach (var elem in doc.Descendants()) { elem.Name= elem.Name.LocalName; } doc.Save(pathMap); wsDr model= XmlHelper.XmlToModelFile(pathMap); Relationships relationships= GetImg(copydirfullname); if (model.twoCellAnchorList.Count > 0) { //将图片路径,图片实体保存在图片位置表(wsDr)中 foreach (var item in model.twoCellAnchorList) { item.pic1.nvPicPr1.cNvPr1.img= relationships.Relationship.SingleOrDefault(a => a.Id == item.pic1.blipFill1.blip1.imgid).img; item.pic1.nvPicPr1.cNvPr1.imgUrl= relationships.Relationship.SingleOrDefault(a => a.Id == item.pic1.blipFill1.blip1.imgid).imgUrl; } } return model; } private Relationships GetImg(string copydirfullname) { string pathMap = Path.Combine(copydirfullname, "xl\\drawings\\_rels\\drawing1.xml.rels");//获取图片所在目录的xml.rels FileInfo fi = new FileInfo(pathMap); string newpathMap = Path.Combine(copydirfullname, "xl\\drawings\\_rels\\drawing1.xml");//转换为xml,不然不可读取 if (fi.Exists) { fi.MoveTo(newpathMap); } //代码可用 var doc = XDocument.Load(newpathMap); doc.Descendants().Attributes().Where(x=> x.IsNamespaceDeclaration).Remove();//移除大部分命名空间 foreach (var elem in doc.Descendants()) elem.Name= elem.Name.LocalName; doc.Save(newpathMap); Relationships model= XmlHelper.XmlToModelFile (newpathMap);//获取图片所在目录的xml string pathMap1 = Path.Combine(copydirfullname, "xl\\drawings\\drawing1.xml");//../media/image1.png 是以xl/drawings文件夹的基础上,而不是xl/drawings/_rels foreach (var item in model.Relationship) { string newpath = GetPath(Path.GetFullPath(pathMap1), item.Target);//获取 yyyyMMddHHssmm\xl\drawings target ../media/image1.png 转换为yyyyMMddHHssmm\xl\media\image1.png //保存路径,该路径在上传图片时使用 item.imgUrl = newpath; //以流形式读取图片,不占用图片 using (var stream = new FileStream(newpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete)) { item.img= Image.FromStream(stream); } } return model; } private string GetPath(string sourPath, string path) { string[] pathT = path.Split('/'); string newpath = sourPath; for (int i = 0; i < pathT.Length; i++) { if (pathT[i] == "..") { DirectoryInfo di= new DirectoryInfo(Path.GetDirectoryName(newpath)); newpath= di.Parent.FullName; } else { newpath= Path.GetFullPath(Path.Combine(newpath, pathT[i])); } } return newpath; }