can't read and write xlsx file php

时光总嘲笑我的痴心妄想 提交于 2019-12-10 12:27:59

问题


this is my code i want to attach this file and send it and the values are numerical variables that in excel file i use them to drow a chart it dosen't work at all , my boss is mad at me , Help

let me explain more . i have to attach an excel file {which contains 4 numbers that are a test result and draw a chart } I've done the test i have the result, i have done sending with attachment but i can't make the file .

require_once '../Classes/PHPExcel.php';

$fileType = 'Excel2007';
$fileName = 'Result.xlsx';

// Read the file
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);

// Change the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);

$objSheet = $objPHPExcel->setActiveSheetIndex(0);

 objSheet->getCell('A2')->setValue($SumY );
  objSheet->getCell('B2')->setValue($SumR );
 objSheet->getCell('C2')->setValue($SumB );
  objSheet->getCell('D2')->setValue($SumG );



// Write the file
$objWriter->save('Result.xlsx'); 

回答1:


Tell PHPExcel that you want to include charts when reading and writing

Assuming that the chart is defined in your template

require_once '../Classes/PHPExcel.php'; 

$fileType = 'Excel2007'; 
$fileName = 'Result.xlsx'; 

// Read the file (including chart template)
$objReader = PHPExcel_IOFactory::createReader($fileType); 
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load($fileName); 

// Change the file 
$objSheet = $objPHPExcel->setActiveSheetIndex(0); 

$objSheet->getCell('A2')->setValue($SumY ); 
$objSheet->getCell('B2')->setValue($SumR ); 
$objSheet->getCell('C2')->setValue($SumB ); 
$objSheet->getCell('D2')->setValue($SumG ); 

// Write the file (including chart)
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType); 
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('Result.xlsx');

If the chart isn't defined in your template, then you need to create it in your code



来源:https://stackoverflow.com/questions/12597769/cant-read-and-write-xlsx-file-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!