dtc66318 2016-04-22 20:17
浏览 252
已采纳

PHPExcel:如何读取内容来自其他工作表中单元格的单元格中的值

I have spent the last days trying to solve this issue. I have a spreadsheet with a couple tabs (sheets). The last tab has the values input in all other tabs. Those values are populated automatically by using formulas like:

=$'otherSheetName'.A1

If I leave calculateFormulas = true those values are read as null. If I set calculateFormulas = false the values are read as the literal formula.

I was firstly using LaravelExcel but I gave up from finding a solution for that, so I moved to PHPExcel. Solutions for Laravel Excel will be appreciated too though.

Thanks

Update: How I am reading values in PHPExcel:

$inputFileType = PHPExcel_IOFactory::identify($file->getRealPath());
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setLoadSheetsOnly("MySheet");
$objPHPExcel = $objReader->load($file->getRealPath()); 
$rows = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

Then, I iterate over the array $rows and extract the values that I want.

  • 写回答

1条回答 默认 最新

  • doutangshuan6473 2016-04-26 07:04
    关注

    You need to load all the dependency sheets that are used in those formulae, though you don't necessarily need to load every worksheet, but can pass a list of sheet names to load as an array

    $objReader->setLoadSheetsOnly(["MySheet", "OtherSheet1", "OtherSheet2"]);
    

    You don't need to iterate over any other worksheets, but can set the active worksheet to whichever sheet you want

    $objReader->setActiveSheetIndexByName("MySheet");
    

    and then any calls to the active sheet ($objPHPExcel->getActiveSheet()->...) to access cells will be a reference to that worksheet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?