dongsuiwo0279 2019-06-20 07:26
浏览 131

如果数据在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 :

enter image description here

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 :

enter image description here

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

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

  • 写回答

1条回答 默认 最新

  • duanliexi1052 2019-06-20 09:21
    关注

    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;
    }
    ?>
    

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

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行