PhpExcel:如何模拟excel拖动单元格内容

标签 php phpexcel

我正在编写一个 PHP 脚本,通过使用 PHP excel 生成 excel 报告。我想做的是允许用户指定可以“拖动”单元格的内容。我的意思是利用或模拟 Excel 的这一功能,该功能允许用户选择一个或多个单元格并拖动其内容以生成相邻单元格的内容。我找不到任何这方面的信息。我是否需要在脚本中生成单元格的内容,或者 PHP excel 是否有一些偷偷摸摸的功能可以为我做到这一点?我认为是前者,但我对 PHPExcel 还很陌生,所以想在深入研究之前先确定一下。

最佳答案

PHPExcel 没有“偷偷摸摸”的功能,您必须为每个单元格设置内容,但有几种方法可以帮助在一次调用中设置多个单元格值。

如果您讨论的是直接数据内容,您可以指定一个起始单元格并将值数组传递给工作表的 fromArray() 方法:

* Fill worksheet from values in array
*
* @param  array   $source                 Source array
* @param  mixed   $nullValue              Value in source array that stands for blank
*                                             cell
* @param  string  $startCell              Insert array starting from this cell
*                                             address as the top left coordinate
* @param  boolean $strictNullComparison   Apply strict comparison when testing for null
*                                             values in the array
* @throws Exception
* @return PHPExcel_Worksheet
public function fromArray( $source = null, 
                           $nullValue = null, 
                           $startCell = 'A1', 
                           $strictNullComparison = false
                         )

如果您需要在一系列单元格中操作公式,例如=$A1+$B1+$C$1 对于一组行,使得 $A1 和 $B1 成为第二行的 $A2 和 $B2,第三行的 $A3 和 $B3 等,然后 ReferenceHelper 的 updateFormulaReferences() 方法可以有用:

* Update references within formulas
*
* @param   string  $pFormula    Formula to update
* @param   int     $pBefore     Insert before this one
* @param   int     $pNumCols    Number of columns to insert
* @param   int     $pNumRows    Number of rows to insert
* @return  string  Updated formula
* @throws  Exception
public function updateFormulaReferences( $pFormula = '', 
                                         $pBefore = 'A1', 
                                         $pNumCols = 0, 
                                         $pNumRows = 0, 
                                         $sheetName = ''
                                       )

关于PhpExcel:如何模拟excel拖动单元格内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9191250/

相关文章:

Java:在 Mac 上创建一个具有 777 权限的新目录

php - MySQL:csv 中的 INTO OUTFILE 生成无效的 csv 格式

PHPExcel图形设计(边框、图形颜色、图形内部位置)

phpexcel - 如何使用PhpExcel将excel电子表格写入变量?

PHPExcel GetCalculatedValue() 返回错误值

php - 意外的 'class' (T_CLASS) 仅在远程(不在本地)

php - mysql查询长记录,需要足够长的时间才能显示记录

php - $.get 不返回 html

php - PHP 函数之间传递数组

php - Laravel-Excel CSV 分隔符不起作用