This topic is locked
[SOLVED]

  Save Excel Output

3/8/2012 7:30:39 PM
PHPRunner General questions
T
Tempus_Erus author

Hi,
Does anyone know if it is possible to save an Excel file direct to a database or directory in order I can the retrieve and send as a mail attatchment as and when required by a user?
Any pointers in achieving this welcome.
Thanks.
A

C
cgphp 3/9/2012

To accomplish this, you need some manual coding.

C
ckapote 3/9/2012

HI,
I was looking the same script but still today have not find anything .
I f you finally manage to do it please post it
thanks



Hi,
Does anyone know if it is possible to save an Excel file direct to a database or directory in order I can the retrieve and send as a mail attatchment as and when required by a user?
Any pointers in achieving this welcome.
Thanks.
A

T
Tempus_Erus author 3/9/2012



To accomplish this, you need some manual coding.


Hi,
Guessed there is an element of this. Where would I start? I would assume with the relevant _export file?
Thanks

Sergey Kornilov admin 3/9/2012

Check include/export_fucntions.php file, function ExportExcelSave(). In this function Excel file is created already and by default it being sent right to the browser.
Since we use PHPExcel class library there check http://phpexcel.codeplex.com/ for additional info and manual.

T
Tempus_Erus author 3/10/2012



Check include/export_fucntions.php file, function ExportExcelSave(). In this function Excel file is created already and by default it being sent right to the browser.
Since we use PHPExcel class library there check http://phpexcel.codeplex.com/ for additional info and manual.


Perfect, thanks.

C
ckapote 3/11/2012

you can also combine information from following link
http://www.asprunner.com/forums/topic/5856-advanced-send-email-events/
Please if you finally find it please post it
thanks



Perfect, thanks.

T
Tempus_Erus author 3/11/2012



you can also combine information from following link
http://www.asprunner.com/forums/topic/5856-advanced-send-email-events/
Please if you finally find it please post it
thanks


Will do. Currently looking at a couple of options.
A

C
ckapote 3/17/2012

hi
did you figure out , if affirmative please post it .
I am sure that many of us will be interesting to see the solution.
thanks



Will do. Currently looking at a couple of options.
A

T
Tempus_Erus author 3/25/2012

ckapote as promised here is an idea below,
I had a look at the PHPExcel and found this a brilliant piece of kit.
I did struggle to unravel the code in the export file and get it to do exactly what I wanted. The below whilst very basic does work and hopefully this can be of help to anyone wishing to create Excel files.
In the original question was not to send to the browser. Also opens as a Genuine xlsx file.
Please read the instructions at http://phpexcel.codeplex.com/ for installation and other info.
Yes appreciate the code below is not perfect and can be tidied up, but none the less should promote ideas and in any case those of you wishing to create more advance Excel exports, I would recommend.
BIG thanks to 'Admin' for pointing the way.
<?php

error_reporting(E_ALL);

date_default_timezone_set('Europe/London');

///connect to db////////

$dbhost = "";

$dbuser = "";

$dbpass = "";

$dbname = "";

mysql_connect($dbhost,$dbuser,$dbpass);

mysql_select_db($dbname);

// get the PHPExcel file

require_once 'Classes/PHPExcel.php'; //reliatve path of course

// Simple Query with a WHERE function and an ORDER function

$query = "SELECT record1, record2, record3, record4, FROM database WHERE (record1 ='avalue' OR record2 ='another value') ORDER BY xxxID DESC";// kind of a few SQL query examples in one

// create you Excel headings

$headings = array('Record1', 'record2', 'Record2'); // or what ever title you wish to give the column.

if ($result = mysql_query($query) or die(mysql_error())) {

// Create a new PHPExcel

$objPHPExcel = new PHPExcel();

// Meta data, please refer to PHPExcel documentation#
$objPHPExcel->getProperties()->setCreator("An Author")

->setLastModifiedBy("An Author")

->setTitle("A Report")

->setSubject("Report")

->setDescription("Auto generated report")

->setKeywords("office 2007 openxml php")

->setCategory("A report");
// PHPExcel is excellent and very user friendly. I have included a few bits out of the manual

// Set width to encapsulate text of heading set above

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);

$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);

$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
// Set auto filters. Here they are accross a range - standard Excel.

$objPHPExcel->getActiveSheet()->setAutoFilter('A1:C1');
// set default font, calibri for me all day!

$objPHPExcel->getDefaultStyle()->getFont()->setName('calibri');

$objPHPExcel->getDefaultStyle()->getFont()->setSize(11);
// Set tab color for current sheet.

$objPHPExcel->getActiveSheet()->getTabColor()->setRGB('00FF00');

$objPHPExcel->getActiveSheet()->setTitle('ITB Alerts Report');
//fill top row with a back ground color

$objPHPExcel->getActiveSheet()->getStyle('A1:C1')->getFill()

->setFillType(PHPExcel_Style_Fill::FILL_SOLID)

->getStartColor()->setARGB('EFFFD6');

//freeze top row. You need to inclide to columns, else you will get a pane not top row.

$objPHPExcel->getActiveSheet()->freezePane('A2');

$objPHPExcel->getActiveSheet()->freezePane('B2');

// Set headings and get data

$cell = "";

$rowNumber = 1;

$col = 'A';

foreach($headings as $cell) {

$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);

$col++;

}

// Go through the results...

$rowNumber = 2;

while ($row = mysql_fetch_row($result)) {

$col = 'A';

foreach($row as $cell) {

$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);

$col++;
}

$rowNumber++;

}

// Save Object NOT to the browser

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWriter->save(str_replace('.php', '.xlsx', FILE));

// unset and clear workbook from memory, read the PHPExcel dcumentation for details.

$objPHPExcel->disconnectWorksheets();

unset($objPHPExcel);

exit();

}

echo 'Something has happened that isn`t right!';

?>

C
ckapote 4/19/2012

thank you very much!!



ckapote as promised here is an idea below,
I had a look at the PHPExcel and found this a brilliant piece of kit.
I did struggle to unravel the code in the export file and get it to do exactly what I wanted. The below whilst very basic does work and hopefully this can be of help to anyone wishing to create Excel files.
In the original question was not to send to the browser. Also opens as a Genuine xlsx file.
Please read the instructions at http://phpexcel.codeplex.com/ for installation and other info.
Yes appreciate the code below is not perfect and can be tidied up, but none the less should promote ideas and in any case those of you wishing to create more advance Excel exports, I would recommend.
BIG thanks to 'Admin' for pointing the way.
<?php

error_reporting(E_ALL);

date_default_timezone_set('Europe/London');

///connect to db////////

$dbhost = "";

$dbuser = "";

$dbpass = "";

$dbname = "";

mysql_connect($dbhost,$dbuser,$dbpass);

mysql_select_db($dbname);

// get the PHPExcel file

require_once 'Classes/PHPExcel.php'; //reliatve path of course

// Simple Query with a WHERE function and an ORDER function

$query = "SELECT record1, record2, record3, record4, FROM database WHERE (record1 ='avalue' OR record2 ='another value') ORDER BY xxxID DESC";// kind of a few SQL query examples in one

// create you Excel headings

$headings = array('Record1', 'record2', 'Record2'); // or what ever title you wish to give the column.

if ($result = mysql_query($query) or die(mysql_error())) {

// Create a new PHPExcel

$objPHPExcel = new PHPExcel();

// Meta data, please refer to PHPExcel documentation#
$objPHPExcel->getProperties()->setCreator("An Author")

->setLastModifiedBy("An Author")

->setTitle("A Report")

->setSubject("Report")

->setDescription("Auto generated report")

->setKeywords("office 2007 openxml php")

->setCategory("A report");
// PHPExcel is excellent and very user friendly. I have included a few bits out of the manual

// Set width to encapsulate text of heading set above

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);

$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);

$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
// Set auto filters. Here they are accross a range - standard Excel.

$objPHPExcel->getActiveSheet()->setAutoFilter('A1:C1');
// set default font, calibri for me all day!

$objPHPExcel->getDefaultStyle()->getFont()->setName('calibri');

$objPHPExcel->getDefaultStyle()->getFont()->setSize(11);
// Set tab color for current sheet.

$objPHPExcel->getActiveSheet()->getTabColor()->setRGB('00FF00');

$objPHPExcel->getActiveSheet()->setTitle('ITB Alerts Report');
//fill top row with a back ground color

$objPHPExcel->getActiveSheet()->getStyle('A1:C1')->getFill()

->setFillType(PHPExcel_Style_Fill::FILL_SOLID)

->getStartColor()->setARGB('EFFFD6');

//freeze top row. You need to inclide to columns, else you will get a pane not top row.

$objPHPExcel->getActiveSheet()->freezePane('A2');

$objPHPExcel->getActiveSheet()->freezePane('B2');

// Set headings and get data

$cell = "";

$rowNumber = 1;

$col = 'A';

foreach($headings as $cell) {

$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);

$col++;

}

// Go through the results...

$rowNumber = 2;

while ($row = mysql_fetch_row($result)) {

$col = 'A';

foreach($row as $cell) {

$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);

$col++;
}

$rowNumber++;

}

// Save Object NOT to the browser

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWriter->save(str_replace('.php', '.xlsx', FILE));

// unset and clear workbook from memory, read the PHPExcel dcumentation for details.

$objPHPExcel->disconnectWorksheets();

unset($objPHPExcel);

exit();

}

echo 'Something has happened that isn`t right!';

?>