How to set excel custom page size with c#

hossein tavakoli 471 Reputation points
2022-12-03T19:58:12.86+00:00

Hi dears,
I want to change (or create new excel file) page Height/Width in custom size for example 7cm*10cm.
I'm using Microsoft.Office.Interop.Excel.
How can I do it?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,221 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 48,856 Reputation points Microsoft Vendor
    2022-12-19T10:16:49.267+00:00

    Hi @hossein tavakoli , Welcome to Microsoft Q&A.

    Normally, if your paper size is special, you should add the corresponding format in the printer's paper format, and then select it when printing.

    The following is a code that can use c# to open the print pop-up box. After the pop-up box, you can select the printer, print paper, and preview the printed page.

    private void button1_Click(object sender, EventArgs e)  
    {  
        Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();  
      
        // Open the Workbook:  
        Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.Open(  
            @"C:\Users\Administrator\Desktop\123.xlsx",  
            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);  
      
        // Get the first worksheet.  
        // (Excel uses base 1 indexing, not base 0.)  
        Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];  
      
        // open print dialog  
        bool userDidntCancel =  
        excelApp.Dialogs[Microsoft.Office.Interop.Excel.XlBuiltInDialog.xlDialogPrint].Show(  
        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, 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, Type.Missing, Type.Missing);  
      
        // Cleanup:  
        GC.Collect();  
        GC.WaitForPendingFinalizers();  
      
        Marshal.FinalReleaseComObject(ws);  
      
        wb.Close(false, Type.Missing, Type.Missing);  
        Marshal.FinalReleaseComObject(wb);  
      
        excelApp.Quit();  
        Marshal.FinalReleaseComObject(excelApp);  
    }  
    

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.