Print an xls file using Aspose Cells

Hello,

We are currently using Apsose Cells 4.4.0.0 version in our applications. Now wanted to use the same in a .NET bases web service. The web service recieves few files and these files should be printed using the required printer. To perform this task we are currently using EXCEL COM objects and we want to replace the COM object with Aspose.Cells however we could not find any provision to print a xls file with the version we have. Please help us in this regard.

For our printing tasks all the sheets available should be considered. The code we are currently using is as follows:

//Create a new excel object

Excel.Application MSExcel = new Excel.Application();

//Declare variable of excel workbook type

Excel._Workbook XLBook;

//Set the alerts to false, this would not display any alerts from excel

MSExcel.DisplayAlerts = false;

// Open the excel file

XLBook = MSExcel.Workbooks.Open(strPath, 0, true, 5, "", "",

true, 2, "", false, false, false, false, 1, false);

//Select all the excel sheets in the excel workbook

XLBook.Sheets.Select(null);

//Activate the entire workbook

XLBook.Activate();

//Declare variable of type excel worksheet

Excel._Worksheet wSheet;

//Assign all the selected sheets to the excel sheet variable

wSheet = (Excel._Worksheet) XLBook.ActiveSheet;

//The below lines of code are used to make sure all the contents of the excel file are displayed in a single page when converted to PDF

wSheet.PageSetup.PrintTitleRows = "";

wSheet.PageSetup.PrintTitleColumns = "";

wSheet.PageSetup.PrintArea = "";

wSheet.PageSetup.LeftHeader = "";

wSheet.PageSetup.CenterHeader = "";

wSheet.PageSetup.RightHeader = "";

wSheet.PageSetup.LeftFooter = "";

wSheet.PageSetup.CenterFooter = "";

wSheet.PageSetup.RightFooter = "";

wSheet.PageSetup.PrintHeadings = false;

wSheet.PageSetup.PrintGridlines = false;

wSheet.PageSetup.PrintComments = Excel.XlPrintLocation.xlPrintNoComments;

wSheet.PageSetup.CenterHorizontally = false;

wSheet.PageSetup.CenterVertically = false;

wSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlPortrait;

wSheet.PageSetup.Draft = false;

wSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperLetter;

wSheet.PageSetup.Zoom = false;

wSheet.PageSetup.FitToPagesWide = 1;

wSheet.PageSetup.FitToPagesTall = false;

//Print the excel file opened

XLBook.PrintOut(1, 9999, 1, false, ConfigurationSettings.AppSettings["PrinterName"].ToString(), true, 0, "");

//Save the workbook

XLBook.Saved = true;

//Close the excel workbook

XLBook.Close(false, "", false);

//Clean up all the objects created

MSExcel.Quit();

MSExcel = null;

XLBook = null;

Hi,

Thanks for considering Aspose.

Please use the latest versions of Aspose.Cells for .NET (e.g 4.7, 4.6 etc) as we have supported to print excel files (feature), we provide Worksheet.SheetToPrinter() method for the task.

Sample code:

//............
Workbook workbook = new Workbook();
workbook.Open("f:\\test\\MyFile.xls", FileFormatType.Default);
Worksheet worksheet = workbook.Worksheets[0];
worksheet.SheetToPrinter(stringprintername);
//............
And for setting page setup and printing options, please see the documentation topic in the section:
http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/page-setup-features.html
Thank you.

Amjad,

Thanks for the reply!

We are assuming that the sample code you have provided will print a particuar sheet (Sheets[0]) only. However if an excel file is having 3 sheets all these 3 sheets be printed at the same time, can you provide us an example on this regard?

Hi,

Well, yes, the method will print a single sheet at a time, I think you may try to loop through the sheets in the excel file and print them.

Sample code:

//...............
Workbook workbook = new Workbook();
workbook.Open("f:\\test\\MyBook1.xls", FileFormatType.Default);
Worksheet worksheet;
for (int i = 0; i < workbook.Worksheets.Count; i++)
{


worksheet = workbook.Worksheets[i];
worksheet.SheetToPrinter(strprintername);

}
//................
Thank you.
Hey guys,
Is there any way to set in the code that the default printing preference would be to print the entire workbook and not just the active sheet?

Thanks,
Sarit

Hi,

We obsoleted the older Sheet to Printer and Sheet to Image API. In the latest versions, we do provide WorkbookRender and SheetRender API. You can use WorkbookRender API to print entire workbook for your need, see the document for your reference:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/printing-workbooks.html

Thank you.