Below the sample codes. It's may help you..
When i'm change the row number(1) into 5 then seven rows are empty in excel sheet.(LOCATION : setCellValueByColumnAndRow($col, 1, $field);)
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
$objPHPExcel->setActiveSheetIndex(0);
$fields = array('ram','one','two');
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
$col++;
}
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="Export.csv"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
$objPHPExcel->setActiveSheetIndex(0);
$field = array('ram','one','two');
$col = 0;
foreach ($fields as $field)
{
if(empty($field)){
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
$col++;
}
}
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="Export.csv"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
You're defining $field but foreach references $fields.... that s makes a lot of difference
$field = array('ram','one','two');
and
foreach ($fields as $field)
Try defining your array with the correct variable name
$fields = array('ram','one','two');
Related
Im working on 2 json data which I need to transfer the data to separate excel files. What I did is, I made 2 function so that I can choose what file to be downloaded. The problem is when I tried to call both, the data is merged on a single excel. What I expect is it will be downloaded separately.
I tried adding sleep after the first function but didn't work, still the same output. I tried also using die after both function but only the first one is working.
Hope you help me.
SAMPLE CODE
function.php
<?php
function bjpk(){
$json = 'api link...';
$arr = json_decode(file_get_contents($json), true);
$name = $arr['code'];
header("Content-Disposition: attachment; filename=\"$name.xls\"");
header("Content-Type: application/vnd.ms-excel;");
header("Pragma: no-cache");
header("Expires: 0");
$out = fopen("php://output", 'w');
$data = array();
foreach ($arr['data'] as $key => $value) {
$data['date'] = substr($value['opentime'], 0, -9);
$data['num'] = substr($value['expect'], 4);
$codes = explode(',', $value['opencode']);
foreach ($codes as $key => $code) {
$data[$key] = $code;
}
fputcsv($out, $data,"\t");
}
fclose($out);
}
function cqssc(){
$json = 'api link..';
$arr = json_decode(file_get_contents($json), true);
$name = $arr['code'];
header("Content-Disposition: attachment; filename=\"$name.xls\"");
header("Content-Type: application/vnd.ms-excel;");
header("Pragma: no-cache");
header("Expires: 0");
$out = fopen("php://output", 'w');
$data = array();
foreach ($arr['data'] as $key => $value) {
$date = DateTime::createFromFormat('Ymd', substr($value['expect'], 0, -3));
$data['date'] = $date->format('Y-m-d');
$data['num'] = substr($value['expect'], 8);
$codes = explode(',', $value['opencode']);
foreach ($codes as $key => $code) {
$data[$key] = $code;
}
fputcsv($out, $data,"\t");
}
fclose($out);
}
index.php
<?php
require_once ('function.php');
bjpk();
cqssc();
Good day, i'm trying to export data from my table to excel with PHPExcel 1.8. Here is my script so far :
$this->load->library('excels');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
$objPHPExcel->setActiveSheetIndex(0);
$query = $this->db->query("SELECT * FROM RESIGN");
$fields = $query->list_fields();
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
$col++;
}
$row = 2;
foreach($query->result() as $data)
{
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
$col++;
}
$row++;
}
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Products_'.date('dMy').'.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
when i run that function. Excel file will be downloaded , inside the excel file i receive this error.
<b>Fatal error</b>: Call to a member function garbageCollect() on null in <b>D:\xampp\htdocs\payroll\application\third_party\PHPExcel\Writer\Excel5.php</b> on line <b>115</b><br />
Currently I have a Class for export to excel, it works perfect for English, however, in other encode , e.g. Chinese character it is wrong encoded
https://github.com/bcit-ci/CodeIgniter/wiki/Export-to-Excel-2013
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
/*
* Excel library for Code Igniter applications
* Based on: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006
* Tweaked by: Moving.Paper June 2013
*/
class Export {
function to_excel($array, $filename) {
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename=' . $filename . '.xls');
//Filter all keys, they'll be table headers
$h = array();
foreach ($array->result_array() as $row) {
foreach ($row as $key => $val) {
if (!in_array($key, $h)) {
$h[] = $key;
}
}
}
//echo the entire table headers
echo '<table><tr>';
foreach ($h as $key) {
$key = ucwords($key);
echo '<th>' . $key . '</th>';
}
echo '</tr>';
foreach ($array->result_array() as $row) {
echo '<tr>';
foreach ($row as $val)
$this->writeRow($val);
}
echo '</tr>';
echo '</table>';
}
function writeRow($val) {
echo '<td>' . utf8_decode($val) . '</td>';
}
}
Attempted add the
header("Content-type: text/html; charset=utf-8");
at the starting of the function but no luck
Thanks a lot for helping.
Instead of a custom class, used PHPExcel and works perfectly
$this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("Trial Report");
// Assign cell values
$objPHPExcel->setActiveSheetIndex(0);
//Filter all keys, they'll be table headers
$h = array();
foreach ($result as $row) {
foreach ($row as $key => $val) {
if (!in_array($key, $h)) {
$h[] = $key;
}
}
}
//Writing the first header row
foreach ($h as $key => $val) {
$val = ucwords($val);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($key, 1, $val); //first row is 1
}
$pos = 0;
foreach ($result as $r_key => $row) {
foreach ($row as $col) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($pos, ($r_key + 2), $col); //skip the first row
$pos++;
}
$pos = 0;
}
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="export.xls"');
$objWriter->save('php://output');
For Chinese Traditional (Big5)
header("Content-type: text/html; charset=big5");
few more :
Chinese Simplified (EUC)
##charset=EUC-CN
Chinese Simplified (GB2312)
##charset=gb2312
Chinese Simplified (HZ)
##charset=hz-gb-2312
Chinese Simplified (Mac)
##charset=x-mac-chinesesimp
Chinese Traditional (Big5)
##charset=big5
Chinese Traditional (CNS)
##charset=x-Chinese-CNS
Chinese Traditional (Eten)
##charset=x-Chinese-Eten
Chinese Traditional (Mac)
##charset=x-mac-chinesetrad
##charset=950
here is some fixing details and how to tut ...
I am exporting mysql data to an excel file using phpexcel library. The code works fine. But the excel file created doesn't have column name. I want to fetch them as well. Is there any built-in method for it? What adjustment do i need to make my code? Here is my current working code:
<?php
require_once 'PHPExcel/classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$rowcount = 1;
while($row = mysqli_fetch_array($rs))
{
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowcount, $row['id']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowcount, $row['title']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowcount, $row['date']);
$rowcount++;
}
$filename = "backup-" . date("h.i.s.d-m-Y") . ".xls";
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=" . $filename);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>
Use mysqli_fetch_fields for getting the field names.
I have edited your code and add column names in code. It might help you.
<?php
require_once 'PHPExcel/classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$rowcount = 1;
$fieldinfo=mysqli_fetch_fields($rs);
foreach ($fieldinfo as $val)
{
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowcount, $val->id);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowcount, $val->title);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowcount, $val->date);
}
$rowcount = 2;
while($row = mysqli_fetch_array($rs))
{
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowcount, $row['id']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowcount, $row['title']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowcount, $row['date']);
$rowcount++;
}
$filename = "backup-" . date("h.i.s.d-m-Y") . ".xls";
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=" . $filename);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>
If i am right , you were searching for something like below
Insert Title Row
$f=0;
$fChar='A';
while($f<mysql_num_fields($rs)){
$objPHPExcel->getActiveSheet()->setCellValue($fChar.'1', mysql_fetch_field($rs,$f++)->name);
$objPHPExcel->getActiveSheet()->getStyle($fChar.'1')->applyFromArray(
array(
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb'=>'E1E0F7'),
),
'font' => array(
'bold' => true,
)
)
);
$fChar++;
}
Now insert values
$fC=2;
while($row=mysql_fetch_array($rs,MYSQL_BOTH)){
$fChar='A';
$f=0;
while($f<mysql_num_fields($rs)){
$objPHPExcel->getActiveSheet()->setCellValue($fChar++.''.$fC, $row[$f++]);
}
$fC++;
}
You need to change the code something like below to show the column name and data
//this is for showing the column name
$q = mysql_query("SHOW COLUMNS FROM table_name");
if (mysql_num_rows($q) > 0) {
while ($row_q = mysql_fetch_assoc($q)) {
$col='A';
$objPHPExcel->getActiveSheet()->SetCellValue($col.$rowcount, $row_q['Field']);
$col++;
}
$rowcount++;
}
//this is for showing coulmn data based in results
while($row_data1 = mysql_fetch_assoc($rs)){
$col=0;
foreach($row_data1 as $key=>$value){
$objPHPExcel->getActiveSheet()->SetCellValueByColumnAndRow($col, $rowcount, $value);
$col++;
}
$rowcount++;
}
I want to display all the rows from a table with the corresponding column names above, which works. The problem is that it removes the first row from the results below the column names. It's as if the column row is somehow counted as a row in the while loop that displays the results, but I can't figure it out.
If I remove the column names code shown below all of the results are shown.
//COLUMN NAMES
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
All of the code shown below.
$query = "SELECT * FROM `" . $_SESSION['sess_table'] . "` ORDER by ID ASC";
if ($result = $mysqli->query($query)) {
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle($excelTitle);
$headingsrow = $result->fetch_assoc();
$headings = array_keys($headingsrow);
//COLUMN NAMES
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
//RESULTS
$rowNumber = 3;
while ($row = $result->fetch_row()) {
$col = 'A';
foreach($row as $key => $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
$objPHPExcel->getActiveSheet()->freezePane('A2');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $excelFilename . '.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
In your code,
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
you have increased the $col value instead of increasing $rowNumber value.
try this,
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$rowNumber++;
}
You're fetching the first row to retrieve your headings, but then discarding it even though it contains data that you want to write as well
if ($result = $mysqli->query($query)) {
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle($excelTitle);
$row = $result->fetch_assoc();
$headings = array_keys($row);
//COLUMN NAMES
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
//RESULTS
$rowNumber = 3;
do {
$col = 'A';
foreach($row as $key => $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
} while ($row = $result->fetch_row());
}