0

I am trying to create an excel file with values which I get from the following array

for ($i=0;$i<=20;$i++){

for ($j=16;$j<=24;$j++){
    $data1=array($R,$C,$i,$j,'0','0'); .

I tried to do something like that

$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
    $data1
);

using an existing example.However,each time I try to run this php file, excel stops working.What should I try?

edit

$R=1;
$C=0.7;


for ($i=0;$i<=20;$i++){

for ($j=16;$j<=24;$j++){
    $data1[]=array($R,$C,$i,$j,'0','0');

    $objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
    array(
        $data1)

);

    }}
13
  • You should try some basic debugging, perhaps var dumping $data1 so we can see what it actually contains Commented Mar 30, 2015 at 12:35
  • array(6) { [0]=> int(1) [1]=> float(0.7) [2]=> int(0) [3]=> int(16) [4]=> string(1) "0" [5]=> string(1) "0" } array(6) { [0]=> int(1) [1]=> float(0.7) [2]=> int(0) [3]=> int(17) [4]=> string(1) "0" [5]=> string(1) "0" } and so on... Commented Mar 30, 2015 at 12:48
  • And do you mean that MS Excel stops working when you try to load the generated file? or that PHPExcel stops working? Is a file generated or not? Are there any error messages, whether displayed on screen or written to log files? Commented Mar 30, 2015 at 12:51
  • Yes, there is a file generated. When I try to open this specific file, the MS Excel stops working.This is the displayed error message : There was a problem sending the command to the program. Commented Mar 30, 2015 at 12:55
  • So show a bit more code, like the save, perhaps you're doing something wrong there Commented Mar 30, 2015 at 13:22

1 Answer 1

1
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$R=1; 
$C=0.7; 
for ($i=0;$i<=20;$i++){ 
    for ($j=16;$j<=24;$j++){ 
        $data1[]=array($R,$C,$i,$j,'0','0'); 
    }
}
$objWorksheet->fromArray( $data1 );

//  Set the Labels for each data series we want to plot
//      Datatype
//      Cell reference for data
//      Format Code
//      Number of datapoints in series
//      Data values
//      Data Marker
$dataSeriesLabels = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1),   //  'Budget'
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1),   //  'Forecast'
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$E$1', NULL, 1),   //  'Actual'
);
//  Set the X-Axis Labels
//      Datatype
//      Cell reference for data
//      Format Code
//      Number of datapoints in series
//      Data values
//      Data Marker
$xAxisTickValues = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$B$13', NULL, 12),    //  Q1 to Q4 for 2010 to 2012
);
//  Set the Data values for each data series we want to plot
//      Datatype
//      Cell reference for data
//      Format Code
//      Number of datapoints in series
//      Data values
//      Data Marker
$dataSeriesValues = array(
    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$13', NULL, 12),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$13', NULL, 12),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$E$2:$E$13', NULL, 12),
);

//  Build the dataseries
$series = new PHPExcel_Chart_DataSeries(
    PHPExcel_Chart_DataSeries::TYPE_BARCHART,       // plotType
    PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED,  // plotGrouping
    range(0, count($dataSeriesValues)-1),           // plotOrder
    $dataSeriesLabels,                              // plotLabel
    $xAxisTickValues,                               // plotCategory
    $dataSeriesValues                               // plotValues
);
//  Set additional dataseries parameters
//      Make it a vertical column rather than a horizontal bar graph
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);

//  Set the series in the plot area
$plotArea = new PHPExcel_Chart_PlotArea(NULL, array($series));
//  Set the chart legend
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_BOTTOM, NULL, false);

$title = new PHPExcel_Chart_Title('Test Grouped Column Chart');
$xAxisLabel = new PHPExcel_Chart_Title('Financial Period');
$yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');


//  Create the chart
$chart = new PHPExcel_Chart(
    'chart1',       // name
    $title,         // title
    $legend,        // legend
    $plotArea,      // plotArea
    true,           // plotVisibleOnly
    0,              // displayBlanksAs
    $xAxisLabel,    // xAxisLabel
    $yAxisLabel     // yAxisLabel
);

//  Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('G2');
$chart->setBottomRightPosition('P20');

//  Add the chart to the worksheet
$objWorksheet->addChart($chart);


// Save Excel 2007 file
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
Sign up to request clarification or add additional context in comments.

1 Comment

I see.That worked.Thanks a lot for your help and my apologies for wasting your time.It's my first time using PHPExcel and I had some problems with learning how to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.