Friday, March 23, 2012

Problems with report fitting to a page when exporting and then printing from Excel

I have various reports in both portrait and landscape and when I export to Excel although the page orientation works correctly the report does not print correctly in that the right hand side of the report gets chopped.

In the report definition I have all the margins set to 0.5cm and the page height set to 29.7cm and the width to 21cm for portrait and the other way around for landscape. In designing my report I have a report width 1cm less to take account of the margins i.e. 28.7 for a landscape report and 20cm for a portrait report. The report is defined in all this space as I have lines in my header and footer than span the whole report width.

If I am doing my calculations above correctly I cannot see why the report does not fit to a page when exporting to Excel. Printing directly from the browser or PDF works fine.

Thanks in advance for any help.

Excel has its own print mechanism. Do a print preview of the report in Excel before printing.

My guess would be that Excel is adding its own margins.

|||

The print margins in Excel are exactly the same as I defined in the report 0.5cm on all sides so that does not look like Excel is having any problems getting the margin information.

|||

Yes, you can call a com object from Microsoft.Office.Interop.Excel and redesign the workbook the example code is here:

yo need the file in Excel (*.xls) this is the "FinalFileName"

Imports:
Excel = Microsoft.Office.Interop.Excel

Code:

Dim app As New Excel.Application
Dim wb As Excel.Workbook
Dim fs As System.IO.FileStream
Dim writer As System.IO.BinaryWriter
fs = New System.IO.FileStream(FinalFileName, System.IO.FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
Dim writer As System.IO.BinaryWriter
fs.Close()
fs.Dispose()
app.DisplayAlerts = False
wb = appp.Workbooks.Open(Finalfile)
With CType(wb.Worksheets(1), Excel.Worksheet).PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.CenterHorizontally = True
.CenterVertically = True
.Draft = False
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
wb.PrintOut()
wb.Unprotect()
appp.Workbooks.Close()
appp.Quit()
fs.Close()
fs.Dispose()

sql

No comments:

Post a Comment