如果数据在php Excel中是分开的,如何使数据自动移动到新页面?

时间:2019-06-20 07:26:57

标签: php excel phpexcel spreadsheet phpspreadsheet

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

enter image description here

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

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

enter image description here

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

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

1 个答案:

答案 0 :(得分:0)

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

<?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%准确的,而必须根据您的代码进行调整。