+ Reply to Thread
Results 1 to 5 of 5

Convert HTML to Excel - page-break-before:always; not working

  1. #1
    Registered User
    Join Date
    01-18-2012
    Location
    HK
    MS-Off Ver
    Excel 2007
    Posts
    4

    Convert HTML to Excel - page-break-before:always; not working

    Hi all,

    I am trying to generate excel file from HTML page by MIME streaming of a php script
    Content-Type: application/vnd.ms-excel
    Content-Disposition: attachment; filename='testexcel.xls'
    ......

    It is working good and can be opened by Excel 2007 as expected.

    However, there is one problem that I cannot fix so far.

    I want to insert page break after some rows.
    I try to use the following HTML code at the line that I want to have a page-break in Excel.
    <p style="page-break-before:always">

    After opening the generated file in Excel 2007, the page-break line is not found and no page-break happen during printing.

    I had tried different ways of page-break tag e.g. <br style="page-break-before:always"> ....etc.
    Never have the page-break effect in all generated excel file.

    Can some one help me to fix this problem?

    Thanks

    Benny

  2. #2
    Registered User
    Join Date
    01-24-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Convert HTML to Excel - page-break-before:always; not working

    I was just looking into this issue this morning. I am writing an HTML to Excel export and I have been working on formatting the Excel so that the users do not need to fiddle with the settings before printing. My example below contains code to set the page orientation to landscape, set the scale to 70%, and to add in some page breaks. I have to admit that the documentation on the page breaks was very lacking. I ended up using information from several different sites and then playing with it a bit to get everything working.

    The part for the page breaks is contained in the "x:PageBreaks" tags. All you need to do is add an

    <x:RowBreak><x:Row>yourRowNumberHere</x:Row></x:RowBreak>

    entry for each break. I hope this helps!

    <html xmlns:v='urn:schemas-microsoft-com:vml'
    xmlns:o='urn:schemas-microsoft-com:office:office'
    xmlns:x='urn:schemas-microsoft-com:office:excel'
    xmlns='http://www.w3.org/TR/REC-html40'>
    <head>
    <style type="text/css">
    @page{mso-page-orientation:landscape;}
    td{text-align:center;}
    td.code{mso-number-format:\@}
    td.cost{mso-number-format:0\.00000}
    td.date{mso-number-format:mm\/dd\/yy \[$-F400\]h\:mm\:ss\\ AM\/PM}
    </style>
    <!--[if gte mso 9]><xml>
    <x:ExcelWorkbook>
    <x:ExcelWorksheets>
    <x:ExcelWorksheet>
    <x:WorksheetOptions>
    <x:Print>
    <x:ValidPrinterInfo/>
    <x:HorizontalResolution>1200</x:HorizontalResolution>
    <x:VerticalResolution>1200</x:VerticalResolution>
    <x:Scale>70</x:Scale>
    </x:Print>
    <x:Selected/>
    <x:DoNotDisplayGridlines/>
    <x:ProtectContents>False</x:ProtectContents>
    <x:ProtectObjects>False</x:ProtectObjects>
    <x:ProtectScenarios>False</x:ProtectScenarios>
    </x:WorksheetOptions>
    <x:PageBreaks>
    <x:RowBreaks>
    <x:RowBreak><x:Row>21</x:Row></x:RowBreak>
    <x:RowBreak><x:Row>50</x:Row></x:RowBreak>
    <x:RowBreak><x:Row>77</x:Row></x:RowBreak>
    <x:RowBreak><x:Row>105</x:Row></x:RowBreak>
    </x:RowBreaks>
    </x:PageBreaks>

    </x:ExcelWorksheet>
    </x:ExcelWorksheets>
    <x:WindowHeight>12780</x:WindowHeight>
    <x:WindowWidth>19035</x:WindowWidth>
    <x:WindowTopX>0</x:WindowTopX>
    <x:WindowTopY>15</x:WindowTopY>
    <x:ProtectStructure>False</x:ProtectStructure>
    <x:ProtectWindows>False</x:ProtectWindows>
    </x:ExcelWorkbook>
    </xml><![endif]-->
    </head>

  3. #3
    Registered User
    Join Date
    01-18-2012
    Location
    HK
    MS-Off Ver
    Excel 2007
    Posts
    4

    Red face Re: Convert HTML to Excel - page-break-before:always; not working

    Hi RockHydra,

    Thanks for your help.
    Your sample code seems working for page break formatting.
    I've manually cut-and-paste <head>...</head> section of your code into a MIME generated .xls text file and the page breaks are shown at rows as defined in the <head> section.

    I will try to incorporate the <head> section into my php report generation program and generate the xls report by MIME streaming.

    Will keep it posted on the outcome.

    Best Regards,

    Benny

  4. #4
    Registered User
    Join Date
    01-18-2012
    Location
    HK
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Convert HTML to Excel - page-break-before:always; not working

    Hi RockHydra,

    I've used your sample code and turned it into a php function with no. of report page and page height as parameters.
    With this function, the Excel report generation php program is working without fault and the pages are breaking in the generated Excel file as required.

    Your help and advice are much appreciated and I believe this solution will be very helpful for those who are looking for a similar solution.

    The php function is shown below
    //*****************************************************
    // Header for generating Excel file by MIME streaming
    // $nPage = No. of Page in the report
    // $hPage = Height of each Page
    //*****************************************************
    function ExcelHeader($nPage, $hPage)
    {
    ?>
    <html xmlns:v='urn:schemas-microsoft-com:vml'
    xmlns:o='urn:schemas-microsoft-com:office:office'
    xmlns:x='urn:schemas-microsoft-com:office:excel'
    xmlns='http://www.w3.org/TR/REC-html40'>
    <head>
    <style type="text/css">
    @page{mso-page-orientation:landscape;}
    td{text-align:center;}
    td.code{mso-number-format:\@}
    td.cost{mso-number-format:0\.00000}
    td.date{mso-number-format:mm\/dd\/yy \[$-F400\]h\:mm\:ss\\ AM\/PM}
    </style>
    <!--[if gte mso 9]><xml>
    <x:ExcelWorkbook>
    <x:ExcelWorksheets>
    <x:ExcelWorksheet>
    <x:WorksheetOptions>
    <x:Print>
    <x:ValidPrinterInfo/>
    <x:HorizontalResolution>1200</x:HorizontalResolution>
    <x:VerticalResolution>1200</x:VerticalResolution>
    <x:Scale>70</x:Scale>
    </x:Print>
    <x:Selected/>
    <x:DoNotDisplayGridlines/>
    <x:ProtectContents>False</x:ProtectContents>
    <x:ProtectObjects>False</x:ProtectObjects>
    <x:ProtectScenarios>False</x:ProtectScenarios>
    </x:WorksheetOptions>
    <x:PageBreaks>
    <x:RowBreaks>
    <?php
    for ($i=0; $i<$nPage; $i++) {
    echo "<x:RowBreak><x:Row>".($hPage*$i)."</x:Row></x:RowBreak>";
    }
    ?>
    </x:RowBreaks>
    </x:PageBreaks>
    </x:ExcelWorksheet>
    </x:ExcelWorksheets>
    <x:WindowHeight>12780</x:WindowHeight>
    <x:WindowWidth>19035</x:WindowWidth>
    <x:WindowTopX>0</x:WindowTopX>
    <x:WindowTopY>15</x:WindowTopY>
    <x:ProtectStructure>False</x:ProtectStructure>
    <x:ProtectWindows>False</x:ProtectWindows>
    </x:ExcelWorkbook>
    </xml><![endif]-->
    </head>

    <?php
    }

    Benny

    www.rightstation.com
    www.shippingatom.com
    Right Station
    Web Solution
    The first Cloud Logistics Solution in Asia.

  5. #5
    Registered User
    Join Date
    01-18-2012
    Location
    HK
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Convert HTML to Excel - page-break-before:always; not working

    Hi RockHydra,

    Your solution is good for generating Excel file with known page (row) break position.

    However, do you have any idea or sample code on how to create a page break if page break position is not known in advance.
    For example, how to insert a page break after a report footer so that next report header can be printed in a new page.
    Such page break will be useful for printing multiple section report headers in new pages when the no. of report row within each section is not known in advance?

    Thanks

    Benny

    www.rightstation.com
    www.shippingatom.com
    Right Station
    Web Solution
    The first Cloud Logistics Solution in Asia.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1