In my project, I make exporting data to excel 2007 using PhpExcel 1.8.0.
I tried it as the followings code.
Excel File is saved in Client side but when I open the downloaded Excel file, I get the warning message "We found a problem with some content in 'filename.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes."
Could anyone help me to solve this problem. Thanks in advance!!!
$_read_sheet = EXCEL_SHEET_PATH . "template.xlsx";
$ym_dirname = date("Y") . '_' . date("m");
$output_file_name = "MyFile_" . date("YmdHis", time());
$output_file = $output_file_name . ".xlsx";
$output_file = mb_convert_encoding($output_file, "SJIS", "UTF-8");
$reader = PHPExcel_IOFactory::createReader('Excel2007');
$xl = $reader->load("$_read_sheet");
$xl->setActiveSheetIndex(0);
$sheet = $xl->getActiveSheet();
$sheet->setCellValue("AV3", date("Y/m/d"));
// redirect output to client browser
header('Content-Type: application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$output_file.'"');
header('Cache-Control: max-age=0');
$writer = PHPExcel_IOFactory::createWriter($xl, 'Excel2007');
// Write file to the browser
$writer->save('php://output');
Related
I am using Php spreadsheet api for modify existing macro enabled excel file. but after download same file with update first sheet data, remaining macro file layout missing and corrupted.
Please suggest if i am wrong ?
Code:
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setRealFormat(true);
$spreadsheet = $reader->load($inputFileName);
$position_at_row1 = $position_at_row+1;
$count_new = $position_at_row1 +24;
$sheet = $spreadsheet->getSheetByName('Tabelle1');
$sheet = $spreadsheet->getSheet(0);
$sheet->getCell('A1')->setValue(10);
$sheet->getCell('A2')->setValue(10);
$writer = new Xlsx($spreadsheet);
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="' . urlencode($original_file_name) . '"');
$writer->save('php://output');
I need to open a existing xlsx file, that contains text boxes (those created with the predefined option like circles, squares, etc), and I need to write just behind this objects (they are transparent).
The code bellow does everything fine, except that downloads the new file without the boxes.
if(file_exists("app/file.xlsx")){
$objTpl = PHPExcel_IOFactory::load("app/file.xlsx");
$objTpl->setActiveSheetIndex(1);
$objTpl->getActiveSheet()->setCellValue('A1', 'x');
$objTpl->getActiveSheet()->setCellValue('A2', 'y');
$objTpl->getActiveSheet()->setCellValue('A3', 'z');
$filename = 'new_file.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel2007');
ob_end_clean();
$ret = $objWriter->save('php://output');
exit;
}
you can probably do this via the COM dotnet php module
simply open the xlsx file:
$excel = new COM("Excel.Application") or die ("Could not initialise Object.");
$excel->Visible = 1;
$excel->DisplayAlerts = 0;
$Workbook = $excel->Workbooks->Open($file) or die("ERROR: Unable to open " . $file . "!\r\n");
$Worksheet = $Workbook->ActiveSheet;
and from here on out alter your file and then use the saveas function. its all +- well documented in the developers doc from microsoft:
https://msdn.microsoft.com/EN-US/library/office/ff198122.aspx
I have a table on an html page that I need to export. I have searched other examples on here but nothing that works for me.
Here is what I have.
$filename = "DownloadReport";
//$table = dbGrid($q);
$table = "<div><table><tbody><tr><td>cars</td><td>cars</td><td>cars</td></tr></tbody></table></div>";
$tmpfile = tempnam(sys_get_temp_dir(), 'html');
file_put_contents($tmpfile, $table);
$excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
$objPHPExcel = $excelHTMLReader->load($tmpfile);
$objPHPExcel->getActiveSheet()->setTitle('anynameyouwant');
unlink($tmpfile);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '-' . date("y.m.d") . '.xlsx"');
header('Cache-Control: max-age=0');
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$writer->save('php://output');
exit;
It properly downloads the file. But for some reason its saving my entire html page with javascript and css files missing. I only need to export the table string. I have a dbGrid function to create my table but for testing I have a simple table string.
How can I export just that table string, not my entire html page.
I have a code in php CodeIgniter that extracts data from database and generates an xls file via PHPExcel.
The problem is that whenever I upload the code into another server, it generates an .xls file with 0kb, and the error while opening is:
"Excel cannot open the file "____" because the file format or file extension not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file".
The incoming data is from same table and all the libraries used are also same.
My questions:
1. Are there any r/w permission applied by the server which makes the phpexcel file 0kb? I am using CodeIgniter.
2. Is there any way to see what is written in the PHPExcel object?
3. Are there any things I am missing out?
My code that generates xls file is:
// Set header and footer. When no different headers for odd/even are used, odd header is assumed.
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BInvoice&RPrinted on &D');
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');
// Set page orientation and size
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$this->load->library('PHPExcel/PHPExcel_IOFactory');
$file_name = $uri_year . "-" . $uri_month . "_staff_report.xlsx";
// Redirect output to a client web browser (Excel2007)
//ob_end_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $file_name);
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
// ob_end_clean();
$objWriter->save('php://output');
Thank you in advance.
My output as displayed in the test server is:
Solution:
It turns out that the php version of the server1 is 5.2.9
The php version of server2 is 5.1.6
Error generated in server2:
Fatal error: Class 'ZipArchive' not found in /var/www/html/APP_NAME/application/libraries/PHPExcel/Writer/Excel2007.php on line 225
The ZipArchive module requires php version >5.2
After the update, it works.
Thank you guys for your suggestions.
It outputs 0kb or "Excel cannot open the file "_ because the execution of the script encountered unusual error. Enable your error reporting, put this at the top of your script:
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
Yes, you may need to check the proper r/w permission and ownership of PHPExcel.php but the output file does not need anymore.
I have tested your code and it works fine..
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
/** Include PHPExcel */
require_once('PHPExcel.php');
$objPHPExcel = new PHPExcel();
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Set header and footer. When no different headers for odd/even are used, odd header is assumed.
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BInvoice&RPrinted on &D');
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');
// Set page orientation and size
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
// $this->load->library('PHPExcel/PHPExcel_IOFactory');
$uri_year = "2015";
$uri_month = "now";
$file_name = $uri_year . "-" . $uri_month . "_staff_report.xlsx";
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', "This is a test");
// Redirect output to a client web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $file_name);
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
// ob_end_clean();
$objWriter->save('php://output');
?>
You may need to check if you have the right libraries for Excel2007, i have changed it to Excel5, but you can use Excel2007 and it works fine for me.
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
please check following code tested:
public function demo()
{
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** Include PHPExcel */
$this->load->library('PHPExcel');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("MokNathal")
->setLastModifiedBy("MokNathal")
->setTitle("STAFF REPORT")
->setSubject("STAFF REPORT")
->setDescription("STAFF REPORT")
->setKeywords("STAFF REPORT")
->setCategory("STAFF REPORT");
$objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
$objPHPExcel->setActiveSheetIndex(0)
->getCell('A1')->setValue("Name of staff:aaaaaa Lname");
$objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->mergeCells('A2:F2');
$objPHPExcel->setActiveSheetIndex(0)
->getCell('A2')->setValue("Group:Sunday Holiday Plan");
$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->mergeCells('A3:F3');
$objPHPExcel->setActiveSheetIndex(0)
->getCell('A3')->setValue("Login Details");
$objPHPExcel->getActiveSheet()->getStyle('A3:F3')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->mergeCells('A4:F4');
$objPHPExcel->setActiveSheetIndex(0)
->getCell('A4')->setValue("Checkin time:09:05:00am");
$objPHPExcel->getActiveSheet()->getStyle('A4:F4')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A3')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A4')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A5')->getFont()->setBold(true);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A5', 'Year')
->setCellValue('B5', 'Month')
->setCellValue('C5', 'Day')
->setCellValue('D5', 'Login Time')
->setCellValue('E5', 'Logout Time')
->setCellValue('F5', 'Login Status');
$objPHPExcel->getActiveSheet()->getStyle('A5:F5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A5')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('B5')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('C5')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('D5')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('E5')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('F5')->getFont()->setBold(true);
$i=6;
$uri_year=2071;
$uri_month=4;
for($j=1;$j<=30;$j++)
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i, $uri_year)
->setCellValue('B'.$i, $uri_month)
->setCellValue('C'.$i, $j);
$i++;
}
foreach(range('A','F') as $columnID) {
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
->setAutoSize(true);
}
$objPHPExcel->getActiveSheet()->setTitle('Trasaction List');
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=".$uri_year."-".$uri_month."_staff_report.xlsx\"");
header("Cache-Control: max-age=0");
$objWriter->save("php://output");
}
I am trying to download excel file using PHPExcel, it's like it download the excel file nicely but the data in excel file is all crap.. it's not what i expected. I passed very basic methods to test my excel sheet data output.
here is the code i'am trying
else if($request->p['type'] == 'excel')
{
$report_type_name = "Graph Survey Report";
$ExcelReport = new ExcelApExport($sections, $group_definition, $questions, $sample_corrections);
$objPHPExcel = $ExcelReport->export($sets, $disp_filter);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; "
. escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xls"));
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
also I make object here and call phpexcel methods here
public function export($Sets, $disp_filter)
{
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("Offic excel Test Document");
$objPHPExcel->getProperties()->setSubject(" Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for XLS, generated using PHP classes.");
//echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');
return $objPHPExcel;
}
please can you suggest me why this crap data is showing up in my file instead of expected data.
thanks in advance
It needs basically
ob_clean();
before saving object.
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
This is the writer for .xlsx files. (See: https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xlsx.php)
So the right header would be:
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; "
. escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xlsx")); //.xlsX!
The writer for old .xls-files is:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
See: https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xls.php
I ran into this problem recently and found another post here that is very helpful if ur still having the problem after using ob_clean()
check this post