We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Read image from Excel

Hi,

Aspose.CELL is really a powerful .Net component!! Now i have a question that how to read image(s) from an excel file? as our users may put the product pictures into excel and i want to get the image out then save them to local drive.

Your kindly response is highly appreciated.

Hi,

Please see the following sample code for your need that will guide you how to extract an image from an Excel file worksheet and then save to your local drive path.

Sample code:
Workbook workbook = new Workbook(“e:\test\Book1.xls”);
//Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
//Get the first Picture in the first worksheet
Aspose.Cells.Drawing.Picture pic = worksheet.Pictures[0];
//Set the output image file path
string fileName = “e:\test2\myextractedpic.Jpg”;
MessageBox.Show(pic.ImageFormat.ToString()); //It is Jpg (you may evaluate the image format before specifying the image path)

//Create the file stream
FileStream file = File.Create(fileName);
byte[] data = pic.Data;
//Save the image file
file.Write(data, 0, data.Length);


Thank you.

Hi,

Moreover, you may simply use Shape.ToImage() method to save the image from the Worksheet, see the other sample code segment here that you may alternatively use:

Sample code:

Workbook workbook = new Workbook(“e:\test\rrpichw.xls”);
//Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
//Get the first Picture in the first worksheet
Aspose.Cells.Drawing.Picture pic = worksheet.Pictures[0];
//Set the output image file path
string fileName = “e:\test2\myextractedpic.Jpg”;
MessageBox.Show(pic.ImageFormat.ToString()); //It is Jpg (you may evaluate the image format before specifying the image path)


ImageOrPrintOptions printoption = new ImageOrPrintOptions();
printoption.ImageFormat = ImageFormat.Jpeg;
pic.ToImage(fileName, printoption);


Thank you.

Thanks for your replying.

I am using the old version as 4.8. Dose it have the some function?

Please advise,

Hi Team, I just figured it out.

Thank you all so much!!

Hi,

If you are using older version v4.8.x, I think you may use my first approach (as I pasted the sample code in one of my previous post in this thread). The method Shape.ToImage() is a new method.

Thank you.

Hi Team,
I need to save a pivot table from excel to png image into local drive. How to achieve this.
I can see code samples for Pictures or saving overall Excel sheet. However I need code to copy the Pivot table into image into local drive. How to achieve this.
I found all other examples, nothing suits my requirement.

Regards,
Mamtha.A.C.D.

@HAREEM_HCL_COM,

Thanks for your query.

Well, you may devise an approach (as following) to take an image of the pivot table in the worksheet for your needs. You got to refresh and calculate PivotTable data before rendering to image file format. See the following code segment for your reference, please refer to it and write/update your own code accordingly for your needs:
e.g
Sample code:

        PivotTableCollection pivotTables = worksheet.getPivotTables();
  		PivotTable pivotTable = pivotTables.get(0);//Get the first PivotTable
//Add the following lines to refresh data and calculate data.
                                    pivotTable.refreshData();
                                    pivotTable.calculateData(); 
  		CellArea pivotArea = pivotTable.getTableRange1();
  		System.out.println("Area:: pivot table area :: " + pivotArea);
	            //Get the first page row/column.
        int strow = pivotArea.StartRow;
        int stcol = pivotArea.StartColumn;

        //Get the counts of rows/columns upto the end
        int numrows = pivotArea.EndRow - strow;
        int numcols = pivotArea.EndColumn - stcol;

        String sarea = CellsHelper.cellIndexToName(strow, stcol);
        String earea = CellsHelper.cellIndexToName(pivotArea.EndRow, pivotArea.EndColumn);

        //Set the printable area in the PageSetup for the sheet.
        worksheet.getPageSetup().setPrintArea(sarea + ":" + earea);
  			worksheet.getPageSetup().setLeftMargin(0.0);
  			worksheet.getPageSetup().setRightMargin(0.0);
  			worksheet.getPageSetup().setTopMargin(0.0);
  			worksheet.getPageSetup().setBottomMargin(0.0);
  			ImageOrPrintOptions options = new ImageOrPrintOptions();
  				options.setImageFormat(ImageFormat.getPng());
  			SheetRender sr = new SheetRender(worksheet, options);
  			sr.toImage(0, "out1.png");

Hope, this helps a bit.