Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Mon Aug 19, 2019 1:25 pm

Dear Iceblue Team,

I am working currently on a WPF project and need to print a selected worksheet out of my workbook, if i press print, it will print all sheets in the workbook. Would be nice, if you could help me out :) Also i can`t change the sheet alignment between Horizontal/Vertical.

Code: Select all
 Workbook workbook = new Workbook();
                workbook.LoadFromFile(filename);
                PrintDialog dialog = new PrintDialog();
                dialog.UserPageRangeEnabled = true;
                PageRange rang = new PageRange(sheetnumber);
                dialog.PageRange = rang;
                PageRangeSelection selection = PageRangeSelection.SelectedPages;
                dialog.PageRangeSelection = selection;
                PrintDocument pd = workbook.PrintDocument;
                if (dialog.ShowDialog() == true)
                {
                    pd.Print();
                }


Your sincerly
Monika

MonikaK
 
Posts: 5
Joined: Mon Aug 19, 2019 1:14 pm

Tue Aug 20, 2019 3:40 am

Hi Monika,

Thanks for your inquiry.
To reach your goal, you could copy the sheet you want to a new Excel document, then print the new file.
Below is the code for your kind reference.
Code: Select all
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"18613.xlsx");
            //get the sheet
            Worksheet desiredSheet = workbook.Worksheets[2];
            //copy the sheet you want to print to a new Excel.
            Workbook newBook = new Workbook();
            newBook.Version = ExcelVersion.Version2010;
            newBook.Worksheets.Clear();
            newBook.Worksheets.AddCopy(desiredSheet);

            //set landscape
            newBook.PrintDocument.PrinterSettings.DefaultPageSettings.Landscape = true;
            //print the new file
            newBook.PrintDocument.Print();


Any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Aug 20, 2019 8:57 am

Dear Betsy,
Thanks for your help, it works perfectly :)

Best regards,
Monika

MonikaK
 
Posts: 5
Joined: Mon Aug 19, 2019 1:14 pm

Tue Aug 20, 2019 9:01 am

Hi Monika,

Thanks for your feedback.
Any question, welcome to contact us. :D

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.XLS