gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:25:30 25 4
gpt4 key购买 nike

我有一个 ASP.NET Web 应用程序,它通过创建 HTML 表格并复制内容将报告复制到 Excel 工作表。

我想在触发打印选项之前将 Excel 报告放入一页。这需要在我生成 Excel 工作簿时以编程方式完成。

最佳答案

以下是我用来打开 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

关于c# - 在 Excel 中以编程方式设置页面设​​置选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/625781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com