Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

php - We found a problem with some content in 'Spreadsheet.xlsx'. Do you want us to try to recover as much as we can?

I have the following basic PHP project (just one file plus composer config):

composer.json

{
    "config": {
        "optimize-autoloader": true,
        "platform": {
            "php": "7.4.9"
        }
    },
    "require": {
        "phpoffice/phpspreadsheet": "1.10.0"
    }
}

index.php

<?php

require_once __DIR__ . '/vendor/autoload.php';
function errorHandler() {
    return true;
}
set_error_handler('errorHandler');

$sheets = array(
    array('index' => 0, 'title' => 'Graph'),
    array('index' => 1, 'title' => 'Data'),
);

$phpSpreadsheetObject = new PhpOfficePhpSpreadsheetSpreadsheet();

foreach ($sheets as $sheet) {
    $name = $sheet['title'];
    if ($sheet['index']) {
        $worksheet[$name] = $phpSpreadsheetObject->createSheet($sheet['index']);
    } else {
        $worksheet[$name] = $phpSpreadsheetObject->getActiveSheet();
    }
    $phpSpreadsheetObject->setActiveSheetIndex($sheet['index']);
    $worksheet[$name]->setTitle($sheet['title']);
}

$sheet = 'Graph'; // !!! SHEET CHANGE

$phpSpreadsheetObject->setActiveSheetIndex(1);
$worksheet[$sheet]->getColumnDimension('A')->setWidth("50");

// Charts

// Clients Chart
$xAxisTickValues = array(new PhpOfficePhpSpreadsheetChartDataSeriesValues('String', "'Data'!A2:A4", null, 3));
$dataSeriesValues = array(new PhpOfficePhpSpreadsheetChartDataSeriesValues('Number', "'Data'!B2:B4", null, 3));
$chartSeries = new PhpOfficePhpSpreadsheetChartDataSeries(
    PhpOfficePhpSpreadsheetChartDataSeries::TYPE_BARCHART, // plotType
    PhpOfficePhpSpreadsheetChartDataSeries::GROUPING_CLUSTERED, // plotGrouping
    range(0, count($dataSeriesValues) - 1), // plotOrder
    [], // plotLabel
    $xAxisTickValues, // plotCategory
    $dataSeriesValues // plotValues
);
$chartSeries->setPlotDirection(PhpOfficePhpSpreadsheetChartDataSeries::DIRECTION_COLUMN);
$plotArea = new PhpOfficePhpSpreadsheetChartPlotArea(null, array($chartSeries));
$title = new PhpOfficePhpSpreadsheetChartTitle('Clients');
$yAxisLabel = new PhpOfficePhpSpreadsheetChartTitle('');
$charts = new PhpOfficePhpSpreadsheetChartChart(
    'clients', // name
    $title, // title
    null, // legend
    $plotArea, // plotArea
    true, // plotVisibleOnly
    0, // displayBlanksAs
    null, // xAxisLabel
    $yAxisLabel // yAxisLabel
);
$charts->setTopLeftPosition('A1');
$charts->setBottomRightPosition('B19');
$worksheet[$sheet]->addChart($charts);

$sheet = 'Data'; // !!! SHEET CHANGE

$phpSpreadsheetObject->setActiveSheetIndex(1);
$dataArray = array(
    1 => array('Date', 'Clients'),
    2 => array(date('m/d/y', strtotime('01/01/2021')), '500'),
    3 => array(date('m/d/y', strtotime('01/02/2021')), '725'),
    4 => array(date('m/d/y', strtotime('01/03/2021')), '930'),
);
foreach (range('A', 'B') as $columnID) {
    $worksheet[$sheet]->getColumnDimension($columnID)->setAutoSize(true);
}
$worksheet[$sheet]->fromArray($dataArray, ' ', 'A1');

// set the first tab as active
$phpSpreadsheetObject->setActiveSheetIndex(0);

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment;filename=Spreadsheet.xlsx");
header('Cache-Control: max-age=0');
$objWriter = new PhpOfficePhpSpreadsheetWriterXlsx($phpSpreadsheetObject);
$objWriter->setIncludeCharts(true);
$objWriter->save('php://output');

?>

Setup:

$ composer i

When I visit the url:

http://localhost/index.php

the following Excel file is downloaded:

enter image description here

where you have 2 sheets: { Graph, Data }. The Graph is genreated based on the data on sheet: Data.

So far so good.

My Problem is: When I upgrade:

"phpoffice/phpspreadsheet": "1.10.0" -> "phpoffice/phpspreadsheet": "1.10.1"

(just patch update)

and hit the same url again, I get the following errors when trying to open the generated Excel file:

enter image description here

We found a problem with some content in 'Spreadsheet (1).xlsx'. Do you want us to try to recover as much as we can? if you trust the source of this workbook, click Yes.

and then this other error:

enter image description here

and the graph is not shown.

Any idea what modification I need to do on my code above to get rid of those errors and get the graph rendered?

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found the solution.

On the code above, just replace: 0, // displayBlanksAs -> 'gap', // displayBlanksAs.

On latest versions like: "phpoffice/phpspreadsheet": "1.16", there is a constant defined for this: DataSeries::EMPTY_AS_GAP, // displayBlanksAs.

That constant is not present on earlier versions like: 1.10.1.

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...