How to export to excel in Arabic? - php

I am trying to export Arabic data from MySQL to Excel, but I get weird symbols,
like this: كرة
Here is my code:
<?php
$conn = new mysqli('localhost', 'root', '');
mysqli_select_db($conn, 'crud');
mysqli_query($conn, "set names 'utf8'");
$sql = "SELECT `userid`,`first_name`,`last_name` FROM `employee`";
$setRec = mysqli_query($conn, $sql);
$columnHeader = '';
$columnHeader = "User Id" . "\t" . "First Name" . "\t" . "Last Name" . "\t";
$setData = '';
while ($rec = mysqli_fetch_row($setRec)) {
$rowData = '';
foreach ($rec as $value) {
$value = '"' . $value . '"' . "\t";
$rowData .= $value;
}
$setData .= trim($rowData) . "\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=User_Detail.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo ucwords($columnHeader) . "\n" . $setData . "\n";
?>
How can I properly export this data?

According to this answer, the only encoding that seems to work consistently is UTF-16LE with a BOM. Try editing your code accordingly:
<?php
$conn = new mysqli('localhost', 'root', '');
mysqli_select_db($conn, 'crud');
mysqli_query($conn, "set names 'utf8'");
$sql = "SELECT `userid`,`first_name`,`last_name` FROM `employee`";
$setRec = mysqli_query($conn, $sql);
$columnHeader = '';
$columnHeader = "User Id" . "\t" . "First Name" . "\t" . "Last Name" . "\t";
$setData = '';
while ($rec = mysqli_fetch_row($setRec)) {
$rowData = '';
foreach ($rec as $value) {
$value = '"' . $value . '"' . "\t";
$rowData .= $value;
}
$setData .= trim($rowData) . "\n";
}
// convert to UTF-16 and add BOM
$setData = chr(255) . chr(254) . mb_convert_encoding($setData, "UTF-16LE", "UTF-8");
// add encoding in headers
header("Content-Encoding: UTF-16LE");
header("Content-type: text/csv; charset=UTF-16LE");
header("Content-Disposition: attachment; filename=User_Detail.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo ucwords($columnHeader) . "\n" . $setData . "\n";
?>
Though you should really be using functions that will do escaping and such for you, like fputcsv() at a minimum.

Related

When i try to generate Excel Report using PHP, values not getting generated,

When i try to generate Excel Report using PHP, values not getting generated, I need to loop the mysqli_stmt_bind_result to excel file using while fetch
mysqli_stmt_bind_result($setRec, $emp_no, $emp_name, $designation, $location, $attend_date);
mysqli_stmt_store_result($setRec);
$resultCheck2 = mysqli_stmt_num_rows($setRec);
echo "result num_rowsss = $resultCheck2 \n";
// $setRec = mysqli_query($link, $setSql);
$columnHeader = '';
$columnHeader = "Emp No" . "\t" . "Emp Name" . "\t" . "Designation" . "\t" . "Location" . "\t" . "Date" . "\t" . "Attendance" . "\t" . "Over Time" . "\t". "Remarks" . "\t";
$setData = '';
while ($rec = mysqli_stmt_fetch($setRec)) {
$rowData = '';
foreach ($rec as $value) {
$value = '"' . $value . '"' . "\t";
$rowData .= $value;
}
$setData .= trim($rowData) . "\n";
}
$filename = "Attendance_Reoprt.xls";
$FN = $pdate.'_'.$filename;
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$FN.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo ucwords($columnHeader) . "\n" . $setData . "\n";
mysqli_close($link);
}

All fields are showing in one column when exporting mysqli data to excel using php

I have written a simple php script for download excel file which contain some MySQLI data fetching by a MySQLI query, it was working fine when I was using English data but when I used utf 8 language such as Arabic, it shows all the data in one column in the excel file,
here is the code, any help will be appreciated.
<?php
$conn = new mysqli('localhost', 'root', '');
mysqli_select_db($conn, 'test_db');
mysqli_query($conn, "set names 'utf8'");
setlocale(LC_ALL, 'en_US.UTF-8');
$sql = "SELECT `user_name`,`user_q1`,`user_q2`,
`user_q3`,`user_q4`,`user_q5`
FROM `usertable`";
$setRec = mysqli_query($conn, $sql);
$columnHeader = '';
$columnHeader = "الاسم" . "\t" . "الاجابة 1" . "\t" . "الاجابة 2" . "\t". "الاجابة 3" . "\t". "الاجابة 4" . "\t". "الاجابة 5" . "\t";
$setData = '';
while ($rec = mysqli_fetch_row($setRec)) {
$rowData = '';
foreach ($rec as $value) {
$value = '"' . $value . '"' . "\t";
$rowData .= $value;
}
$setData .= trim($rowData) . "\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=User_Detail.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo ucwords($columnHeader) . "\n" . $setData . "\n";
?>

Phone Number Automatically Add When Export To Excel File

I have +82-2-573-3585 in my database but when I export .xls file using the following codes below the number add automatically so it result to -4078. How can I make the phone number in my excel file the same with my database phone number?
<?php
$conn = new mysqli('localhost', 'root', '');
mysqli_select_db($conn, 'databasename');
$setSql = "SELECT contact_number FROM supplier";
$setRec = mysqli_query($conn, $setSql);
$columnHeader = 'SUPPLIERS';
$columnHeader = "Mobile Number" . "\t";
$setData = '';
while ($rec = mysqli_fetch_row($setRec)) {
$rowData = '';
foreach ($rec as $value) {
$value = '"' . $value . '"' . "\t";
$rowData .= $value;
}
$setData .= trim($rowData) . "\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Machine_Supplier.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo ucwords($columnHeader) . "\n" . $setData . "\n";
?>
Please try this following code.
<?php
$conn = new mysqli('localhost', 'root', '');
mysqli_select_db($conn, 'databasename');
$setSql = "SELECT contact_number FROM supplier";
$setRec = mysqli_query($conn, $setSql);
$columnHeader = 'SUPPLIERS';
$columnHeader = "Mobile Number" . "\t";
$setData = '';
while ($rec = mysqli_fetch_row($setRec)) {
$rowData = '';
foreach ($rec as $value) {
$value = '"' . $value . '"' . "\t";
$rowData .= strval($value);
}
$setData .= trim($rowData) . "\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Machine_Supplier.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo ucwords($columnHeader) . "\n" . $setData . "\n";
?>
Hope it works

PHP EXPORT MYSQL TO CSV - got column headers, can't get rest of data?

I can't seem to figure out why my table data won't appear in the csv file.
I have 1 page with a form submit to start the export process.
I am getting the correct column headers when I open the file with excel...
Thanks for the help!!
if(isset ($_POST['submit'])) {
$host = "";
$user = "";
$password = "";
$db = "";
$con = mysql_connect("$host", "$user", "$password") or die("Couldn't connect to server.");
$db = mysql_select_db("$db", $con) or die ("Couldn't select database.");
$table = "wp_users";
function exportMysqlToCsv($table,$filename = 'content.csv') {
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$query = "select * from $table";
$result = mysql_query($query);
$rowcount = mysql_num_fields($result);
$data = '';
for ($i = 0; $i < $rowcount; $i++) {
$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
mysql_field_name($result, $i)) . $csv_enclosed;
$data .= $l;
$data .= $csv_separator; }
$output = trim(substr($data, 0, -1));
$output .= $csv_terminated;
while ($row = mysql_fetch_array($result)) {
$data = '';
for ($j = 0; $j < $fields_cnt; $j++) {
if ($row[$j] == '0' || $row[$j] != '') {
if ($csv_enclosed == '') { $data .= $row[$j]; }
else { $data .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed; } }
else { $data .= ''; }
if ($j < $rowcount - 1) { $data .= $csv_separator; } }
$output .= $schema_insert;
$output .= $csv_terminated; }
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($output));
header("Content-type: text/x-csv");
//header("Content-type: text/csv");
//header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
echo $output;
exit;
}
exportMysqlToCsv($table);
}
The problem appears to be that you are assigning the CSV string to the $data variable but you are not appending it to $output. You need a line like:
$output .= $data;
just before:
$output .= $csv_terminated;
Secondly, I really recommend you to fix the code structure. It is very difficult to read the code the way it is right now. Read PSR standards for more information (or there are tons of decent standards).

Using Session variable for loop in php (3php script) with form checkbox for Excel sheet

<?php
session_start();
// $filename = "Report_" . date('MmDY') . ".xls";
// header("Content-type: application/octet-stream");
// header("Content-Disposition: attachment; filename=\"$filename\"");
// header("Pragma: no-cache");
// header("Expires: 0");
include ('../connection.php');
include ('../libchart/classes/libchart.php');
$calendarDate1 = $_SESSION['dateCalendarONE'];
$calendarDate2 = $_SESSION['dateCalendarTWO'];
$inputValue = $_SESSION['menuInputVALUE'];
$sql = ("
SELECT *
FROM `tally`, `category`
WHERE tally.catid = category.catid AND
date BETWEEN '" . $calendarDate1 . "' AND '" . $calendarDate2 . "'
AND CATLABEL='" . $inputValue . "'
GROUP BY `catlabel`
");
$rec = mysql_query($sql) or die (mysql_error());
$num_fields = mysql_num_fields($rec);
//loop count
for($i = 0; $i < $num_fields; $i++ )
{
$header .= mysql_field_name($rec,$i)."\t";
}
//Oraganize information on the database.
while($row = mysql_fetch_assoc($rec))
{
$line = '';
foreach($row as $brotocol)
{
if((!isset($brotocol)) || ($brotocol == ""))
{
$brotocol = "\t";
}
else
{
$brotocol = str_replace( '"' , '""' , $brotocol );
$brotocol = '"' . $brotocol . '"' . "\t";
}
$line .= $brotocol;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace("\r" , "" , $data);
if ($data == "")
{
$data = "\n BROTOCOL_ERROR: contact support!!!\n";
}
print "$header\n$data";
?>
When the session variable is passed, the exporting for excel is not looping through the and only reading 1 row. but When the sql statement is hard-coded in, it loops through as many rows as needed. Is the error with the loop alogrithm? or do I have to add another while loop in the code?

Categories