Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

php - PHPExcel modify existing .xlsx file with multiple worksheet


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


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

$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load('dentkey.xlsx');
$excel2->setLoadAllSheets();
$excel2->setActiveSheetIndex(0);
$excel2->getActiveSheet()->setCellValue('C6', '4')           

->setCellValue('C7', '5')         

  ->setCellValue('C8', '6')       

    ->setCellValue('C9', '7');
$excel2->setActiveSheetIndex(1);
$excel2->getActiveSheet()->setCellValue('A7', '4')

->setCellValue('C7', '5');


$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('dentkey1.xlsx');


here, i am able to load (.xlsx) file and i also able to modify (dentkey.xlsx). But After generating new .xlsx file (dentkey1.xlsx)..all worksheets data which was in (dentkey.xlsx) is not loading and also i cannot set value for 2nd worksheet.

you can fine

$excel2->setActiveSheetIndex(1);
$excel2->getActiveSheet()->setCellValue('A7', '4')

->setCellValue('C7', '5');

is not setting value 5 to 'C7' column of generated (dentkey.xlsx) file

please help me...

thanks a lot in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code seems working, Tested in local Environment WampServer Version 2.1

Download latest version of PHPExcel from here PHPExcel 1.7.6

Working Code.

<?php
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
require_once '../Classes/PHPExcel/IOFactory.php';
require_once '../Classes/PHPExcel.php';

$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load('nTest.xlsx'); // Empty Sheet
$excel2->setActiveSheetIndex(0);
$excel2->getActiveSheet()->setCellValue('C6', '4')
    ->setCellValue('C7', '5')
    ->setCellValue('C8', '6')       
    ->setCellValue('C9', '7');

$excel2->setActiveSheetIndex(1);
$excel2->getActiveSheet()->setCellValue('A7', '4')
    ->setCellValue('C7', '5');
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('Nimit New.xlsx');
?>

Remove this line from your code: $excel2->setLoadAllSheets();.

Hope this will help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...