真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

C#winform使用NOPI讀取Excel讀取圖片

需求:在Winform使用NOPI做導(dǎo)入時(shí)候,需要導(dǎo)入數(shù)據(jù)的同時(shí)導(dǎo)入圖片。

專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)福田免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

雖然代碼方面不適用(我好像也沒仔細(xì)看過代碼),但是感謝大佬給了靈感http://www.wjhsh.net/IT-Ramon-p-13100039.html,將excel后綴修改成Zip,解壓,在其中找到圖片的xml位置信息,以及對應(yīng)的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); }//創(chuàng)建緩存文件夾
            string copyAfterFullName = Path.Combine(modelExlPath, copyAfterFileName + copyAfterFileNameExt);//移動(dòng)到該目錄并改為壓縮包
            string copyDirFullName = Path.Combine(modelExlPath, copyAfterFileName);//解壓后的文件夾位置

            OpenFileDialog openfile= new OpenFileDialog();
            openfile.Filter= "導(dǎo)入Excel(*.xls,*.xlsx)|*.xls;*.xlsx";
            openfile.FilterIndex= 0;
            openfile.RestoreDirectory= true;
            openfile.Title= "導(dǎo)入文件路徑";
//openfile.ShowDialog();
            if (openfile.ShowDialog() != DialogResult.OK)
            {
return;
            }

           Common.ShowWaitForm();//正在加載..窗口
            FileInfo fi1 = new FileInfo(openfile.FileName);
            fi1.CopyTo(copyAfterFullName);//移動(dòng)文件,并修改稱為yyyyMMddHHssmm.zip

            try
            {
                wsDr wsDrModel= null;
if (!Directory.Exists(copyDirFullName)) { Directory.CreateDirectory(copyDirFullName); }
//解壓到當(dāng)前文件夾
                if (SharpZip.UnpackFiles(copyAfterFullName, copyDirFullName) == false)//yyyyMMddHHssmm.zip為文件夾yyyyMMddHHssmm
                {
                    Common.ShowErrorDialog("導(dǎo)入失敗!");
                    LogHelper.Instance.Error("產(chǎn)品導(dǎo)入失敗," + copyDirFullName + "自解壓失??!");
return;
                }
else
                {
                    wsDrModel= GetImgLoaction(copyDirFullName);//讀取excel圖片信息,對應(yīng)位置,對應(yīng)的絕對路徑,對應(yīng)的圖片實(shí)體
                }

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("導(dǎo)入失?。?/span>");
return;
                }
                LoadList();//刷新表格
            }
catch (Exception ex)
            {
               Common.ShowErrorDialog("導(dǎo)入錯(cuò)誤!" + 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屬性中的情況只能手動(dòng)指定了
            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)
            {
//將圖片路徑,圖片實(shí)體保存在圖片位置表(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");//轉(zhuǎn)換為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文件夾的基礎(chǔ)上,而不是xl/drawings/_rels
            foreach (var item in model.Relationship)
            {
string newpath = GetPath(Path.GetFullPath(pathMap1), item.Target);//獲取 yyyyMMddHHssmm\xl\drawings target ../media/image1.png 轉(zhuǎn)換為yyyyMMddHHssmm\xl\media\image1.png
//保存路徑,該路徑在上傳圖片時(shí)使用
                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;
        }

分享題目:C#winform使用NOPI讀取Excel讀取圖片
路徑分享:http://weahome.cn/article/dsoioji.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部