开发者

read and write using phpexcel while using listWorksheetNames()

开发者 https://www.devze.com 2023-03-16 08:44 出处:网络
I am havinga .xlsx file. In the .xlsx file there are 4 sheets \"activity\", \"performance\", \"store\", \"display\".

I am having a .xlsx file. In the .xlsx file there are 4 sheets "activity", "performance", "store", "display". I want to load only one sheet in the memory at a time and after adding data to report write it. my code is below

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$worksheet_names = $objReader->listWorksheetNames('/tmp/ac.xlsx');
$objReader->setLoadSheetsOnly('store');
$objPHPExcel = $obj开发者_StackOverflowReader->load('/tmp/ac.xlsx');
$objPHPExcel->setActiveSheetIndexByName('store');
$sheet = $objPHPExcel->getActiveSheet();
$max_row = $sheet->getHighestRow();
$sheet->setCellValue("A$max_row", "Data");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setPreCalculateFormulas(false);
$objWriter->save('/tmp/ac.xlsx');
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);

the problem is it is writing on store sheet and deleting all the other sheets. How do i make remain all the sheets while updating 'store sheet' is there any function which will write one sheet at a time while retaining the other sheets.


Only what you have loaded into the PHPExcel object will be placed into your new file.

The createWriter isn't editing the file and retaining the sheets, it's just writing a fresh copy of what you pass in to the file name you give it. In this case, it will overwrite the file because it is the same file that you have just opened to read from. So, you must take some caution to grab the entire workbook first, then alter what you want (from the entire worksheet). After that, write everything to the file with the new changes.

The code below should help you out with retaining the other sheets. To edit only specific sheets just place the sheet names you want to edit in the $editable_worksheets array. I was very descriptive with the comments, so hopefully they will clarify step by step how this is done.

// Load your PHPExcel class
require_once 'classes/PHPExcel/Classes/PHPExcel.php';

// Set variables for file location and type to make code more portable and 
// less memory intensive
$file = '/tmp/ac.xlsx';
$file_type = 'Excel2007';

// Open file for reading
$objReader = PHPExcel_IOFactory::createReader($file_type);

// Take all exisiting worksheets in open file and place their names into an array
$worksheet_names = $objReader->listWorksheetNames($file);

// Array of worksheet names that should be editable
$editable_worksheets = array('activity', 'store');

// You will need to load ALL worksheets if you intend on saving to the same 
// file name, so we will pass setLoadSheetsOnly() the array of worksheet names 
// we just created.
$objReader->setLoadSheetsOnly($worksheet_names);

// Load the file
$objPHPExcel = $objReader->load($file);

// Loop through each worksheet in $worksheet_names array
foreach($worksheet_names as $worksheet_name) {

    // Only edit the worksheets with names we've allowed in  
    // the $editable_worksheets array
if(in_array($worksheet_name, $editable_worksheets)) {
        // Take each sheet, one at a time, and set it as the active sheet
        $objPHPExcel->setActiveSheetIndexByName($worksheet_name);

        // Grab the sheet you just made active
        $sheet = $objPHPExcel->getActiveSheet();

        // Grab the highest row from the current active sheet
        $max_row = $sheet->getHighestRow();

        // Set the value of column "A" in the last row to the text "Data"
        $sheet->setCellValue("A" . $max_row, "Data");

    }

    // Foreach loop will repeat until all sheets in the workbook have been looped
    // through
}

// Unset variables to free up memory
unset($worksheet_names, $worksheet_name, $sheet, $max_row);

// Prepare to write a new file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $file_type);

// Tell excel not to precalculate any formulas
$objWriter->setPreCalculateFormulas(false);

// Save the file
$objWriter->save($file);

// This must be called before unsetting to prevent memory leaks
$objPHPExcel->disconnectWorksheets();

// Again, unset variables to free up memory
unset($file, $file_type, $objReader, $objPHPExcel);


You're loading only a single sheet, so the PHPExcel object in memory contains only that sheet. When you save, you're overwriting the exoisting file with the workbook in memory (not editing the original file). If you want to save with the same name, and retain all four worksheet; you need to lpoad all four worksheets.

0

精彩评论

暂无评论...
验证码 换一张
取 消