使用listWorksheetNames()时使用phpexcel读写

时间:2011-06-28 12:10:58

标签: php phpexcel

我有一个.xlsx文件。在.xlsx文件中有4张“活动”,  “表演”,“商店”,“展示”。 我想一次只在内存中加载一张纸,然后再添加数据 报告写下来。我的代码在下面

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$worksheet_names = $objReader->listWorksheetNames('/tmp/ac.xlsx');
$objReader->setLoadSheetsOnly('store');
$objPHPExcel = $objReader->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);
问题是它是在商店表上书写并删除所有其他表。 如何在更新“商品单”时保留所有纸张 是否有任何功能,一次写一张纸 保留其他床单。

2 个答案:

答案 0 :(得分:2)

只有加载到PHPExcel对象中的内容才会被放入新文件中。

createWriter不是正在编辑文件并保留工作表,它只是您传入文件的新副本你给它的名字。在这种情况下,它将覆盖该文件,因为它与您刚刚打开以进行读取的文件相同。因此,您必须首先抓住整个工作簿,然后改变您想要的内容(从整个工作表中)。之后,使用新更改将所有内容写入文件。

下面的代码可以帮助您保留其他表格。要仅编辑特定工作表,只需将要编辑的工作表名称放在$ editable_worksheets数组中。我对这些评论非常具有描述性,所以希望他们能够逐步澄清如何做到这一点。

// 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);

答案 1 :(得分:1)

您只加载一张工作表,因此内存中的PHPExcel对象仅包含该工作表。保存时,您将使用内存中的工作簿覆盖exoisting文件(不编辑原始文件)。如果要使用相同的名称保存,并保留所有四个工作表;你需要lpoad所有四个工作表。