php - Convert array data into Excel -
i converting array data excel using phpexcel. when excel file created, data stored row wise want store data column wise. array given below:
array (size=3) 0 => string '8801755568952' (length=13) 1 => string '8801755556987' (length=13) 2 => string '8801755587985' (length=13)
my array excel conversion code is:
$objphpexcel->getactivesheet()->fromarray($csv_data, null, 'a1') $objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5'); return $objwriter->save($xls_output_path);
my output is:
8801755568952 8801755556987 8801755587985
my desired output is:
8801755568952 8801755556987 8801755587985
if pass simple 1-dimensional array fromarray()
method, phpexcel assumes single row, you're seeing.
either convert array 2-dimensional array
as phpexcel treat these values numeric, , they're larger limit 32-bit signed integer in php (and you're running 32-bit php), they'll cast float, , you'll potentially lose accuracy, wouldn't use approach myself.
if want convert array 2-dimensional array, use:
$csv_data = call_user_func_array( 'array_map', array_merge(array(null), [$csv_data]) );
or loop on writing each row individually
$row = 1; foreach($csv_data $celldata) { $objphpexcel->getactivesheet()->setcellvalueexplicit('a'.$row++, $celldata); }
the use of setcellvalueexplicit()
force values treated string, nothing cast float, no loss of precision
Comments
Post a Comment