Free Support Forum - aspose.com

How to display print preview and then print the excel

How to display print preview and then print the excel?

Hi,


Can you elaborate your requirements please? Please tell us in detail what you want to achieve, so that we can assist you further.

Thanks

I want to achieve the print preview function,but i don’t know how to do,please give me an example.

Hi,

Are you using Aspose.Cells for .Net product? The options as offered by MS-Excel’s Print Preview such as Page Setup, Print Options, etc. can be found in the undermentioned articles:

Setting Print Options
Setting Page Options

You can also find the Aspose.Cells for .Net programming guide at:
Developers Guide

If you still want more clarifications, please provide us with your sample Excel file and Screenshots of what you actually need. We will try our best to assist you in getting what you need.

Thanks and best regards,

MS-Excel'API sample code:

object Nothing = System.Reflection.Missing.Value;

xlWorkbook = xlApp.Workbooks.Open(strFilePath, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Worksheets[strSheetName];
xlWorksheet.PrintPreview(true);
xlWorkbook.Close(oMissing, oMissing, oMissing);

Hi,


I am sorry but, at present, Aspose.Cells does not provide any user interface for Print Preview of the workbook. For printing your workbook/worksheet, you can follow the following lines of code:

//--------------------- Printing Code Start ---------------------//

//open a workbook that is to be printed
Workbook workbook = new Workbook(“C://Book1.xls”);

//Get the worksheet to be printed
Worksheet worksheet = workbook.Worksheets[0];

//Apply different Image / Print options.
Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();

//Set the Printing page property
options.PrintingPage = PrintingPageType.Default;

//Render the worksheet
SheetRender sr = new SheetRender(worksheet, options);

//send to printer
sr.ToPrinter(strPrinterName);

//--------------------- Printing Code End ---------------------//

To get your printer name, you can proceed as follow:

System.Drawing.Printing.PrinterSettings printSettings = new System.Drawing.Printing.PrinterSettings();
string strPrinterName = printSettings.PrinterName;

I hope this helps you in achieving what you want. Please feel free to contact us for any further query.

Thanks and Best Regards

I follow your code but it doesn’t work.

I Write the codes as follow :
System.Drawing.Printing.PrinterSettings printSettings = new System.Drawing.Printing.PrinterSettings();
string strPrinterName = printSettings.PrinterName;
However I get error message: Can't get default printer name
How should I do?

Hi,


Thank you for using Aspose.Cells.

The PrinterSettings object retrieves the printer name that has been set as default on the system. Please set the printer installed on your system as default printer, and this code will work. You can do so by going to the properties of your printer in the “Devices and Printer” section as you can see in attached image.

You can see I setup default printer and still encounter “Printer Not Found” issue. My OS is Win 7 and .Net is 2010.

I follow the article ( http://www.c-sharpcorner.com/UploadFile/mahesh/printerslisting06072007092859AM/printerslisting.aspx )

to get print name, however I get those printer’s names which are local not network printers.

My code as follow which it is work, but I am not satisfied.

sr.ToPrint("\copyg-211\5F-A-XEROXC-1")

Hi,


Thank you for your feedback.

Can you tell us if you can access your network printer from your computer with any other software that you use? There is an article about getting the network printer’s address HERE that may be useful for you. The code is like as follow:
using System.Drawing.Printing;
string GetDefaultPrinter()
{
PrinterSettings settings = new PrinterSettings();
foreach (string printer in PrinterSettings.InstalledPrinters)
{
settings.PrinterName = printer;
if (settings.IsDefaultPrinter)
return printer;
}
return string.Empty;
}

Let us know your feedback.

I use PrintDialog to get printer name.

Dim dlg As New PrintDialog()
Dim doc As New System.Drawing.Printing.PrintDocument()

Dim result As DialogResult = dlg.ShowDialog()

If (result = DialogResult.OK) Then
dlg.PrinterSettings.PrinterName
End If

I test your code and it is work to get default printer name. Thanks a lot.

Hi,


Thank you for the feedback


The provided code gives only the default printer because there is an IF check in it for this purpose as follow:

if(settings.IsDefaultPrinter)
return printer;

Anyways, it’s good to know that your problem is solved now. Feel free to contact us if you have any other query. We will try our best to help you.

is Aspose.Cell support show printpreview?


Hi Pyntia,


Thank you for contacting Aspose support.

Please note, the SheetRender class works as Print Preview with it’s default settings, that means, you can render the Worksheet to images as they are displayed in the Print Preview of the Excel application. Please check the following piece of code for better elaboration. In case I have misunderstood your requirements then please provide more details.

C#

Workbook srcworkbook = new Workbook(“D:/input.xlsx”);
SheetRender render = new SheetRender(srcworkbook.Worksheets[0], new ImageOrPrintOptions());
for (int i = 0; i < render.PageCount; i++)
{
render.ToImage(i).Save(“D:/” + i + “.png”);
}

hi


please use attachment for test.

the attach file is create by aspose.cells, use your sample code to output print preview, there has 23 pages, but in excel application, there only has 19 pages.

file render in excel

file render by SheetRender(aspose)

i think the SheetRender’s pagesize is not exactly equals to Excel’s pagesize.

please check it. thanks.

Hi,

Thanks for your posting and using Aspose.Cells.

I have checked your test.xlsx on Microsoft Excel 2013 and it showed me 24 pages in a Print Preview. Then I saved it as PDF and it also showed 24 pages. I then converted it to PDF using Aspose.Cells and it again showed 24 pages. I have attached the screenshot for your reference.

Strange ah


in my excel application, there only 19 pages. on god, why?




i use aspose.cell 8.5.2


my software envirement:
1, win10 x64 professional
2, office2013 x64

Hi Pyntia,


I believe the difference of Print Preview is because we are viewing different spreadsheets or you have changed the orientation of the view before uploading it here for our review. Please check the attached snapshot that you have shared earlier, it shows that the paper orientation is Landscape. However, when we viewed the spreadsheet on our side (also attached) the paper orientation is Portrait. Moreover, I think you probably have different paper size set. Please check if it is 8.5x11 inches in your Excel application.