I want to generate excelsheet using php so which method or code is preferable to use to generate dynamic excelsheet? Please check a screenshot image for more idea what I am facing issue.!
screenshot of required excel sheet format
Please, anyone can help me to sort out these issue? Thanks in advance.
I am using below code example.
$result = mysql_query("SELECT * FROM ".$pre."customers");
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++)
{
$headers[] = mysql_field_name($result , $i);
mysql_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result)
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export1.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysql_fetch_row($result))
{
fputcsv($fp, array_values($row));
}
die;
}
But I don't that I can lock any cell into this excel sheet. So which php method or class are suitable to generate excelsheet for my purpose to use?
public function get_excel($err = array())
{
// Clear any previous output
ob_end_clean();
// I assume you already have your $result
$search = ($this->uri->segment(2))?$this->uri->segment(2):'key';
$result=mysql_query("select * from job");
$num_fields = mysql_num_fields($result);
// Fetch MySQL result headers
$headers = array();
//$headers[] = "serial no";
for ($i = 0; $i < $num_fields; $i++) {
$headers[] = strtoupper(mysql_field_name($result , $i));
}
// Filename with current date
$current_date = date("y/m/d");
$filename = "MyFileName" . $current_date . ".csv";
// Open php output stream and write headers
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.$filename);
header('Pragma: no-cache');
header('Expires: 0');
// Write mysql headers to csv
fputcsv($fp, $headers);
//$row_tally = 0;
// Write mysql rows to csv
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
// $row_tally = $row_tally + 1;
//echo $row_tally.",";
fputcsv($fp, array_values($row));
}
die;
}
}
It seems like you want more out of CSV than possible. Are you aware of the import functions in excel? You can easily set up the document to recieve only the dynamic content as CSV (lower part) from your own php-service etc...
My version of Excel is danish but its under det data-tab and something like "from internet" :)
Related
I have a php script that exports a mysql database table to a csv file. For one of the fields of data, a user included an octothorp # in their text. For example, My Idea #1.
When my code gets to this piece of data, it stops at the # symbol. I'm guessing it hits the # and believes everything else that follows is a comment.
As a temporary solution, I am stripping out this symbol when adding the data to the database, but this is not ideal. Given the script below, how might I escape this symbol to prevent this from happening?
$query = "SELECT * FROM my_table";
$result = #mysqli_query($dbc, $query);
if (!$result) {
die('Couldn\'t fetch records');
}
$num_fields = mysqli_num_fields($result);
$headers = [];
for ($i = 0; $i < $num_fields; $i++) {
$field = mysqli_fetch_field_direct($result, $i);
// var_dump($field);
$headers[] = $field->name;
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}
i am trying to fetch all data from a table and save into a csv file.
i did not get any error but it is not creating the csv file with the result as well.
but when i see the output i see that the results is there but why the csv file is not creating for the result!!
PHP code --
$result = mysql_query('SELECT * FROM `info_contact`');
if (!$result){
die('Couldn\'t fetch records');
}
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++) {
$headers[] = mysql_field_name($result, $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="myCsvFile.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysql_fetch_row($result)) {
fputcsv($fp, array_values($row));
}
die;
}
Output ---
id,name,email,message,content,contactTime
34,chris,christoferhansen#gmail.com,"Hi Chris","C:fakepathCoding style.pdf","2015-11-29 19:49:02"
35,Hansen,hansen#yahoo.com,ad,,"2015-11-29 19:52:20"
Where is my mistake for not being able to create a cvs file !
Can anyone help me with this please
You script create csv for download. If you have write it to file on server than replace $fp = fopen('php://output', 'w'); with $fp = fopen('path/to/file.csv', 'w'); and remove all header calls
I have a script that downloads results from a MySql table into an CSV file like so
$result = $sql2;
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++) {
$headers[] = mysql_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysql_fetch_assoc($result)) {
fputcsv($fp, array_values($row));
}
die;
}
Which works fine, the problem i am having is some of the values of columns contain & quot;. When printing this data on to the web page it shows as a real quote " but when downloading it into the CSV it prints as & quote;. I have tried to htmlspecialchars_decode() but the problem is still there.
Any help will be appreciated.
I need Name,Given phonenumber mobile notes location to all be in separate columns on my csv export using the php script below. The current code exports all of the selected data in one column for each record. Thank you!
$result = mysql_query('SELECT who as "Name,Given" , phonenumber as "mobile", notes, location FROM `phpbb_phonelist` WHERE `activenumber` = 1');
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++)
{
$headers[] = mysql_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result)
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="phonelist.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = mysql_fetch_row($result))
{
fputcsv($fp, array_values($row));
}
die;
Use this code
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings
fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));
// fetch the data
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
$rows = mysql_query('SELECT field1,field2,field3 FROM table');
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows))
fputcsv($output, $row);
I am trying to export sql to csv with php and this is what I managed to do. It works well without 2 things:
1. Before the sql data that is outputted in the csv I get the HTML code of the current page for some reason.
2. How can I change the table header rows? Want to rename the table header column in the csv.
$result = $db->query("SELECT * FROM user_history WHERE member_id = '$user_id'");
$num_fields = $result->field_count;
$headers = array();
$headers[] = "Number";
for($i = 0; $i < $num_fields; $i++ ) {
$Obj = $result->fetch_field_direct($i);
$headers[] = $Obj->name."\t";
}
$current_date = date("y/m/d");
$filename = "MyFileName" . $current_date . ".csv";
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.$filename);
header('Pragma: no-cache');
header('Expires: 0');
echo "Title of Your CSV File\n\n";
// Write mysql headers to csv
fputcsv($fp, $headers);
$row_tally = 0;
// Write mysql rows to csv
while ($row = $result->fetch_array(MYSQLI_NUM)) {
$row_tally = $row_tally + 1;
echo $row_tally.",";
fputcsv($fp, array_values($row));
}
die;
}
fetch_field_direct returns an object with no __toString() method. According to this you need to change the code:
$header .= $result->fetch_field_direct($i)."\\t";
To:
$Obj = $result->fetch_field_direct($i);
$header .= $Obj->name."\t";
Secondly if you print "\\t" then you will literally get \t. print "\t"; will give you the tab character.