我正在尝试修改下载的googe doc电子表格,然后将其保存到我的服务器。 下载文件很好,我把它保存到我的服务器,它打开确定,一切都很好。当我将文件加载到PHPExcel对象然后修改一些单元格然后重新保存修改后的文件时出现问题。 任何人都可以看到我错在哪里?忽略从数据库获取股票价值的行,它们在我的代码中正确编码我刚刚从粘贴的代码示例中删除了查询。
我收到错误:
致命错误:未捕获的异常'异常'有消息'无效的单元格坐标'当前产品'!A2'在.. /PHPExcel \ Class \ PHPExcel \ Cell.php:508堆栈追踪:#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');
}
答案 0 :(得分:0)
好的,我终于有了这个工作。我必须重写它,以便创建第二个PHPExcel对象并通过将原始文件中的每个单元格值复制到新文件并更新我想要更新的值来创建新文件,因为我将文件复制到新的那一个。 我已手动设置要复制的列数/行数,因为我知道需要多少列/行。我还必须修改PHPExcel的Calculation.php文件,因为我的文件中有一个vlookup引用了一个不存在于文件中的工作表,它一直在抛出一个错误,在非对象上调用cellExists函数,所以我在尝试获取单元格之前添加了一个检查表单是否存在。我的代码现在是:
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');
}