Printing a worksheet

I'm working through the tech knowledge base on this one. I need to print the worksheet, but when I instantiate the sheet render

sheetrender sr = new SheetRender(sheet, printoptions);

I get this error:

Error 3 Argument 1: cannot convert from 'Aspose.Cells.GridDesktop.Worksheet' to 'Aspose.Cells.Worksheet' C:\Users\U0116802\Desktop\ProjectX\ProjectX\Aspose\GridView.cs 389 46 ProjectX

This is very similar to your example in the knowledge base.

Here's the entire function.

private void Printme(string ThePrinter)
{
Worksheet sheet = this.dgvMain.GetActiveWorksheet();

ImageOrPrintOptions printoptions = new ImageOrPrintOptions();

printoptions.PrintingPage = Aspose.Cells.PrintingPageType.Default;
SheetRender sr = new SheetRender(sheet, printoptions);
sr.ToPrinter(ThePrinter);

}

I've tried to cast the sheet to a (Aspose.Cells.Worksheet) and that didn't work.

As always, Thank you, guys for your very quick assistance!!

Hi,

Please see Aspose.Cells for .NET and Aspose.Cells for GridDesktop are two different components.

They might have similar classes but you must know that you are using which.

SheetRender expects a Worsheet object which is from Aspose.Cells namespace and not from Aspose.Cells.GridDesktop namespace.

You must use full naming to resolve conflicts.

These are two different objects, you cannot type cast one into anther.

Aspose.Cells.Worksheet
Aspose.Cells.GridDesktop.Worksheet

Ok, that’s all well and good, but how to I get the Aspose.Cell.worksheet object from a Desktop workbook object?

Hi,

You will first export your GridDesktop workbook into memory stream and then you will create Aspose.Cells Workbook from the resultant memory stream.

Please see the following code and the comments for more help how to achieve this.

C#


//First export your griddesktop workbook into memory stream

MemoryStream ms = new MemoryStream();

gridDesktop1.ExportExcelFile(ms);


//Then create aspose.cells workbook from memory stream

ms.Position = 0;

Workbook workbook = new Workbook(ms);


//And now get your desired worksheet

Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];

Perfect! Thank you!

Hi,

Please also try the Offline C# or VB.NET Demos for more help.

These demos are available for both Aspose.Cells for .NET and Aspose.Cells for GridWeb.

Please visit this link.

Aspose.Cells for .NET - Demos