2009-03-09 8 views
2

Ich habe eine ASP.NET Web App, die einen Bericht in ein Excel-Blatt kopiert, indem sie eine HTML-Tabelle erstellt und den Inhalt kopiert. Ich möchte den Excel-Bericht in 1 Seite anpassen, bevor die Druckoption ausgelöst wird. Dies muss programmgesteuert erfolgen, während ich die Excel-Arbeitsmappe erzeuge. Bitte stellen Sie so schnell wie möglich eine Lösung bereit.Programmgesteuertes Festlegen von Optionen für das Seiten-Setup in Excel

Antwort

4

Es folgt der Code, den ich verwenden, um eine Excel-Arbeitsmappe zu öffnen und es mit benutzerdefinierten Druckereinstellungen drucken:

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 
Verwandte Themen