使用PHPExcel编辑excel文件

时间:2017-08-01 14:54:08

标签: php excel phpexcel xlsx

我尝试使用PHPExcel编辑excel文件,但我的结束文件已损坏。我的猜测是因为文件过于复杂,但我不确定。一个非常简单的excel文件没有任何问题。

首先,PHPExcel读取文件,改变数据然后重新创建它? 它不只是打开和写入同一个文件?

我收到此错误消息:

  

致命错误:未捕获的异常' PHPExcel_Exception'与消息   '无效的单元格坐标(MAX(MATCH(REPT)(" Z"' in   /Library/WebServer/Documents/sb/Classes/PHPExcel/Cell.php:594 Stack   追踪:#0   /Library/WebServer/Documents/sb/Classes/PHPExcel/Cell.php(651):   PHPExcel_Cell :: coordinateFromString('(MAX(MATCH(REPT ...')#1   /Library/WebServer/Documents/sb/Classes/PHPExcel/Cell.php(624):   PHPExcel_Cell :: absoluteCoordinate('(MAX(MATCH(REPT ...')#2   /Library/WebServer/Documents/sb/Classes/PHPExcel/Writer/Excel2007/Workbook.php(438):   PHPExcel_Cell :: absoluteReference('(MAX(MATCH(REPT ...')#3   /Library/WebServer/Documents/sb/Classes/PHPExcel/Writer/Excel2007/Workbook.php(282):   PHPExcel_Writer_Excel2007_Workbook-> writeDefinedNameForPrintArea(对象(PHPExcel_Shared_XMLWriter),   对象(PHPExcel_Worksheet),0)#4   /Library/WebServer/Documents/sb/Classes/PHPExcel/Writer/Excel2007/Workbook.php(75):   PHPExcel_Writer_Excel2007_Workbook-> writeDefinedNames(对象(PHPExcel_Shared_XMLWriter),   对象(PHPExcel))#5 / Library / WebServer / Doc in   /Library/WebServer/Documents/sb/Classes/PHPExcel/Cell.php 594号线

public static function coordinateFromString($pCoordinateString = 'A1')
{
    if (preg_match("/^([$]?[A-Z]{1,3})([$]?\d{1,7})$/", $pCoordinateString, $matches)) {
        return array($matches[1],$matches[2]);
    } elseif ((strpos($pCoordinateString, ':') !== false) || (strpos($pCoordinateString, ',') !== false)) {
        throw new PHPExcel_Exception('Cell coordinate string can not be a range of cells');
    } elseif ($pCoordinateString == '') {
        throw new PHPExcel_Exception('Cell coordinate can not be zero-length string');
    }

    throw new PHPExcel_Exception('Invalid cell coordinate '.$pCoordinateString); // line 594
}

This is the excel file我正在使用。想法?

我的PHP代码:

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('America/Los_Angeles');

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

date_default_timezone_set('America/Los_Angeles');

/**
 * PHPExcel
 *
 * Copyright (c) 2006 - 2015 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version    ##VERSION##, ##DATE##
 */

/** PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';

//echo date('H:i:s') , " Load from Excel2007 file" , EOL;
$objReader = PHPExcel_IOFactory::createReader("Excel2007");
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load("templates/Cue_Sheet_Template.xlsx");


echo date('H:i:s') , " Update cell data values that are displayed in the chart" , EOL;
$objWorksheet = $objPHPExcel->getActiveSheet();
/*$objWorksheet->fromArray(
    array(
        array(50-12,   50-15,       50-21),
        array(50-56,   50-73,       50-86),
        array(50-52,   50-61,       50-69),
        array(50-30,   50-32,       50),
    ),
    NULL,
    'B2'
);*/

// Save Excel 2007 file
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;


// Echo memory peak usage
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;

// Echo done
echo date('H:i:s') , " Done writing file" , EOL;
echo 'File has been created in ' , getcwd() , EOL;

1 个答案:

答案 0 :(得分:2)

编辑(以下原始答案)

我得到了一个成功的结果。我所做的是复制xlsx模板文件的内容。打开一个新的Excel工作表并粘贴原始模板中的内容。我删除了一些定义了一些字段的条件函数,现在我的格式化得到了一个不错的结果。

Here's the preedited file.

enter image description here

<强> ORIGINAL

对不起,这不是你想要的答案。 PHPExcel无法解析打印范围。

您的解决方案应该是将您的打印区域重新定义为PHPExcel可以解析的内容,或者使用regex(即preg_split)为PHPExcel编写自己的自定义解析器,而不仅仅是分割范围值。

您基本上是在使用Excel工作表,插入值并尝试输出新的Excel工作表。

如果你发表评论

List<Commit> GetIndependent(IRepository repo, IEnumerable<Commit> commits) { var indep = new List<Commit>(); foreach (var commit in commits) { if (repo.Commits.QueryBy(new CommitFilter { FirstParentOnly = false, IncludeReachableFrom = commit, ExcludeReachableFrom = commits.Where(x => x.Equals(commit) == false) }).Any()) { indep.Add(commit); } } return indep; }

然后你会得到你的新excel表格,格式化了。

问题在于您的打印区域

throw new PHPExcel_Exception('Invalid cell coordinate '.$pCoordinateString); // line 594

PHPExcel决定在“Z”之后的':'爆炸。

我尝试重新定义您的打印区域,但未能。我现在没时间尝试这个。希望这能让你朝着正确的方向前进。