How to get image from excel ? - CodingPot | Programming Blog - ASP.NET, C#, VB.NET, AngularJs, SQL Server, AJAX, JQuery Tutorials.

Thursday 28 September 2017

How to get image from excel ?

Hello Friends, How are you? Today we are going with new topic, in previous post we have seen how to read data from excel. We have seen read single data from once cell, second read multiple values from one cell and third read user selected any one value.It’s easy to read data from excel as a text but image get is difficult. In software development may be excel used to read data from excel and store in project database. Developers are getting so difficult when come how to get image from excel. In today’s post we give innovative solution for it to developer.

How to get image from excel

Step 1: Open visual studio which version you has, may be you has latest version of visual studio like (visual studio 2015) but it’s doesn’t matter which version you use for do this thing. There one another editor is also available for done this “How to get image from excel” that is a “visual studio code”. Most of visual studio code is used when you develop the application using javascript, Jquery or Angularjs. You can update the latest version of visual studio code, there is notify for update visual studio code.

Step 2: After opened visual studio or visual studio code, you must create one project. It is not necessary to create only project you can create as website. you can create empty website and do your work. The difference between “New Project” and “New Website” that when you create project then it’s create one solution file but you create website then it do not create any solution file. So you can create any one from both with project or website name (“GetImageFromExcel”).

Step 3: If you has created new project or website then go ahead, In visual studio or visual studio code, if we want to do any document related coding then we must specify their library in that one page or main page. If you use visual studio then you must specify the “namespace” of that library, if you use visual studio code then must specify the “library name” in module file. Now we have used visual studio so first of all we must install the package library is “EPPlus.4.1.0” for do excel related work using NuGet Package Manager. There are different versions are available you can install latest version of package.


Step 4: The required package (“EPPlus 4.1.0”) is install in project library, now we need create one excel folder because in this folder we put our excel file. In this excel file multiple images are available and we will get that images from excel and put in one another folder like (FinalData). We assume all images name is something match like (item1, item2) because we can easily get that image. If the all images name is different then we must write code for replace old name to new name and all images name make as simple.

Step 5: Now all the required folders are created, so we have to create (.aspx) file, when you create (.aspx) file then it create automatically (.aspx.cs) file. You know that (.aspx) file is use for UI purpose and (.aspx.cs) file use for our logic, coding, etc. In (.aspx.cs) file we will write the code for “GetImageFromExcel”. We must add namespaces in (.aspx.cs) file that’s are (“using System.IO” and second is “using OfficeOpenXml”) and then start the coding for “Get Image from excel”.

Code :

//... Read Excel from project folder (Excel)... var fi = new FileInfo(@"D:BlogProjectsGetImageFromExcelGetImageFromExcelExcelGallary Image.xlsx"); //... Excel package... using (var package = new ExcelPackage(fi)) { var workbook = package.Workbook; //... Read first worksheet... var worksheet = workbook.Worksheets.First(); //... Read Only Data Rows, Not a blank space.. int noOfRow = worksheet.Dimension.End.Row - 1; for (int a = 0; a < noOfRow; a++) { //... In Excel file we assume the all images names like this (item1,item2..) var pic = worksheet.Drawings["item " + (a + 1)] as ExcelPicture; if (pic != null) { //... Get Excel file name... string imgName = pic.Name; String path = HostingEnvironment.MapPath("~/ExcelImages/"); if (!System.IO.Directory.Exists(path)) { //Create directory if itdoesn't exist System.IO.Directory.CreateDirectory(path); } string imgPath = Path.Combine(path, imgName + (".jpeg")); //... Save image in ExcelImsges folder... pic.Image.Save(imgPath); } } }

Step 6: In above code, first of all we have got the excel file from project folder, then second is read as workbook of excel and then we have read first worksheet from that workbook because images are available in first worksheet. Third one is count the total no. of row that the fulfill cells from first worksheet but we didn’t assume first row as data because in first row we have write only titles. On fourth one is we start loop while count of no of rows and read image file by image name like (item1, item2..) because we have set name of all images are same. Fifth one we have check if image is read then get image name in one variable then get the mappath of folder there we put the images then in six one we combine the path and image name and in last we have save the images in readable path, do the same and see result.

No comments:

Post a Comment