Я использую пакетную обработку для чтения / записи файла xlsx .. Пожалуйста, кто-нибудь, помогите мне, если вы использовали или работали с box / spout библиотекой ... и ее документация дана на http://opensource.box.com/spout/
- Можно ли записать (загрузить) существующий файл?(без создания другого файла
- получить последний адрес строки на конкретном листе.
// on first call data start on batch processing
$contents =
Array
( [main] => Array // it will create new sheet named main on first batch call
(
[0] => Array
( [0] => data1
[1] => data2
[2] => data3 ...)
[1] => Array
( [0] => data1
[1] => data2
[2] => data3 ...)
............
));
// second batch data
Array
( [category] => Array // it will create new sheet named category
(
[0] => Array
( [0] => data1
[1] => data2
[2] => data3 ...)
[1] => Array
( [0] => data1
[1] => data2
[2] => data3 ...)
............
)
);
function writeToExcel($fileName, $contents){
$file_path = "vmbackup/" .$fileName.".xlsx";
// create new file and sheet on batch process start
if($this->createNewFile){
if (JFile::exists($file_path)){
unlink($file_path);
}
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($file_path);
$writer->getCurrentSheet()->setName(key($contents));
}
// to load existing file and write into it.
if(!$this->createNewFile){
//code: load exising file ($file_path) here to write data in it.
// $writer = WriterFactory::create(Type::XLSX);
// $writer->openToFile($file_path); // ...........this creates new file instead to load it.
$reader = ReaderFactory::create(Type::XLSX);
$reader->open($file_path);
$createNewSheet = true;
$cellAddress = 0;
foreach ($reader->getSheetIterator() as $sheet) {
if(key($contents) == $sheet->getName()){
$createNewSheet = false;
foreach ($sheet->getRowIterator() as $row) {
// last written rowAddress number on 'key($contents)'
$cellAddress++;
}
}
}
if($createNewSheet){
$writer->addNewSheetAndMakeItCurrent(); //..error will come $writer not available
$writer->getCurrentSheet()->setName(key($contents));
$cellAddress = 0;
}
}
// -----------start writing content from specified rowAddress-----------
// code: how to write an array from specific address
$writer->addRows(current($contents));
$writer->close();
}