在使用listWorksheetNames()时使用phpexcel进行读写
我有一个.xlsx文件。在.xlsx文件中有4页“活动”, “表演”、“商店”、“展示”。 在向内存中添加数据后,我希望一次只加载内存中的一张工作表 写报告。我的代码在下面在使用listWorksheetNames()时使用phpexcel进行读写,php,phpexcel,Php,Phpexcel,我有一个.xlsx文件。在.xlsx文件中有4页“活动”, “表演”、“商店”、“展示”。 在向内存中添加数据后,我希望一次只加载内存中的一张工作表 写报告。我的代码在下面 $objReader = PHPExcel_IOFactory::createReader('Excel2007'); $worksheet_names = $objReader->listWorksheetNames('/tmp/ac.xlsx'); $objReader->setLoadSheetsOnly(
$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);
问题是它正在存储表上写入,并删除所有其他表。
如何在更新“存储表”时保留所有表
有没有函数可以一次写一张纸
保留其他工作表。您只加载了一张工作表,因此内存中的PHPExcel对象只包含该工作表。保存时,您正在用内存中的工作簿覆盖现有文件(而不是编辑原始文件)。如果要以相同的名称保存,并保留所有四个工作表;您需要加载所有四个工作表。只有加载到PHPExcel对象中的内容才会被放置到新文件中 createWriter并没有编辑文件并保留工作表,它只是将您传入的内容的新副本写入您指定的文件名中。在这种情况下,它将覆盖该文件,因为它与您刚打开要读取的文件相同。因此,您必须注意首先获取整个工作簿,然后更改所需内容(从整个工作表中)。在那之后,用新的更改将所有内容写入文件 下面的代码将帮助您保留其他工作表。若要仅编辑特定图纸,只需将要编辑的图纸名称放置在$editable_工作表数组中。我对这些评论很有描述性,所以希望他们能一步一步地澄清这是如何做到的
// 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);
你好,马克,谢谢你的建议。。我面临着同样的问题。。。您在解决方案的最后一行写道,“您需要Ipod所有四个工作表”应该如何完成?如果可能,请向我提供代码…因为这对我来说非常紧急…提前感谢:))