Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6922065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:21:31+00:00 2026-05-27T10:21:31+00:00

I am trying to set the page margins on an excel spreadsheet that is

  • 0

I am trying to set the page margins on an excel spreadsheet that is being generated by open xml sdk. I am not opening an excel document that already exists, it is generated from scratch. I am using the PageMargins class but am not sure how to attach this instance to the worksheet. The SDK productivity tool gives this code:

PageMargins pageMargins1 = worksheet.GetFirstChild<PageMargins>();
pageMargins1.Left = 0.45D;
pageMargins1.Right = 0.45D;
pageMargins1.Top = 0.5D;
pageMargins1.Bottom = 0.5D;

The GetFirstChild() function returns null. I also tried to do

worksheet.Append(pageMargins1);

but no luck.

Also using the code from this example: How change excel 2007 document orientation to landscape by OpenXML sdk
to set the page orientation does not work if creating the document from scratch. How do you add a PageSetup & PageMargin instance to the document?

Any one have knowledge of this SDK and knows how to use the margins or page setup class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:21:31+00:00Added an answer on May 27, 2026 at 10:21 am

    The order that things are appended to the worksheet is important. PageMargins needs to be appended before PageSetup, and they both need to be at the end after the SheetData. Also, all settings need to be set. I used this code:

    PageMargins pageMargins1 = new PageMargins();
    pageMargins1.Left = 0.45D;
    pageMargins1.Right = 0.45D;
    pageMargins1.Top = 0.5D;
    pageMargins1.Bottom = 0.5D;
    pageMargins1.Header = 0.3D;
    pageMargins1.Footer = 0.3D;
    worksheetPart.Worksheet.AppendChild(pageMargins1);
    
    PageSetup pageSetup = new PageSetup();
    pageSetup.Orientation = OrientationValues.Landscape;
    pageSetup.FitToHeight = 2;
    pageSetup.HorizontalDpi = 200;
    pageSetup.VerticalDpi = 200;
    worksheetPart.Worksheet.AppendChild(pageSetup);
    

    A neat trick to be used along with the sdk productivity tool is to rename the .xlsx file to .zip, then extract the contents. Then open /xl/worksheets/sheet.xml and compare the markup to the markup of an excel file created by excel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set a page that displays the visitor's IP. All the methods
For while I have been trying to set the Page Scaling of Excel page
I am trying to set up a page that will allow the user to
I am trying to set up a page on a wordpress site that displays
I am trying to set up so that my default page http://demo.liginsurance.com/ goes to
I'm trying to set up a page so that the navigation bar has a
I'm trying to set my default page to Index.html on an ASP.NET site running
im trying to set the height of two divs on my page, because my
I am trying to set up a web page where trusted users can upload
I have been trying to set the scroll of my page to a certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.