C # Page Setup when you print an Excel document

Source: Internet
Author: User

1, on the printed pagefacing, page width, page heightTo set

Reference source [1]

Using Excel = Microsoft.Office.Interop.Excel; Excel.Application tmpexcel = new Excel.applicationclass (); Excel.Workbook Tmpbook = TmpExcel.Workbooks.Open (Tmppath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,  Type.Missing, Type.Missing); Permission settings for the table, such as read-only, password, permissions
The following is the addition of a new sheetexcel.worksheet objsheet = (excel.worksheet) tmpbook. Worksheets.add (Type.Missing, Type.Missing, Type.Missing); objsheet. pagesetup.orientation = xlpageorientation.xllandscape;//page orientation Horizontal objsheet. Pagesetup.zoom = false; When printing page settings, must be set to False, the following two lines high, page width is valid objsheet. Pagesetup.fittopagestall = 1; Page High objsheet. Pagesetup.fittopageswide = 1; Page width objsheet. Pagesetup.zoom = 75;//The page is set when printing, scale objsheet. Pagesetup.topmargin = 0; The top margin is 0objsheet. Pagesetup.bottommargin = 0; The bottom margin is 0objsheet. PageSetup.LeftMargin = 0; The left margin is 0objsheet. Pagesetup.rightmargin = 0; The right margin is 0objsheet. pagesetup.centerhorizontally = true;//Horizontal Center

2. Printing options and printing documents [2]

Printing an Excel document is a common operation, but sometimes we encounter various printing needs, such as printing only one part of an Excel worksheet, or having a header on each page when printing, or printing more than 1 pages of all content on a worksheet to 1 pages, and so on, when we need to print the Excel Item to be set. This article is mainly about sharing how to use Excel components and C # to set some common Excel printing options and print Excel documents.

The following Excel worksheet contains a total of 17 rows and 20 columns of data:

Target: Print all the data on line 7th, 8 to one page, and print the header (header row).

Create a WinForm project that uses the following namespaces:

Using System;  Using System.Drawing.Printing;  Using System.Windows.Forms;  Using Spire.xls;     

Step 1 : Creates a new workbook object and loads the Excel document.

New Workbook (); Workbook. LoadFromFile ("sample.xlsx");   

Step 2 : Gets the first sheet of the Excel document and sets the print options.

Worksheet sheet = workbook. worksheets[0];

Here are a few common settings for printing options:

Set the print area/range:

"a7:t8"; 

Set the print header (header row):

"$1:$1"; 

Set the Excel worksheet to zoom to one page wide one page High:

11;

Here you can set their value to 0 or change the printing effect to meet different needs.

In addition, you can set the page orientation and print page size, etc.:

Set page orientation:

Sheet. Pagesetup.orientation = pageorientationtype.portrait;

To set the print page size:

Sheet. Pagesetup.papersize = PAPERSIZETYPE.PAPERA3;

Step 3 : Create a new PrintDialog object, set the dialog property and print the page range, and print the document.

PrintDialog dialog =NewPrintDialog ();d ialog. Allowprinttofile =True;d Ialog. Allowcurrentpage =True;d Ialog. Allowsomepages =true;d Ialog. Allowselection = true;d Ialog. Useexdialog = true;d Ialog. Printersettings.duplex = Duplex.simplex;dialog. Printersettings.frompage = 0;d Ialog. Printersettings.topage = 8;d Ialog. Printersettings.printrange = Printrange.somepages;workbook. PrintDialog = Dialog; PrintDocument PD = workbook. PrintDocument; if (dialog. ShowDialog () == DialogResult.OK) {PD. Print (); }

The following dialog box appears when you run the program:

Here I select Microsoft XPS Document Writer to print this Excel file as an XPS format, resulting in the following XPS files:

All code:

usingSystem;usingSystem.Drawing.Printing;usingSystem.Windows.Forms;usingSpire.xls;namespaceprint_excel_in_csharp{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {Workbook Workbook=NewWorkbook (); Workbook. LoadFromFile ("sample.xlsx"); Worksheet sheet= Workbook. worksheets[0]; Sheet. Pagesetup.printarea="A7:t8"; Sheet. Pagesetup.printtitlerows="$1:$1"; Sheet. Pagesetup.fittopageswide=1; Sheet. Pagesetup.fittopagestall=1; //sheet.            Pagesetup.orientation = Pageorientationtype.landscape; //sheet. Pagesetup.papersize = PAPERSIZETYPE.PAPERA3;PrintDialog Dialog=NewPrintDialog (); Dialog. Allowprinttofile=true; Dialog. Allowcurrentpage=true; Dialog. Allowsomepages=true; Dialog. Allowselection=true; Dialog. Useexdialog=true; Dialog. Printersettings.duplex=Duplex.simplex; Dialog. Printersettings.frompage=0; Dialog. Printersettings.topage=8; Dialog. Printersettings.printrange=printrange.somepages; Workbook. PrintDialog=Dialog; PrintDocument PD=workbook.            PrintDocument; if(dialog. ShowDialog () = =DialogResult.OK) {PD. Print (); }        }    }}

Reference Blog

1. brian0031. C # Print Page Setup (landscape, page width, page height), 2012-03.

2. E-iceblue. C # sets Excel print options and prints Excel documents ,2016-05.

Extended Reading

1. C # merging and splitting PDF files

2. C # Add a content control to a Word document

C # Page Setup when you print an Excel document

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.