Free Support Forum - aspose.com

Copy range of cells and save it as image

Hi,

Is there a way to copy a range of cells from a worksheet and save them as an image?

-Pload

Hi Pload,

We will check the feasibility and look into the feature if we can support it for your need.

Thank you.

Hi,

Well, I think as a wokaround you may achieve it. Kindly check the following code and consult it:

// Create a Workbook object
Workbook workbook = new Workbook();
// Open the Excel file
workbook.Open("d:\\test\\book1.xls");
// Get the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Add a worksheet to the workbook.
int i = workbook.Worksheets.Add();
Worksheet mysheet = workbook.Worksheets[i];
//Name the sheet.
mysheet.Name = "MySheet";
// Create a named range of the Cells
Range range1 = worksheet.Cells.CreateRange("A1", "C9");
//Create another range of cells in the new sheet.
Range range2 = mysheet.Cells.CreateRange("A1", "C9");
// Copy the data with formatting of the range to the second range.
range2.Copy(range1);
mysheet.AutoFitColumns();
//Take the image of the sheet.
Bitmap bitmap = mysheet.SheetToImage();
bitmap.Save(@"d:\rangeimage.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
//Now delete the newly added sheet.
workbook.Worksheets.RemoveAt(i);
// Save the modified Excel file in default (Excel 2003) format
workbook.Save("d:\\test\\range_copied_outbook.xls",FileFormatType.Default);

Thank you.

Hi Amjad,

Thanks for your reply. I tried your sample code and I was able to copy the required portion on to a seperate sheet, but when I try to convert it to an image I am getting nullreference exception. I guess its because of the formulas that I have on the cells that am converting. Attached is a template of sheet that I want to convert to image.

-Pload

Hi Pload,

Well, I tried the conversion (sheet2image) using you template sheet and i don't find the problem / error you have mentioned.

Following is my testing code and attached is the output image file.

Workbook book = new Workbook();
book.Open("d:\\test\\range_copied_outbook1.xls");
Worksheet sheet = book.Worksheets[0];
Bitmap bitmap = sheet.SheetToImage();
bitmap.Save(@"d:\2sheetbitmap.png",System.Drawing.Imaging.ImageFormat.Png);

Which version you are using? I am using 4.4.3.15 (latest fix).

Thank you.

Hi Amjad,

I am using 4.4.3.8 version of Aspose.Cells that you posted couple weeks ago. Can you give me the latest version and I will try it out again.

Thanks,

-Pload

Hi Amjad,

I just took the latest version from the SheetToImage thread and it works now. Thanks for all your help. I really appreciate it.

-Pload