Need help with CSV import and column widths

Topics: Developer Forum, Project Management Forum, User Forum
Feb 17, 2012 at 4:08 PM

First I would like to start CSV Import at row 10 to allow for my image at the top. I've searched but can't find the solution.

Secondly I would like to set the column widths wider but nothing seems to work.

thanks for your help!


/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php'; 

// set headers to redirect output to client browser as a file download
header('Content-Type: application/vnd.openXMLformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');


//-----Create a reader, set some parameters and read in the file-----
$objReader = PHPExcel_IOFactory::createReader('CSV');
$objPHPExcel = $objReader->load('detail.csv');

//------image stuff
$gdImage = imagecreatefromjpeg('logo.jpg');
// Add a drawing to the worksheetecho date('H:i:s') . " Add a drawing to the worksheet\n";
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Sample image');$objDrawing->setDescription('Sample image');
//$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

//-----Create a Writer and output the file to the browser-----
$objWriter2007 = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter2007->save('php://output'); //push out to the client browser

Feb 19, 2012 at 10:10 PM
Edited Feb 19, 2012 at 10:10 PM

Load the CSV as normal, and then insert 10 new rows at the top of the worksheet using

$objPHPExcel->getActiveSheet()->insertNewRowBefore(1, 10);


Set the drawing in a page heading rather than in the worksheet itself (may or may not be an appropriate solution depending on whether the image is only needed for printing or not)


Tweak the CSV reader code. There is a boolean flag called _contiguous that is used if you want to load multiple csv files into the same worksheet without overwriting. When this flag is set to true using the CSV Reader's setContiguous(true) method, then the code uses the value of the private _contiguousRow attribute to identify the row where it should start loading the CSV file. If you add a method allowing you to set this attribute to 10, of change the setContiguous() to accept a second rowNumber argument, then you have another solution.