简体   繁体   中英

use “exceldatatables” to join two sheets of a different workbook

I have to create an Excel file with a data sheet that will vary according to a database and a sheet containing multiple PivotTables that have their own PivotCharts.

"data.xlsx" contains a sheet with all new data.

"graph.xlsx" contains a sheet with old data and a sheet with PivotTables.

My goal is to have "graph.xlsx" containing a sheet with all new data and the sheet with PivotTables.

I found a perfect lib to do this : https://github.com/svrnm/exceldatatables

But I block on the use of it, I would open "graph.xlsx" delete its sheet named "brut data", then to add a new sheet named "brut data" initialized with the new data contained in "data.xlsx".

To do it I saw this function from ExcelWorkbook.php a Class of this lib.

    public function addWorksheet(ExcelWorksheet $worksheet, $id = null, $name = null)

But I don't understand how to use it.

(I'm the author of the mentioned library)

There are two steps you need to take, to achieve your goal, the first is reading the data.xlsx. The second is writing that data into your graph.xlsx. The library is solving step two:

    require_once('../vendor/autoload.php');
    $dataTable = new Svrnm\ExcelDataTables\ExcelDataTable();
    $in = 'graph.xlsx';
    $out = 'out.xlsx';
    $data = /* ... step 1 ... */ 
    $dataTable->showHeaders()->addRows($data)->attachToFile($in, $out);

For step 1 you could leverage PHPExcel . I haven't tested it, but something similar like this:

$r = PHPExcel_IOFactory::createReader('Excel2007');
$data = $r->load($filename)->getActiveSheet()->toArray(null, true, true);

There is another option: You could unpack both graph.xlsx and data.xlsx and merge the sheets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM