如果数据在php Excel中是分开的,如何使数据自动移动到新页面? [英] How do I make a data automatically move to a new page if the data is separate in the php excel?

查看:105
本文介绍了如果数据在php Excel中是分开的,如何使数据自动移动到新页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有动态数据.所以我的数据可以改变 除此之外,我还有一个无法分离的数据签名. 此签名数据必须在同一页面上.看到此图中的红色标记:

I have dynamic data. So my data can change besides that I also have a data signature that cannot be separated. This signature data must be on the same page. See the red mark in this image :

它是一个单位,不能分开.签名数据 必须在同一页面上

It is a unit and cannot be separated. Signature data must be on the same page

我的问题是因为我的数据是动态的.这使得签名数据的位置可以位于任何位置.参见下图:

My problem is because my data is dynamic. This makes the position of the signature data can be located in any position. See image below :

因为我的数据增加了,所以校长在签名数据中的位置是独立的

Because my data increases, the position of the headmaster in the signature data is separate

我如何制作签名数据(见图1),如果数据分开,红色标记会自动移到下一页?

How do I make signature data (see picture 1), which red marks automatically move to the next page if the data is separate?

推荐答案

您可以使用以下方式设置手动分页符:

You can set manual page breaks with something like this:

<?php
//All lines are written from the sheet code at this moment 
//The code will insert a page break and the repeated header 

//Page margins
$sheet->getPageMargins()->setTop(0.5);
$sheet->getPageMargins()->setRight(0.75);
$sheet->getPageMargins()->setLeft(0.75);
$sheet->getPageMargins()->setBottom(1);

//Use fit to page for the horizontal direction
$sheet->getPageSetup()->setFitToWidth(1);
$sheet->getPageSetup()->setFitToHeight(0);

$headerItems = array(); //add your header items here as array
$headerRowsHeight = 0;// calculated height of header and images of the top
$rowCounter = 100; //get last written row

//add (other) modifier of page hight here

$pageHeight=25+50 + $headerRowsHeight; //Current used page height Header + Footer + $headerRowsHeight
$reset = $pageHeight; //If you will have the firstpage diffrent change reset value and/or pageheight
$pageMaxHeight = 980 ; //Maximale page height DIN A4 arround this
$pageClearance = 15; //Clearance of footer

//Iterate trough all written lines
for ($row = 1; $row <= $rowCounter; ++$row) {
    $height=15.1; //standard row height

    //get the row height 
    $dim = $sheet->getRowDimension($row)->getRowHeight();

    //get special cell heights (non standard)
    if($dim != -1){
        $height=$dim;
    }

    //add height for line to pageheight = get current used space
    $pageHeight = $pageHeight + $height;

    //Check if the space is still in the range of page 
    $leftOverSpace = $pageMaxHeight-$pageHeight;

    //Change $pageClearance  to your preferd space before footer
    if( $leftOverSpace < $pageClearance){
        //Set pagebraek
        $sheet->setBreak('A'.$row, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::BREAK_ROW);
        //Reset page height
        $pageHeight=$reset;
        //Add page header to new page
        createHeader($sheet, $row+1, $headeritems);
    }
}

//Creates a header for every page
function createHeader($sheet, $row, $texts){
    $count = $row;
    // Iterate trough the header text array
    foreach($texts as $text){
        //Insert a new line with header
        $sheet->insertNewRowBefore($count, 1);

        //Do your header stuff here
        $count++;
    }
    //Add two lines after the header
    $sheet->insertNewRowBefore($count, 2);
    //Return row number
    return $count;
}
?>

不要认为计算不是100%准确的,而必须根据您的代码进行调整.

Place not that the calculation is not 100% exact and that you must adapt this to your code.

这篇关于如果数据在php Excel中是分开的,如何使数据自动移动到新页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆