简体   繁体   English

PHPExcel - 复制列范围

[英]PHPExcel - copy range of columns

I have Excel file where I need to expend space by duplicating excat column range. 我有Excel文件,我需要通过复制excat列范围来扩展空间。

在此输入图像描述

I upload a part of that file that can be seen in a picture. 我上传了一张可以在图片中看到的文件的一部分。 I need to copy row from A16 to H16 and set it right below (A17 to H17) so it would automatically move other columns below. 我需要将行从A16复制到H16并将其设置在下面(A17到H17),这样它会自动移动到下面的其他列。

This is one of examples I hvae tried. 这是我尝试过的一个例子。 None of them work. 他们都没有工作。

   $appPath = $this->container->getParameter('kernel.root_dir');
        $webPath = realpath($appPath . '/../web/uploads/statements/the-file.xlsx');
    $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($webPath);

        $phpExcelObject->getActiveSheet()->insertNewRowBefore(16,16);
        $phpExcelObject->getActiveSheet()->duplicateStyle($phpExcelObject->getActiveSheet()->getStyle('A16'), 'A16:H16');

    $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel2007');
    $response = $this->get('phpexcel')->createStreamedResponse($writer);
    $dispositionHeader = $response->headers->makeDisposition(
        ResponseHeaderBag::DISPOSITION_ATTACHMENT,
        'the-file.xlsx'
    );
    $response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
    $response->headers->set('Cache-Control', 'maxage=1');
    $response->headers->set('Content-Disposition', $dispositionHeader);

    return $response;

Api call works, to be exact, it donwloads that file with updated edits but the part with copying rows doesn't work.. Api调用工作,确切地说,它使用更新的编辑来下载该文件但是复制行的部分不起作用..

Looking at the parameters and the example on PhpExcel - How insert the same row after row N? 查看PhpExcel上的参数和示例- 如何在行N之后插入相同的行? I would assume your parameters are incorrect, I think insertNewRowBefore() has parameters insert point and number of rows, so this should be 16 and 1 in your case. 我认为你的参数是不正确的,我认为insertNewRowBefore()有参数插入点和行数,所以在你的情况下这应该是16和1。 Also the duplicateStyle() call should be with the rows you want to update and not the range of cells - I've put 'A15', but this may need to be tweaked. 另外, duplicateStyle()调用应该是你想要更新的行,而不是单元格的范围 - 我已经把'A15',但这可能需要调整。

    $phpExcelObject->getActiveSheet()->insertNewRowBefore(16,1);
    $phpExcelObject->getActiveSheet()->duplicateStyle($phpExcelObject->getActiveSheet()->getStyle('A16'), 'A15');

Update: 更新:

If you want to copy the data as well, I've rewritten it to cut down the repetitive bits, but start with the row you want to copy. 如果你想复制数据,我已经重写了它以减少重复位,但是从你要复制的行开始。 This then inserts a row before this and then creates a range which covers all of the row using getHighestColumn() to get the end of the row. 然后在此之前插入一行,然后使用getHighestColumn()创建一个覆盖所有行的范围以获取行的结尾。 It then copies this range for you. 然后它会为您复制此范围。

$copyFrom= "4";
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->insertNewRowBefore($copyFrom,1);
$activeSheet->duplicateStyle($activeSheet->getStyle('A'.($copyFrom+1)), 'A'.$copyFrom);
$lastColumn = $objPHPExcel->getActiveSheet()->getHighestColumn();
$rangeFrom = 'A'.($copyFrom+1).':'.$lastColumn.($copyFrom+1);
$activeSheet->fromArray($activeSheet->rangeToArray($rangeFrom), null, 'A'.$copyFrom);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM