I am using this code to export selected data from MySQL to CSV using PHP, I am facing a little problem that when data exported to the csv file it looks like below:
First line in its correct place, but starting from the second row data shifts to the right one space, if I open the csv file in Excel, I can see the most left cell after the first row is empty.
parts destination
===================
1 9.71504E+11
1 9.71504E+11
1 96656587662
1 9.71504E+11
This is my code :
$values =mysql_query( "SELECT parts,destination from log");
$rown = 0;
$row = array();
$row[] = 'parts';
$row[] = 'destination'."\n";
$data .= join(',', $row)."\n";
while( $row_select = mysql_fetch_assoc($values) ) {
if($rown++==0)
$row = array();
$row[] = $row_select['parts'];
$row[] = $row_select['destination']."\n";
}
$data .= join(',', $row)."\n";
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: text/csv");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $data;
exit();
Would you please help?
Regards,
First line in its correct place, but starting from the second row data shifts to the right one space, if I open the csv file in Excel, I can see the most left cell after the first row is empty.
I've found out the hard way that writing CSV is more complex than one would imagine. Take, for example, escaping the values so that the delimiter is properly escaped inside values. I've created a simple class that utilises fwritecsv( ) instead of trying to render CSV myself:
EDIT: I've changed the example to your usage scenario.
<?php
/**
* Simple class to write CSV files with.
*
* #author Berry Langerak <berry#ayavo.nl>
*/
class CsvWriter {
/**
* The delimiter used. Default is semi-colon, because Microsoft Excel is an asshole.
*
* #var string
*/
protected $delimiter = ";";
/**
* Enclose values in the following character, if necessary.
*
* #var string
*/
protected $enclosure = '"';
/**
* Constructs the CsvWriter.
*/
public function __construct( ) {
$this->buffer = fopen( 'php://temp', 'w+' );
}
/**
* Writes the values of $line to the CSV file.
*
* #param array $line
* #return CsvWriter $this
*/
public function write( array $line ) {
fputcsv( $this->buffer, $line, $this->delimiter, $this->enclosure );
return $this;
}
/**
* Returns the parsed CSV.
*
* #return string
*/
public function output( ) {
rewind( $this->buffer );
$output = $this->readBuffer( );
fclose( $this->buffer );
return $output;
}
/**
* Reads the buffer.
* #return type
*/
protected function readBuffer( ) {
$output = '';
while( ( $line = fgets( $this->buffer ) ) !== false ) {
$output .= $line;
}
return $output;
}
}
/**
* Example usage, in your scenario:
*/
$values = mysql_query( "SELECT parts,destination from log" );
$csv = new CsvWriter( );
$csv->write( array( 'parts', 'destination' ) ); // write header.
while( $line = mysql_fetch_assoc( $values ) ) {
$csv->write( $line );
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: text/csv");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv->output( );
exit();
Like #ChrisPatrick points out, the line
$data .= join(',', $row)."\n";
is causing the problem, you should move this to inside the while loop and then you'll get rid of the cell skipping.
The problem is in the following line
$data .= join(',', $row)."\n";
You're concatenating all the elements in $row with commas. So the destination of one row is concatenated with the parts of the next row with a comma in between, so you get something like this:
$data = $row_select['parts'] . ',' . $row_select['destination']."\n" . ',' . $row_select['parts'] etc...
The comma after the newline creates an empty cell at the beginning of the row.
UPDATE It should work with the following
while( $row_select = mysql_fetch_assoc($values)){
$row = array();
$row[] = $row_select['parts'];
$row[] = $row_select['destination']."\n";
$data .= join(',', $row);
}
Related
I try to export an Excel file from a WordPress database users table. It works in general. But I also need to add all user_meta data into this file. How could I combine this?
My code:
<?php
//Include the wp-load.php file
include('../../../../wp-load.php');
//As this is external file, we aren't using the WP theme here. So setting this as false
define('WP_USE_THEMES', false);
global $wpdb;
$table_name = $wpdb->prefix . 'users';
$file = 'email_csv'; // ?? not defined in original code
$results = $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A);
if (empty($results)) {
return;
}
$csv_output = '"'.implode('";"',array_keys($results[0])).'";'."\n";;
foreach ($results as $row) {
$csv_output .= '"'.implode('";"',$row).'";'."\n";
}
$csv_output .= "\n";
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
For your custom list of meta_keys (company, firstname, address) you can define an array containing these, and add them to the CSV as trailing columns like so:
global $wpdb;
// Define your custom meta keys you want to add as columns HERE:
$meta_keys = ['company', 'firstname', `address`]; // can contain as many as you want
// This function will load the desired keys as an associative array
// in the form: [$meta_key_1 => $meta_value_1, ..., $meta_key_n => $meta_value_n]
function load_user_meta($user_id, $keys) {
$meta_row = [];
foreach ($keys as $meta_key) {
$value = get_user_meta($user_id, $meta_key, true);
$meta_row[$meta_key] = $value;
}
return $meta_row;
}
$table_name = $wpdb->prefix . 'users';
$results = $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A);
if (empty($results)) {
return;
}
// Merge the result's keys with the list of custom meta keys:
$csv_output = '"'.implode('";"',array_merge(array_keys($results[0]), $meta_keys )).'";'."\n";
foreach ($results as $row) {
// load the custom meta for this user's ID:
$custom_meta_row = load_user_meta($row['ID'], $meta_keys);
// merge the acquired user meta into the row output:
$csv_output .= '"'.implode('";"',array_merge($row, $custom_meta_row)).'";'."\n";
}
$csv_output .= "\n";
If a certain meta_key does not exist for all users, this still works since get_user_meta will produce an empty string for these cases.
Additional note: it's generally not a good idea to produce CSV output the way you do. This works as long as there are no " and ; characters anywhere in your user_meta - those characters would have to be escaped with a certain escape character. Therefore, it is better to use fputcsv on the output stream ( $outstream = fopen("php://output", 'w');).
I am working on Export to CSV in PHP. I have code that works fine it gives me output in ExcelSheet as I want.
Code snippet:
public function generate_csv() {
$data_rows = array();
$table = 'ProductDetails';
$data_rows = array();
global $wpdb, $bp;
$data= $wpdb->get_results("SELECT * FROM " . $table . "");
$fh = #fopen( 'php://output', 'w' );
foreach ($data as $u ) {
$row = array();
$row[0] = $u->productCode;
$row[1] = $u->productTitle;
$row[2] = $u->productDescription;
$row[3] = $u->specification;
$row[4] = $u->whereToBuy;
$data_rows[] = $row;
}
header("Pragma: public");
... Some more header ...
header("Content-Transfer-Encoding: binary");
fputcsv( $fh, $header_row );
foreach ( $data_rows as $data_row ) {
fputcsv( $fh, $data_row );
}
fclose( $fh );
die();
}
As you can see in code I am hard coding all column names and creating array. The problem is if phpMyAdmin add/remove column in database then to get perfect ExcelSheet necessary changes need to make in this code also. Can any one please help me to make this code dynamic.? Like what should be instead of $row[0], $row[1], $row[2].... ??
Thank You
More global approach is to use double foreaches
$data_rows=array();
foreach ($data as $u ) {
$row = array();
foreach ($u as $field)
{
$row[] = $field; // collect dynamic row fields
}
$data_rows[] = $row; // each row will have own array of fields
}
/// EDITED
public function generate_csv($table) // better to have table name here
{
$data_rows = array();
$data_rows = array();
global $wpdb, $bp;
$sql = "SELECT * FROM " . $table . "";
$data= $wpdb->get_results($sql);
$fh = #fopen( 'php://output', 'w' );
//following the example from: https://stackoverflow.com/a/31068464/1171074
$header_data=array();
foreach ( $wpdb->get_col( "DESC " . $table, 0 ) as $column_name ) {
$header_data[] = $column_name;
}
array_push($data_rows,$header_data); // first array will be columns names
foreach ($data as $u ) {
$row = array();
foreach ($u as $field)
{
$row[] = $field; // collect dynamic row fields
}
$data_rows[] = $row; // each row will have own array of fields
}
............ // rest of the code
}
You can use the virtual INFORMATION_SCHEMA.COLUMNS table to get the column names, like so:
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = {$table}"
This should get you pretty close, if not all the way there. It will query the table, build a headers row for the csv, then it will assemble each data row. You shouldn't need to know the row name, if you iterate the response row as value..
I apologize up front if it's a little buggy, since I don't have a PHP box handy where I'm at to verify the precise syntax.
public function generate_csv() {
global $wpdb;
global $bp;
$headers = array();
$data_rows = array();
$table = 'ProductDetails';
$data_rows = array();
$header_row;
# determine table field names
$table_sql = sprintf("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '%s'",$table);
# this will run the query and stuff field names into
$resp = $wpdb->get_results($table_sql);
foreach($resp as $row){
array_push($headers,$row[0]);
}
# get the records from the datbase
$data= $wpdb->get_results(sprintf("SELECT * FROM %s",$table));
# open output handle
$fh = #fopen( 'php://output', 'w' );
foreach ($data as $record ) {
$row = array();
foreach($record as $value){
array_push($row,$value);
}
array_push($data_rows,$row);
}
header("Pragma: public");
... Some more header ...
header("Content-Transfer-Encoding: binary");
fputcsv( $fh, $headers );
foreach ( $data_rows as $data_row ) {
fputcsv( $fh, $data_row );
}
fclose( $fh );
return;
}
I am working on exporting data from a MySQL database into an Excel spreadsheet. I have found code for doing this before and it works quite well. It does not format the spreadsheet. It was also the first PHP/MySQL site I created and the data didn't need to be pretty. The current project requires formatted data.
Yesterday, I found an article that may have allowed me to do what I need to do. Like an idiot, I didn't copy the link down. I can't find it today.
Here's my code -
<?php
require_once("includes/connection.php");
require_once("includes/functions.php");
// set $closed to 0 for development
$closed = 0;
//create a sting to allow the user to see if s/he is looking at open or closed items
if ($closed) {
$filename = FILENAME."_closed";
} else {
$filename = FILENAME."_open";
}
// $data will hold the result
$data = '<table>';
// is the row a header?
$th = FALSE;
// define the separator character
$sep = '\t';
// array for keys
$thKey = array();
// are we in the first row?
$firstRow = TRUE;
// create the query
$query = "SELECT ";
$query .= "training_requirements.Training, mechanism.mechName, location.locationName, impacted_employees.groupName, ";
$query .= "training_requirements.DateReceived, training_requirements.DateStart, training_requirements.DateDue, ";
$query .= "requester.lastName, requester.firstName, impact.impactName, training_requirements.TimeNeeded, ";
$query .= "priority.priority, training_requirements.Notes ";
$query .= "FROM ";
$query .= "training_requirements, impact, impacted_employees, location, mechanism, requester, priority ";
$query .= "WHERE impact.impactId = training_requirements.impactId ";
$query .= "AND impacted_employees.groupId = training_requirements.impEmpId ";
$query .= "AND location.locationId = training_requirements.trainLocId ";
$query .= "AND mechanism.mechId = training_requirements.mechId ";
$query .= "AND requester.requesterId = training_requirements.requesterId ";
$query .= "AND priority.id = training_requirements.Priority ";
$query .= "AND training_requirements.Closed = $closed ";
$query .= "AND training_requirements.Deleted = 0";
// run the query
$result = executeQuery($connection, $query);
// process the query
if (mysqli_num_rows($result) > 0) {
while ($resource = mysqli_fetch_assoc($result)) {
if (empty($thKey)) {
foreach($resource as $key => $value) {
$thKey[] = $key;
}
}
$data .= '<tr>';
for ($i = 0; $i < count($resource); $i++) {
if ($firstRow) {
// create the header
for ($j = 0; $j < count($resource); $j++) {
$data .= '<th>';
$data .= $thKey[$j];
$data .= '</th>';
}
$data .= '</tr><tr>';
$firstRow = FALSE;
}
$data .= '<td>';
if (isset($resource[$thKey[$i]])) {
$data .= $resource[$thKey[$i]];
} else {
$data .= ' ';
}
$data .= '</td>';
}
$data .= '</tr>';
}
$data .= '</table>';
echo $data;
}
//header('Content-type: application/excel');
//header("Content-Disposition: attachment; filename={$filename}.xls");
//header("Pragma: no-cache");
//header("Expires: 0");
?>
The code continues on to allow me to display the results in a browser. I get a proper looking table in Firefox. When I un-comment the header statements at the bottom, I get a blank Excel file. No cells, no nothing.
Using Excel 2007(12.0.6715.5000) SP3 MSO (12.0.6721.5000), PHP 5.4.24, and MySQL 5.5.40. Using plugins and libraries is not an option.
Vern
your code is just fech data from MySQL and not make any excel file .
here you are a great class for making execl file
with pure XML code without any extension
<?php
/********************************/
/* Code By Mr Korosh Raoufi */
/* WwW.k2-4u.CoM */
/********************************/
/**
* Simple excel generating from PHP5
*
* #package Utilities
* #license http://www.opensource.org/licenses/mit-license.php
* #author Oliver Schwarz <oliver.schwarz#gmail.com>
* #version 1.0
*/
/**
* Generating excel documents on-the-fly from PHP5
*
* Uses the excel XML-specification to generate a native
* XML document, readable/processable by excel.
*
* #package Utilities
* #subpackage Excel
* #author Oliver Schwarz <oliver.schwarz#vaicon.de>
* #version 1.1
*
* #todo Issue #4: Internet Explorer 7 does not work well with the given header
* #todo Add option to give out first line as header (bold text)
* #todo Add option to give out last line as footer (bold text)
* #todo Add option to write to file
*/
class Excel_XML
{
/**
* Header (of document)
* #var string
*/
private $header = "<?xml version=\"1.0\" encoding=\"%s\"?\>\n<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">";
/**
* Footer (of document)
* #var string
*/
private $footer = "</Workbook>";
/**
* Lines to output in the excel document
* #var array
*/
private $lines = array();
/**
* Used encoding
* #var string
*/
private $sEncoding;
/**
* Convert variable types
* #var boolean
*/
private $bConvertTypes;
/**
* Worksheet title
* #var string
*/
private $sWorksheetTitle;
/**
* Constructor
*
* The constructor allows the setting of some additional
* parameters so that the library may be configured to
* one's needs.
*
* On converting types:
* When set to true, the library tries to identify the type of
* the variable value and set the field specification for Excel
* accordingly. Be careful with article numbers or postcodes
* starting with a '0' (zero)!
*
* #param string $sEncoding Encoding to be used (defaults to UTF-8)
* #param boolean $bConvertTypes Convert variables to field specification
* #param string $sWorksheetTitle Title for the worksheet
*/
public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')
{
$this->bConvertTypes = $bConvertTypes;
$this->setEncoding($sEncoding);
$this->setWorksheetTitle($sWorksheetTitle);
}
/**
* Set encoding
* #param string Encoding type to set
*/
public function setEncoding($sEncoding)
{
$this->sEncoding = $sEncoding;
}
/**
* Set worksheet title
*
* Strips out not allowed characters and trims the
* title to a maximum length of 31.
*
* #param string $title Title for worksheet
*/
public function setWorksheetTitle ($title)
{
$title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);
$title = substr ($title, 0, 31);
$this->sWorksheetTitle = $title;
}
/**
* Add row
*
* Adds a single row to the document. If set to true, self::bConvertTypes
* checks the type of variable and returns the specific field settings
* for the cell.
*
* #param array $array One-dimensional array with row content
*/
private function addRow ($array)
{
$cells = "";
foreach ($array as $k => $v):
$type = 'String';
if ($this->bConvertTypes === true && is_numeric($v)):
$type = 'Number';
endif;
$v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
$cells .= "<Cell><Data ss:Type=\"$type\">" . $v . "</Data></Cell>\n";
endforeach;
$this->lines[] = "<Row>\n" . $cells . "</Row>\n";
}
/**
* Add an array to the document
* #param array 2-dimensional array
*/
public function addArray ($array)
{
foreach ($array as $k => $v)
$this->addRow ($v);
}
/**
* Generate the excel file
* #param string $filename Name of excel file to generate (...xls)
*/
public function generateXML ($filename = 'excel-export')
{
// correct/validate filename
$filename = preg_replace('/[^aA-zZ0-9\_\-]/', '', $filename);
// deliver header (as recommended in php manual)
header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");
// print out document to the browser
// need to use stripslashes for the damn ">"
echo stripslashes (sprintf($this->header, $this->sEncoding));
echo "\n<Worksheet ss:Name=\"" . $this->sWorksheetTitle . "\">\n<Table>\n";
foreach ($this->lines as $line)
echo $line;
echo "</Table>\n</Worksheet>\n";
echo $this->footer;
}
}
?>
usage:
$xls = new Excel_XML('UTF-8', false, 'title text');
$data = array(
1 => array ('column1 text', 'coloumn2 text'),
1 => array ('column1 text', 'coloumn2 text'),
1 => array ('column1 text', 'coloumn2 text'),
);
$xls -> addArray($data);
$xls -> generateXML('file Name'); // its print code to browser use requred header to download
I have been using stack overflow for a good couple of years but only now made an account because I have never become so stuck that I needed to ask a question (not because I'm smart, because my code is simple!), anyway on to my question;
I am exporting a table in .xls format via PHP with information from a few tables in my MYSQL table. all the information and styling works perfectly. The only problem is that when it opens in office 2007 i get this error message:
"the file you are trying to open, 'export.xls', is a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Are you sure you want to view this file?"
So to sum up, all information is pulled from DB successful and all the styling works - I know this to be true because when I click 'yes' on the dialog box that pops up I see everything laid-out perfectly.
Below is the Excel exporter class:
/**
* Class for generating xml with multiple spreadsheets
* #author Marin Crnković
* #version 0.9
* #update_date 21.01.2009
*/
class excel_xml {
var $xml_data;
var $nl;
var $tab;
var $cols;
var $rows;
var $worksheets;
var $counters;
var $xml;
/**
* Constructor
*/
function excel_xml(){
$this->column_width = 150;
$this->debug = false;
$this->cols = array();
$this->row_array = array();
$this->rows = array();
$this->worksheets = array();
$this->counters = array();
$this->nl = "\n";
$this->tab = "\t";
}
/**
* Set debug
*/
function debug() {
$this->debug = true;
}
/**
* Generate xml
* #returns string
*/
function generate() {
// Create header
$xml = $this->create_header().$this->nl;
// Put all worksheets
$xml .= join('', $this->worksheets).$this->nl;
// Finish with a footer
$xml .= $this->create_footer();
$this->xml = $xml;
return $this->xml;
}
/**
* Create worksheet
* Uppon creating a worksheet, delete counters
* #param string $worksheet_name: name of the worksheet
*/
function create_worksheet($worksheet_name) {
$worksheet = '<Worksheet ss:Name="'.$worksheet_name.'">';
$worksheet .= $this->create_table();
$worksheet .= '<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>';
// Unset the counters and rows so you can generate another worksheet table
$this->counters = array();
$this->row_array = array();
$this->rows = '';
// Add generated worksheet to the worksheets array
$this->worksheets[] = $worksheet;
}
/**
* Create table
* #returns string
*/
function create_table() {
// Create rows with the method that automaticaly sets counters for number of columns and rows
$rows = $this->create_rows();
// Table header
$table = '<Table ss:ExpandedColumnCount="'.$this->counters['cols'].'" ss:ExpandedRowCount="'.$this->counters['rows'].'" x:FullColumns="1" x:FullRows="1">'.$this->nl;
// Columns data (width mainly)
for($i = 1; $i <= $this->counters['cols']; $i++) {
$table .= '<Column ss:Index="'.$i.'" ss:Width="'.$this->column_width.'" />'.$this->nl;
}
// Insert all rows
$table .= join('', $rows);
// End table
$table .= '</Table>'.$this->nl;
return $table;
}
/**
* Add another row into the array
* #param mixed $array: array with row cells
* #param mixed $style: default null, if set, adds style to the array
*/
function add_row($array, $style = null) {
if(!is_array($array)) {
// Asume the delimiter is , or ;
$array = str_replace(',', ';', $array);
$array = explode(';', $array);
}
if(!is_null($style)) {
$style_array = array('attach_style' => $style);
$array = array_merge($array, $style_array);
}
$this->row_array[] = $array;
}
/**
* Create rows
* #returns array
*/
function create_rows() {
$row_array = $this->row_array;
if(!is_array($row_array)) return;
$cnt = 0;
$row_cell = array();
foreach($row_array as $row_data) {
$cnt++;
// See if there are styles attached
$style = null;
if($row_data['attach_style']) {
$style = $row_data['attach_style'];
unset($row_data['attach_style']);
}
// Store the counter of rows
$this->counters['rows'] = $cnt;
$cells = '';
$cell_cnt = 0;
foreach($row_data as $key => $cell_data) {
$cell_cnt++;
$cells .= $this->nl.$this->prepare_cell($cell_data, $style);
}
// Store the number of cells in row
$row_cell[$cnt][] = $cell_cnt;
$this->rows[] = '<Row>'.$cells.$this->nl.'</Row>'.$this->nl;
}
// Find out max cells in all rows
$max_cells = max($row_cell);
$this->counters['cols'] = $max_cells[0];
return $this->rows;
}
/**
* Prepare cell
* #param string $cell_data: string for a row cell
* #returns string
*/
function prepare_cell($cell_data, $style = null) {
$str = str_replace("\t", " ", $cell_data); // replace tabs with spaces
$str = str_replace("\r\n", "\n", $str); // replace windows-like new-lines with unix-like
$str = str_replace('"', '""', $str); // escape quotes so we support multiline cells now
preg_match('#\"\"#', $str) ? $str = '"'.$str.'"' : $str; // If there are double doublequotes, encapsulate str in doublequotes
// Formating: bold
if(!is_null($style)) {
$style = ' ss:StyleID="'.$style.'"';
} elseif (preg_match('/^\*([^\*]+)\*$/', $str, $out)) {
$style = ' ss:StyleID="bold"';
$str = $out[1];
}
if (preg_match('/\|([\d]+)$/', $str, $out)) {
$merge = ' ss:MergeAcross="'.$out[1].'"';
$str = str_replace($out[0], '', $str);
}
// Get type
$type = preg_match('/^([\d]+)$/', $str) ? 'Number' : 'String';
return '<Cell'.$style.$merge.'><Data ss:Type="'.$type.'">'.$str.'</Data></Cell>';
}
/**
* Create header
* #returns string
*/
function create_header() {
if (is_array($this->styles)) {
$styles = join('', $this->styles);
}
$header = <<<EOF
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<DownloadComponents/>
<LocationOfComponents HRef="file:///\\"/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>12525</WindowHeight>
<WindowWidth>15195</WindowWidth>
<WindowTopX>480</WindowTopX>
<WindowTopY>120</WindowTopY>
<ActiveSheet>0</ActiveSheet>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="bold">
<Font ss:Bold="1" />
</Style>
$styles
</Styles>
EOF;
return $header;
}
/**
* Add style to the header
* #param string $style_id: id of the style the cells will reference to
* #param array $parameters: array with parameters
*/
function add_style($style_id, $parameters) {
foreach($parameters as $param => $data) {
switch($param) {
case 'size':
$font['ss:Size'] = $data;
break;
case 'font':
$font['ss:FontName'] = $data;
break;
case 'color':
case 'colour':
$font['ss:Color'] = $data;
break;
case 'bgcolor':
$interior['ss:Color'] = $data;
break;
case 'bold':
$font['ss:Bold'] = $data;
break;
case 'italic':
$font['ss:Italic'] = $data;
break;
case 'strike':
$font['ss:StrikeThrough'] = $data;
break;
}
}
if(is_array($interior)) {
foreach($interior as $param => $value) {
$interiors .= ' '.$param.'="'.$value.'"';
}
$interior = '<Interior ss:Pattern="Solid"'.$interiors.' />'.$this->nl;
}
if(is_array($font)) {
foreach($font as $param => $value) {
$fonts .= ' '.$param.'="'.$value.'"';
}
$font = '<Font'.$fonts.' />'.$this->nl;
}
$this->styles[] = '
<Style ss:ID="'.$style_id.'">
'.$interior.$font.'
</Style>';
}
/**
* Create footer
* #returns string
*/
function create_footer() {
return '</Workbook>';
}
/**
* Output as download
*/
function download($filename) {
if(!strlen($this->xml)) $this->generate();
header("Cache-Control: public, must-revalidate");
header("Pragma: no-cache");
header("Content-Length: " .strlen($this->xml) );
header("Content-Type: application/vnd.ms-excel");
if(!$this->debug){
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
} else {
header("Content-Type: text/plain");
}
print $this->xml;
exit;
}
}
And this is my implementation with sensitive information censored;
$excel = new excel_xml();
$header_style = array(
'bold' => 1,
'size' => '14',
'color' => '#FFFFFF',
'bgcolor' => '#4F81BD'
);
$excel->add_style('header', $header_style);
$header_style2 = array(
'bold' => 1,
'color' => '#FFFFFF',
'bgcolor' => '#92d050'
);
$excel->add_style('green', $header_style2);
$header_style3 = array(
'bold' => 1,
'color' => '#FFFFFF',
'bgcolor' => '#c00000'
);
$excel->add_style('error', $header_style3);
$header_style4 = array(
'bold' => 1,
'color' => '#FFFFFF',
'bgcolor' => '#e46d0a'
);
$excel->add_style('orange', $header_style4);
$header_style5 = array(
'bold' => 1,
'color' => '#FFFFFF',
'bgcolor' => '#c00000'
);
$excel->add_style('red', $header_style5);
/**
* Add row and attach the style "header" to it
*/
$excel->add_row(array(
'col1',
'col2',
'col3',
'col4',
'col5'
), 'header');
/**
* Add some rows, if you encapsulate the string inside asterisks,
* they will get bold using the predefined style "bold"
* If you append "|x" where x is a number, that cell will be
* merged with the x following cells
*/
$excel->add_row(array(
$Queried_Info_From_DB1,
$Queried_Info_From_DB2,
$Queried_Info_From_DB3,
$Queried_Info_From_DB4,
$Queried_Info_From_DB5
), 'red');
if(mysql_num_rows($result) == 0){
$excel->add_row(array(
'You have no info to display!|4'
), 'error');
}
/**
* Tell the object to create the worksheet.
* The passed string is the name of the worksheet
*/
$excel->create_worksheet('Your info');
/**
* If you invoke the generate method, you will get the
* XML returned or...
*/
$xml = $excel->generate();
/**
* ... you can pass the whole thing for download with
* the passed string as the filename
*/
$excel->download('Export');
You're writing a file that's spreadsheetML format, not BIFF (.xls) format, so MS Excel will complain at this discrepancy. Try saving it with a file extension of .xml (because that's what you're actually creating)
Im just getting started with PHP, and I need to export a table from my sqlite database to a CSV or ideally XLS. I have found an example using mysql, but i cant convert it to work with sqlite.
Here is what i have so far:
<?php
$db = new PDO('sqlite:../ccm.sqlite');
$query = $db->query('SELECT * FROM Emails');
$export = sqlite_query ($query);
$fields = sqlite_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ ){
$header .= sqlite_field_name( $export , $i ) . "\t";
}
while( $row = sqlite_fetch_row( $export ) ){
//sqlite_fetch_row doesnt actually exist...
$line = '';
foreach( $row as $value ){
if ( ( !isset( $value ) ) || ( $value == "" ) ){
$value = "\t";
}else{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" ){
$data = "\n(0) Records Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=emails.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
Can anyone help me out with this?
or if there is a simpler way that would be great.
Cheers
You are mixing PDO methods with sqlite_* functions; try using one or the other:
$db = new PDO("sqlite:../ccm.sqlite");
$query = $db->query("select * from emails");
$first_row = true;
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
if ($first_row)
{
// I'm not sure how to get the field names using a PDO method but
// we can use the first row's (or any row's) key values as these
// are the field names.
$first_row = false;
$number_of_fields = count($row);
$field_names = array_keys($row);
$first_field_name = $field_names[0];
}
// do stuff here with the row
print_r($row);
}
or
$db = sqlite_open("../cmm.sqlite");
$query = sqlite_query($db, "select * from emails");
$number_of_fields = sqlite_num_fields($query);
$first_field_name = sqlite_field_name($query, 0);
while ($row = sqlite_fetch_array($query))
{
// do stuff here with the row.
print_r($row);
}
I'm not 100% sure but I think PDO works with sqlite3 databases and sqlite_* functions works with sqlite2 databases.
If I need to quickly get data out of a sqlite3 database as CSV files, I use the sqlite3 CLI:
$ sqlite3 ccm.sqlite
sqlite> .mode csv
sqlite> .output emails.csv
sqlite> .headers on
sqlite> select * from emails
sqlite> .output stdout
sqlite> .quit
$ cat emails.csv
This starts the sqlite3 CLI opening the ccm.sqlite database, sets the output mode to csv (the format of select statements), sets output to the file named emails.csv, turns select column headers on (optional), selects all the data in the emails table, sets output to standard out (closing the emails.csv file), quits the CLI and checks the output by sending it to standard out.
There are other formats you can output, type .help at the sqlite3 CLI prompt:
.mode MODE ?TABLE? Set output mode where MODE is one of:
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by .separator string
tabs Tab-separated values
tcl TCL list elements
I've improved upon Stacey's answer, and thought I'll share it here.
I've added headers to make the browser download the output as a CSV file.
<?
// Set headers to make the browser download the results as a csv file
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
// Connect to DB
$conn = new PDO('sqlite:db_name.db');
// Query
$query = $conn->query("SELECT * FROM some_table");
// Fetch the first row
$row = $query->fetch(PDO::FETCH_ASSOC);
// If no results are found, echo a message and stop
if ($row == false){
echo "No results";
exit;
}
// Print the titles using the first line
print_titles($row);
// Iterate over the results and print each one in a line
while ($row != false) {
// Print the line
echo implode(array_values($row), ",") . "\n";
// Fetch the next line
$row = $query->fetch(PDO::FETCH_ASSOC);
}
// Prints the column names
function print_titles($row){
echo implode(array_keys($row), ",") . "\n";
}
If you need to send a CSV file directly to the browser, without writing in an external file, you can open the output and use fputcsv on it.
<?php
$out = fopen('php://output', 'w');
// print column header
fputcsv($out, array_keys($row)));
//or print content directly
fputcsv($out, array_values($row)));
fclose($out);
?>