简体   繁体   English

使用“ exceldatatables”连接不同工作簿的两张纸

[英]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. 我必须创建一个Excel文件,该文件的数据表将根据数据库而有所不同,并且该表包含多个具有自己的数据透视图的数据透视表。

"data.xlsx" contains a sheet with all new data. “ data.xlsx”包含具有所有新数据的工作表。

"graph.xlsx" contains a sheet with old data and a sheet with PivotTables. “ graph.xlsx”包含具有旧数据的工作表和具有数据透视表的工作表。

My goal is to have "graph.xlsx" containing a sheet with all new data and the sheet with PivotTables. 我的目标是使“ graph.xlsx”包含一个包含所有新数据的表和一个包含PivotTables的表。

I found a perfect lib to do this : https://github.com/svrnm/exceldatatables 我找到了一个完美的库来做到这一点: 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". 但是我禁止使用它,我将打开“ graph.xlsx”,删除其名为“ brut data”的工作表,然后添加一个新的名为“ brut data”的工作表,并使用“ data.xlsx”中包含的新数据进行初始化。

To do it I saw this function from ExcelWorkbook.php a Class of this lib. 为此,我从ExcelWorkbook.php看到了这个库的一个类。

    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. 要实现目标,您需要执行两个步骤,第一步是读取data.xlsx。 The second is writing that data into your graph.xlsx. 第二步是将数据写入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 . 对于步骤1,您可以利用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. 还有另一种选择:您可以解开graph.xlsx和data.xlsx并合并工作表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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