How to modify existing macro enabled excel file in laravel? - php

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');

Related

Creating Excel file using PhpSpreadsheet with default data using Laravel 5.5

I am trying to download excel file with pre-populated data using PhpSpreadsheet in laravel. But I am got empty file downloaded.Here`s my code.
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('B1','testing');
$writer = new Xlsx($spreadsheet);
$filename = 'tessa';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'. $filename .'.Xlsx"');
header('Cache-Control: max-age=0');
$writer->save($filename);
I tried dd($sheet) Here how it looks,it contains my data;
The file downloaded but empty file.Can any one tell me how to solve the issue?
You don't really need to set those headers. But if you must, edit filename to:
$filename = 'tessa.xlsx';
You need to change
$writer->save($filename);
to:
$writer->save('php://output');

PHPExcel missing text boxes

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

PHPExcel Export HTML String is Exporting Entire HTML Page

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.

Codeigniter + PHPExcel Library Issue

I am using PHPExcel library as shown here. But, tt's not working as expected. When I searched, someone has suggested replacing PHPEXcel_IOFactory to IOFactory. It downloads the file, but it couldn't be opened. Any suggestions?
Here is my library code:
Excel.php
require_once APPPATH.'/third_party/PHPExcel.php';
class Excel extends PHPExcel
{
public function __construct()
{
parent::__construct();
}
}
Here is the controller code:
#load our new PHPExcel library
$this->load->library('excel');
$data = $this->report_model->get_all_report_product_info();
$filename = "Output";
# Set the active Excel worksheet to sheet 0
$this->excel->setActiveSheetIndex(0);
$row_count = 2;
foreach ($data as $row) {
$this->excel->getActiveSheet()->setCellValue('A' . $row_count, $row->product_name);
$this->excel->getActiveSheet()->setCellValue('B' . $row_count, $row->product_sku);
$this->excel->getActiveSheet()->setCellValue('C' . $row_count, $row->customer_name);
$this->excel->getActiveSheet()->setCellValue('D' . $row_count, $row->site_url);
$row_count++;
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
header('Cache-Control: max-age=0');
// Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file
$objWriter = IOFactory::createWriter($this->excel, 'Excel5');
// Write the Excel file to filename
$objWriter->save('php://output');
$objWriter = IOFactory::createWriter($this->excel, 'Excel5');
"Excel5" is the old spreadsheet format (used for .XLS files). If you want to create a XLSX file, you need to use "Excel2007".

Downloaded Excel File cannot be open properly using PHPExcel

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');

Categories