PHPExcel 编辑单元格并保存 - 'Invalid Cell Coordinate'
PHPExcel edit cells and resave - 'Invalid Cell Coordinate'
我正在尝试修改下载的 googe doc 电子表格,然后将其保存到我的服务器。
文件下载没问题,我把它保存到我的服务器上,它打开正常,一切都很好。当我将文件加载到 PHPExcel 对象中,然后修改一些单元格,然后重新保存修改后的文件时,就会出现问题。
谁能看到我哪里出错了?忽略从数据库获取股票价值的行,它们在我的代码中正确编码我刚刚从粘贴的代码示例中删除了查询。
我收到错误:
Fatal error: Uncaught exception 'Exception' with message 'Invalid cell coordinate 'CURRENT PRODUCTS'!A2' in .. /PHPExcel\Classes\PHPExcel\Cell.php:508 Stack trace: #0 ....
function updateDoc($url)
{
file_put_contents("filename.xlsx",fopen($url,"r"));
$filename = "filename.xlsx";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$excel2 = $objReader->load('filename.xlsx');
$excel2->setActiveSheetIndexByName("Current Products");
$rowIterator = $excel2->getActiveSheet()->getRowIterator();
$rows = 1;
$columnAsLetters = 'E';
foreach($rowIterator as $row)
{
if($rows <= 2)
{
continue;//skip first 2 rows
}
else
{
$data = $excel2->getActiveSheet()->getCell($columnAsLetters.$rows)->getCalculatedValue();
$sku = trim($data);
$stock = //** I select stock from database **//
$r = //** returned sql row **//
$stk = $r[0];
$excel2->getActiveSheet()->setCellValue("G" . $rows, $stk);
}
$rows++;
}
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->setPreCalculateFormulas(FALSE);
$objWriter->save('filename2.xlsx');
}
好的,我终于搞定了。我不得不重写它,以便我创建第二个 PHPExcel 对象并通过将每个单元格值从原始文件复制到新文件并更新我想要更新的值来创建一个新文件,因为我将文件复制到新文件一。
我已经手动设置了要复制的 columns/rows 的数量,因为我知道需要多少。我还必须修改 PHPExcel 的 Calculation.php 文件,因为我的文件中有一个 vlookup 引用了文件中不存在的 sheet 并且它一直抛出调用 cellExists 函数的错误非对象所以我在尝试获取单元格之前添加了一个检查以查看 sheet 是否存在。我的代码现在是:
function updateDoc($url)
{
file_put_contents("filename.xlsx",fopen($url,"r"));
$filename = "filename.xlsx";
$objPHPExcel = new PHPExcel();
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setLoadAllSheets();
$objPHPExcel = $objReader->load($filename);
$sheets = $objReader->listWorksheetNames($filename);
// Created file
$objPHPExcel2 = new PHPExcel();
$objPHPExcel2->getProperties()->setCreator("User")
->setLastModifiedBy("User")
->setTitle("filename2")
->setSubject("filename2")
->setDescription("filename2")
->setCategory("PublicWEB");
$s=0;
foreach($sheets as $sheet)
{
if($s != 0)
{
$objPHPExcel2->createSheet($s);
$objPHPExcel2->getSheet($s)->setTitle($sheet);
}
else
{
$objPHPExcel2->getSheet($s)->setTitle($sheet);
}
$objPHPExcel->setActiveSheetIndexByName($sheet);
$objPHPExcel2->setActiveSheetIndex($s);
for($rows = 1 ; $rows < 250 ; $rows++)
{
for ($col=0 ;$col < 100 ; $col++)
{
$cellV = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($col, $rows)->getValue();
if($sheet == "Current Products" && $col == 6 && $rows > 2)
{
$data = $objPHPExcel2->getActiveSheet()->getCellByColumnAndRow(4,$rows)->getValue();
$sku = trim($data);
if($sku != "")
{
$stock = $mysqli->query("SELECT stock FROM table WHERE partno = '$sku'");
if($stock->num_rows > 0)
{
$r = $stock->fetch_array();
$stk = $r[0];
$objPHPExcel2->getActiveSheet()->setCellValueByColumnAndRow($col, $rows, $stk );
}
else
{
$objPHPExcel2->getActiveSheet()->setCellValueByColumnAndRow($col, $rows, "NA" );
}
}
else
{
$objPHPExcel2->getActiveSheet()->setCellValueByColumnAndRow($col, $rows, "" );
}
}
else
{
$objPHPExcel2->getActiveSheet()->setCellValueByColumnAndRow($col, $rows, $cellV );
}
}
}
++$s;
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel2, 'Excel2007');
$objWriter->save('filename2.xlsx');
}
我正在尝试修改下载的 googe doc 电子表格,然后将其保存到我的服务器。 文件下载没问题,我把它保存到我的服务器上,它打开正常,一切都很好。当我将文件加载到 PHPExcel 对象中,然后修改一些单元格,然后重新保存修改后的文件时,就会出现问题。 谁能看到我哪里出错了?忽略从数据库获取股票价值的行,它们在我的代码中正确编码我刚刚从粘贴的代码示例中删除了查询。
我收到错误:
Fatal error: Uncaught exception 'Exception' with message 'Invalid cell coordinate 'CURRENT PRODUCTS'!A2' in .. /PHPExcel\Classes\PHPExcel\Cell.php:508 Stack trace: #0 ....
function updateDoc($url)
{
file_put_contents("filename.xlsx",fopen($url,"r"));
$filename = "filename.xlsx";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$excel2 = $objReader->load('filename.xlsx');
$excel2->setActiveSheetIndexByName("Current Products");
$rowIterator = $excel2->getActiveSheet()->getRowIterator();
$rows = 1;
$columnAsLetters = 'E';
foreach($rowIterator as $row)
{
if($rows <= 2)
{
continue;//skip first 2 rows
}
else
{
$data = $excel2->getActiveSheet()->getCell($columnAsLetters.$rows)->getCalculatedValue();
$sku = trim($data);
$stock = //** I select stock from database **//
$r = //** returned sql row **//
$stk = $r[0];
$excel2->getActiveSheet()->setCellValue("G" . $rows, $stk);
}
$rows++;
}
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->setPreCalculateFormulas(FALSE);
$objWriter->save('filename2.xlsx');
}
好的,我终于搞定了。我不得不重写它,以便我创建第二个 PHPExcel 对象并通过将每个单元格值从原始文件复制到新文件并更新我想要更新的值来创建一个新文件,因为我将文件复制到新文件一。 我已经手动设置了要复制的 columns/rows 的数量,因为我知道需要多少。我还必须修改 PHPExcel 的 Calculation.php 文件,因为我的文件中有一个 vlookup 引用了文件中不存在的 sheet 并且它一直抛出调用 cellExists 函数的错误非对象所以我在尝试获取单元格之前添加了一个检查以查看 sheet 是否存在。我的代码现在是:
function updateDoc($url)
{
file_put_contents("filename.xlsx",fopen($url,"r"));
$filename = "filename.xlsx";
$objPHPExcel = new PHPExcel();
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setLoadAllSheets();
$objPHPExcel = $objReader->load($filename);
$sheets = $objReader->listWorksheetNames($filename);
// Created file
$objPHPExcel2 = new PHPExcel();
$objPHPExcel2->getProperties()->setCreator("User")
->setLastModifiedBy("User")
->setTitle("filename2")
->setSubject("filename2")
->setDescription("filename2")
->setCategory("PublicWEB");
$s=0;
foreach($sheets as $sheet)
{
if($s != 0)
{
$objPHPExcel2->createSheet($s);
$objPHPExcel2->getSheet($s)->setTitle($sheet);
}
else
{
$objPHPExcel2->getSheet($s)->setTitle($sheet);
}
$objPHPExcel->setActiveSheetIndexByName($sheet);
$objPHPExcel2->setActiveSheetIndex($s);
for($rows = 1 ; $rows < 250 ; $rows++)
{
for ($col=0 ;$col < 100 ; $col++)
{
$cellV = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($col, $rows)->getValue();
if($sheet == "Current Products" && $col == 6 && $rows > 2)
{
$data = $objPHPExcel2->getActiveSheet()->getCellByColumnAndRow(4,$rows)->getValue();
$sku = trim($data);
if($sku != "")
{
$stock = $mysqli->query("SELECT stock FROM table WHERE partno = '$sku'");
if($stock->num_rows > 0)
{
$r = $stock->fetch_array();
$stk = $r[0];
$objPHPExcel2->getActiveSheet()->setCellValueByColumnAndRow($col, $rows, $stk );
}
else
{
$objPHPExcel2->getActiveSheet()->setCellValueByColumnAndRow($col, $rows, "NA" );
}
}
else
{
$objPHPExcel2->getActiveSheet()->setCellValueByColumnAndRow($col, $rows, "" );
}
}
else
{
$objPHPExcel2->getActiveSheet()->setCellValueByColumnAndRow($col, $rows, $cellV );
}
}
}
++$s;
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel2, 'Excel2007');
$objWriter->save('filename2.xlsx');
}