以编程方式在Excel中设置页面设​​置选项

时间:2021-12-01 10:55:21

I have a ASP.NET Web App which is copying a Report to an Excel Sheet by creating an HTML table and copying the contents. I want to fit the excel report into 1 page before firing the print option. This needs to be done programatically while I'm generating the Excel workbook. Please provide a solution to this asap.

我有一个ASP.NET Web应用程序,它通过创建HTML表并复制内容将报表复制到Excel表。我希望在激活打印选项之前将excel报告放入1页。这需要在我生成Excel工作簿时以编程方式完成。请尽快提供解决方案。

1 个解决方案

#1


Following is the code that I used to open an Excel workbook and print it with custom printer settings:

以下是我用来打开Excel工作簿并使用自定义打印机设置打印它的代码:

Dim xl As New Excel.Application
    xl.DisplayAlerts = False
    xl.Workbooks.Open("<FilePath>", 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)
    Dim sheet As Excel.Worksheet

    Dim ws
    Try
        For Each ws In xl.ActiveWorkbook.Worksheets
            ws.Select(Type.Missing)
            With ws.PageSetup
                .PaperSize = Excel.XlPaperSize.xlPaperA4
                .Orientation = Excel.XlPageOrientation.xlLandscape
                .Zoom = 80
                .BottomMargin = 0.25
                .LeftMargin = 0.25
                .RightMargin = 0.25
                .TopMargin = 0.25
                .FitToPagesWide = 1
            End With
            ws.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
        Next

    Catch exp As Exception
        MsgBox("Unable to setup printing properties for the sheet." & Chr(13) & "Check if you have printer installed on your machine.", MsgBoxStyle.OKOnly)

    Finally
        xl.Workbooks.Close()
        xl.Quit()
        While (System.Runtime.InteropServices.Marshal.ReleaseComObject(xl) > 0)
            ''do nothing
        End While
        xl = Nothing
    End Try

#1


Following is the code that I used to open an Excel workbook and print it with custom printer settings:

以下是我用来打开Excel工作簿并使用自定义打印机设置打印它的代码:

Dim xl As New Excel.Application
    xl.DisplayAlerts = False
    xl.Workbooks.Open("<FilePath>", 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)
    Dim sheet As Excel.Worksheet

    Dim ws
    Try
        For Each ws In xl.ActiveWorkbook.Worksheets
            ws.Select(Type.Missing)
            With ws.PageSetup
                .PaperSize = Excel.XlPaperSize.xlPaperA4
                .Orientation = Excel.XlPageOrientation.xlLandscape
                .Zoom = 80
                .BottomMargin = 0.25
                .LeftMargin = 0.25
                .RightMargin = 0.25
                .TopMargin = 0.25
                .FitToPagesWide = 1
            End With
            ws.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
        Next

    Catch exp As Exception
        MsgBox("Unable to setup printing properties for the sheet." & Chr(13) & "Check if you have printer installed on your machine.", MsgBoxStyle.OKOnly)

    Finally
        xl.Workbooks.Close()
        xl.Quit()
        While (System.Runtime.InteropServices.Marshal.ReleaseComObject(xl) > 0)
            ''do nothing
        End While
        xl = Nothing
    End Try