Wrong encode string when export the excel using PHP - php

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 ...

Related

Data from separate function being merged in excel

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();

PHPExcel Multi Sheet Loop Issue

I'm developing a small class that will allow you to pass queries and it will create a worksheet for each query.
FYI: This class is still in development and I will be reducing down into smaller functions.
My issue is that for some reason my sheet increment is off and I cant figure out where to put it.
I am calling my class like this:
$ex2 = new ExportToExcel2('Somefile');
$ex2->AddSheet('Sheet1', 'Select * from Division;');
$ex2->AddSheet('Sheet2', 'Select * from Zone');
$ex2->ExportMultiSheet();
I should have two tabs, "Sheet1" and "Sheet2". This is how my sheet ends up looking. All the data is on Sheet1 and Worksheet.
Here is my class:
class ExportToExcel2 {
public $AllSheetData = [];
protected $SheetData = [];
protected $PHPExcel = '';
protected $FileName = '';
function __construct($_filename) {
$this->FileName = $_filename;
$this->PHPExcel = new PHPExcel;
}
public function AddSheet($_WorkSheetName, $_Query) {
$this->SheetData['Sheet_Name'] = $_WorkSheetName;
$this->SheetData['Query'] = $_Query;
$this->AllSheetData[] = $this->SheetData;
unset($this->SheetData);
}
public function ExportMultiSheet() {
$Result='';
$count=0;
$this->PHPExcel->setActiveSheetIndex(0);
foreach($this->AllSheetData as $subarray)
{
foreach($subarray as $key => $value)
{
if($count>0)
{
$this->PHPExcel->createSheet($count);
$this->PHPExcel->setActiveSheetIndex($count);
}
if($key == 'Query') {
$Result = dbQuery($value);
//set header row
$row = 1; // 1-based index
$row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC);
$col = 0;
foreach(array_keys($row_data) as $key) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $key);
$col++;
}
//set body rows
$row2 = 2;
while($row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC)) {
$col2 = 0;
foreach($row_data as $key=>$value) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col2, $row2, $value);
$col2++;
}
$row2++;
}
$count++;
}
if($key =='Sheet_Name') {
$this->PHPExcel->getActiveSheet()->setTitle($value);
}
//set all columns to align left
$this->PHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
//show gridlines?
$this->PHPExcel->getActiveSheet()->setShowGridlines(true);
//set columns a through z to auto width
for($col = 'A'; $col !== 'Z'; $col++) {
$this->PHPExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}
}
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
}
Any ideas on where to place my $count++?
Solved, Here is the finished class(until its not finished again)
class ExportToExcel2 {
public $AllSheetData = [];
protected $SheetData = [];
protected $PHPExcel = '';
protected $FileName = '';
function __construct($_filename) {
$this->FileName = $_filename;
$this->PHPExcel = new PHPExcel;
//clean the output buffer before download
ob_clean();
}
public function AddSheet($_WorkSheetName, $_Query) {
$this->SheetData['Sheet_Name'] = $_WorkSheetName;
$this->SheetData['Query'] = $_Query;
$this->AllSheetData[] = $this->SheetData;
unset($this->SheetData);
}
public function ExportMultiSheet($_ExportType='xls') {
if(!empty($this->AllSheetData)) {
$count=0;$Result='';
$this->PHPExcel->setActiveSheetIndex(0);
foreach($this->AllSheetData as $subarray) {
if($count>0){
$this->PHPExcel->createSheet(null);
$this->PHPExcel->setActiveSheetIndex($count);
}
$count++;
foreach($subarray as $key => $value) {
if($key == 'Query') {
$Result = dbQuery($value);
$this->SetHeaderCells($Result);
$this->SetbodyCells($Result);
}
if($key =='Sheet_Name') {
$this->PHPExcel->getActiveSheet()->setTitle($value);
}
}
}
$this->ExportType($_ExportType);
}
}
public function ExportSingleSheet($_Query, $_ExportType='xls') {
$Result = dbQuery($_Query);
$this->SetHeaderCells($Result);
$this->SetBodyCells($Result);
$this->SetProperties();
$this->ExportType($_ExportType);
}
private function ExportType($_ExportType) {
if($_ExportType=='xls') {
$this->DownloadXLS();
}
else if($_ExportType=='csv') {
$this->DownloadCSV();
}
}
private function SetProperties() {
//set all columns to align left
$this->PHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
//show gridlines?
$this->PHPExcel->getActiveSheet()->setShowGridlines(true);
//set columns a through z to auto width
for($col = 'A'; $col !== 'Z'; $col++) {
$this->PHPExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}
//set the first sheet to open first
$this->PHPExcel->setActiveSheetIndex(0);
}
private function DownloadXLS() {
$this->SetProperties();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$this->FileName.'-'.date("y.m.d").'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
private function DownloadCSV() {
$this->SetProperties();
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="'.$this->FileName.'-'.date("y.m.d").'.csv"');
header('Cache-Control: max-age=0');
$objWriter = new PHPExcel_Writer_CSV($this->PHPExcel);
$objWriter->save("php://output");
exit;
}
private function SetHeaderCells($Result) {
$row = 1; // 1-based index
$row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC);
$col = 0;
foreach(array_keys($row_data) as $key) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $key);
$col++;
}
}
private function SetBodyCells($Result) {
$row2 = 4;
while($row_data = sqlsrv_fetch_array($Result, SQLSRV_FETCH_ASSOC)) {
$col2 = 0;
foreach($row_data as $key=>$value) {
$this->PHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col2, $row2, $value);
$col2++;
}
$row2++;
}
}
}
You should first move the sheet generation code from the foreach($subarray loop to your foreach($this->AllSheetData (since you want to add new sheet for every.. well.. new sheet. Not for every new sheet property).
You should then use a very similar code to the one you had, and $counter will be used only within that part of the code. Note that to create a new sheet and place is as the last one, you should simply pass null to the createSheet() method.
So your code should look like this:
public function ExportMultiSheet() {
...
$count = 0;
foreach($this->AllSheetData as $subarray)
{
if ($count > 0)
{
$this->PHPExcel->createSheet(null);
$this->PHPExcel->setActiveSheetIndex($count);
}
$count++
foreach($subarray as $key => $value)
...
}
...

Save query results to .csv with PHP

I have the following query running and I am looking at the best way to export the data to a .csv or .xls file.
<?php
$channels = ee()->db->select('channel_titles.entry_id, channel_titles.title, channel_data.field_id_164')
->from('channel_titles')
->join('channel_data', 'channel_titles.entry_id = channel_data.entry_id')
->where(array(
'channel_titles.channel_id' => '12',
))
->or_where(array(
'channel_titles.channel_id' => '31',
))
->get();
if ($channels->num_rows() > 0)
{
$i = 0;
foreach($channels->result_array() as $row)
{
$i++;
echo $row['field_id_164'].",".$row['title']."<br />\n";
}
echo $i;
}
?>
I have tried a few methods but cannot seem to figure out the best option.
The classic echo explode(',',$col) etc way is fine, but you can also write directly to the csv file using php's built in functions.
$filename = 'test.csv';
$file = fopen($filename,"w");
if ($channels->num_rows() > 0) {
foreach($channels->result_array() as $key => $row) {
if ($key==0) fputcsv($file, array_keys((array)$row)); // write column headings, added extra brace
foreach ($row as $line) {
$line = (array) $line;
fputcsv($file, $line);
}
}
}
fclose($file);
edit:
If you want to download/view the file instantly you have to set the headers.
$filename = 'test.csv';
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=' . $filename);
header("Content-Transfer-Encoding: UTF-8");
$file = fopen('php://output', 'a');
if ($channels->num_rows() > 0) {
foreach($channels->result_array() as $key => $row) {
if ($key==0) fputcsv($file, array_keys((array)$row)); // write column headings, added extra brace
foreach ($row as $line) {
$line = (array) $line;
fputcsv($file, $line);
}
}
}
fclose($file);

insert a 2d array into a csv file in php

I'm trying to insert the 2d array into a csv file
here is my code below
<?php
$cars = array( array("Volvo",100,96), array("BMW",60,59), array("Toyota",110,100));
$fp = fopen('file.xls', 'w');
foreach ($cars as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
but the values are inserted in single row in csv file.
any ideas
EDIT 1:
This version saves it to file without prompting to save and outputs in a table with a slight border. The border in the table is not written to file, it only outputs to screen.
<?php
$fp = fopen('file.xls', 'w');
$cars = array( array(Volvo,100,96), array(BMW,60,59), array(Toyota,110,100));
$titleArray = array_keys($cars[0]);
$delimiter = "\t";
$filename="file.xls";
//Loop through each subarray, which are our data sets
foreach ($cars as $subArrayKey => $subArray) {
//Separate each datapoint in the row with the delimiter
$dataRowString = implode($delimiter, $subArray);
// print $dataRowString . "\r\n"; // prints output to screen
fwrite($fp, $dataRowString . "\r\n");
} // keep this always end of routine
// start of cell formatting
$row = 1;
if (($handle = fopen("file.xls", "r")) !== FALSE) {
echo '<table border="1" cellspacing="0" cellpadding="3">';
while (($data = fgetcsv($handle, 1000, '\t')) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td align="center">'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
EDIT 2:
This version will process the data then prompt to save the file.
Output:
Volvo 100 96
BMW 60 59
Toyota 110 100
PHP code:
<?php
$cars = array( array(Volvo,100,96), array(BMW,60,59), array(Toyota,110,100));
$titleArray = array_keys($cars[0]);
$delimiter = "\t";
$filename="file.xls";
//Send headers
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
//Loop through each subarray, which are our data sets
foreach ($cars as $subArrayKey => $subArray) {
//Separate each datapoint in the row with the delimiter
$dataRowString = implode($delimiter, $subArray);
print $dataRowString . "\r\n";
}
?>
1st version:
Output will be:
Volvo
100
96
BMW
60
59
Toyota
110
100
If this is the desired result, then this is the code to accomplish this:
<?php
$cars = array( array(Volvo,100,96), array(BMW,60,59), array(Toyota,110,100));
$fp = fopen('file.xls', 'w');
foreach ($cars as $fields) {
fputcsv($fp, $fields, "\n");
}
fclose($fp);
?>
You need to seperate the contents with comma to write in seperate rows.
$export_arr =$_POST['dataString'];
$fp = fopen('file.xls', 'w');
foreach ($export_arr as $fields) {
fputcsv($fp, $fields,",");
}
fclose($fp);

Html Codes In Exported Data [PHP]

I am trying to export the data got from my database.
The problem is that the data comes with html codes.
I just want to export the data without html codes.
Note: My database doesn't have any html code.
$exported_db_datas (global array variable) is created like this:
while($row = mysql_fetch_array($resultset,MYSQL_ASSOC))
{
$resultsarray[$rowcount] = $row;
$exported_db_datas[$rowcount] = $row;
/*foreach($resultsarray[$rowcount] as $column)
{
$resultsarray2[$rowcount][] = $column;
}*/
$rowcount++;
}
Export codes :
$export_file = "export_phisto";
if ($format == "CSV")
{
$file = $export_file.".csv";
$separator = ",";
}
elseif ($format == "TAB")
{
$file = $export_file.".tab";
$separator = "\t";
}
elseif ($format == "TXT")
{
$file = $export_file.".txt";
$separator = "\t";
}
else// XLS
{
$file = $export_file.".xls";
$separator = "\t";
}
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Type: text/plain");
$flag = false;
foreach($exported_db_datas as $row)
{
if(!$flag)
{
// display field/column names as first row
echo implode($seperator, array_keys($row)) . "\r\n";
$flag = true;
}
echo implode($seperator, array_values($row)) . "\r\n";
}
Note: Even if I don't use print $data, exported data has html codes of the web site without data.
How can I just export the data I get from database?
Example exported data is was here.
Use strip_tags
$exported_db_datas[$rowcount] = strip_tags($row);
just use strip_tags() in this line $exported_db_datas[$rowcount] = $row; like so: $exported_db_datas[$rowcount] = strip_tags($row);
PS: don't use the mysql_* extension, it was deprecated, see the red warning box here

Categories