PhpSpreadsheet 棒グラフ作成 作成されたExcelファイルを開くと一部内容に問題あり

PHP

1require 'vendor/autoload.php';2use PhpOffice\PhpSpreadsheet\Spreadsheet;3use PhpOffice\PhpSpreadsheet\Writer\Xlsx;4use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;5use PhpOffice\PhpSpreadsheet\Chart\Chart;6use PhpOffice\PhpSpreadsheet\Chart\DataSeries;7use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;8use PhpOffice\PhpSpreadsheet\Chart\Layout;9use PhpOffice\PhpSpreadsheet\Chart\Legend;10use PhpOffice\PhpSpreadsheet\Chart\PlotArea;11use PhpOffice\PhpSpreadsheet\Chart\Title;12use PhpOffice\PhpSpreadsheet\IOFactory;13 14 private function outputExcel3($filePath,$fileName) {15 // Spreadsheetオブジェクト生成16 $objSpreadsheet = new Spreadsheet();17 // ワークシートオブジェクト取得18 $objWorksheet = $objSpreadsheet->getActiveSheet();19 // チャート用テストデータ生成20 $objWorksheet->fromArray(21 array(22 array('売上', '商品1', '商品2', '商品3'),23 array( 2016, 12, 18, 15),24 array( 2017, 15, 19, 10),25 array( 2018, 21, 23, 20),26 array( 2019, 18, 14, 12),27 array( 2020, 20, 21, 23),28 )29 );30 31 // 系列ラベルの指定32 $arrDataSeriesLabels = array(33 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', NULL, 1), //商品134 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', NULL, 1), //商品235 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', NULL, 1), //商品336 );37 // X軸ラベルの指定38 $arrCategorysDataSeries = array(39 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$6', NULL, 5), //2016 to 202040 );41 // 描画データの指定42 $arrDataSeriesValues = array(43 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$6', NULL, 5), //商品144 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$6', NULL, 5), //商品245 new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$6', NULL, 5), //商品346 );47 // チャート・データシリーズの生成48 $objSeries = new DataSeries(49 DataSeries::TYPE_BARCHART, // plotType50 // NULL, // plotGrouping(DataSeries::GROUPING_STANDARD)51 DataSeries::GROUPING_STANDARD, // plotGrouping52 range(0, count($arrDataSeriesValues) - 1), // plotOrder53 $arrDataSeriesLabels, // plotLabel54 $arrCategorysDataSeries, // plotCategory55 $arrDataSeriesValues // plotValues56 );57 58 // プロットエリアにチャート・データシリーズに設定59 $objPlotArea = new PlotArea(NULL, array($objSeries));60 // レジェンド生成(各折れ線の説明を行う)61 $objLegend = new Legend(Legend::POSITION_TOPRIGHT, NULL, false);62 // チャート・タイトル生成63 $objTitle = new Title('売上データ:Bar Chart');64 // チャート生成65 $objChart = new Chart(66 'chart1', // name67 $objTitle, // title68 $objLegend, // legend69 $objPlotArea, // plotArea70 TRUE, // plotVisibleOnly71 0, // displayBlanksAs72 NULL, // xAxisLabel73 NULL // yAxisLabel74 );75 // ワークシート内のチャート位置設定76 $objChart->setTopLeftPosition('A8'); // 左上77 $objChart->setBottomRightPosition('G20'); // 右下78 79 // ワークシートにチャート追加80 $objWorksheet->addChart($objChart);81 82 // [test-g-2-1.xlsx]:Excel2007形式で保存する83 $objWriter = IOFactory::createWriter($objSpreadsheet, 'Xlsx');84 $objWriter->setIncludeCharts(TRUE);85 $objWriter->save($filePath.'/'.$fileName);86 87 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;');88 header('Content-Disposition: attachment; filename="'.$fileName.'"');89 header('Cache-Control: max-age=0');90 echo file_get_contents($filePath.'/'.$fileName);91 }

コメントを投稿

0 コメント