【问题标题】:PhpSpreadSheet : Writing to a specific WorkSheetPhpSpreadSheet :写入特定的工作表
【发布时间】:2018-11-01 10:21:52
【问题描述】:

我是 PhpSpreadSheet 的新手,我想知道是否有办法将 CSV 加载到特定的 WorkSheet 中?

我尝试了下面的代码,但它似乎一直将 CSV 加载到第一个 WorkSheet 中:/。

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Csv;

$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);

$pathToCsv1 = 'files/csv_files/1.csv';
$pathToCsv2 = 'files/csv_files/2.csv';
$pathToCsv3 = 'files/csv_files/3.csv';
$pathToCsv4 = 'files/csv_files/4.csv';

$aCsvFiles = array($pathToCsv1, $pathToCsv2, $pathToCsv3, $pathToCsv4);

foreach ($aCsvFiles as $index => $csvFile) {
    $reader = new Csv();
    $reader->setDelimiter(';');
    $reader->loadIntoExisting($csvFile, $spreadsheet);
    $workSheet = $spreadsheet->createSheet();
    $spreadsheet->setActiveSheetIndex($index + 1);
}

$writer = new Xlsx($spreadsheet);
$writer->save('files/xls_files/all.xlsx');

我在 all.xlsx 中只得到 4.csv,但我有创建的 WorkSheets

【问题讨论】:

    标签: php phpspreadsheet


    【解决方案1】:

    将多个文件组合成一个电子表格对象

    虽然您可以限制从 使用setLoadSheetsOnly() 方法的工作簿文件,某些读者也 允许您组合来自不同文件的多个单独的“工作表” 到单个 Spreadsheet 对象中,其中每个单独的文件是一个 该工作簿中的单个工作表。对于您阅读的每个文件,您 需要指出应该使用哪个工作表索引加载它 $reader 的 setSheetIndex() 方法,然后使用 loadIntoExisting() 方法而不是 load() 方法来实际读取 将文件放入该工作表中。

    示例:

    $inputFileType = 'Csv';
    $inputFileNames = [
        './sampleData/example1.csv',
        './sampleData/example2.csv'
        './sampleData/example3.csv'
    ];
    
    /**  Create a new Reader of the type defined in $inputFileType  **/
    $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
    
    /**  Extract the first named file from the array list  **/
    $inputFileName = array_shift($inputFileNames);
    /**  Load the initial file to the first worksheet in a `Spreadsheet` Object  **/
    $spreadsheet = $reader->load($inputFileName);
    /**  Set the worksheet title (to the filename that we've loaded)  **/
    $spreadsheet->getActiveSheet()
        ->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
    
    /**  Loop through all the remaining files in the list  **/
    foreach($inputFileNames as $sheet => $inputFileName) {
        /**  Increment the worksheet index pointer for the Reader  **/
        $reader->setSheetIndex($sheet+1);
        /**  Load the current file into a new worksheet in Spreadsheet  **/
        $reader->loadIntoExisting($inputFileName,$spreadsheet);
        /**  Set the worksheet title (to the filename that we've loaded)  **/
        $spreadsheet->getActiveSheet()
            ->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
    }
    

    请注意,对多个工作表使用相同的工作表索引不会将文件附加到同一个工作表中,而是会覆盖先前加载的结果。您不能将多个 CSV 文件加载到同一个工作表中。

    https://phpspreadsheet.readthedocs.io/en/develop/topics/reading-files/#combining-multiple-files-into-a-single-spreadsheet-object

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 2018-09-15
      • 2017-10-29
      • 2019-07-15
      • 2019-06-01
      相关资源
      最近更新 更多