PHPExcel missing text boxes - php

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

Related

How to modify existing macro enabled excel file in laravel?

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

PHP fails to download Excel file that is generated by PHP Excel

I want to download excel file (instead of saving it). Since I want to export HTML table I am using PHPExcel_reader_HTML to read contents of HTML table. Excel file is created perfectly when I want to save it but when I want to download it, nothing happens. Does someone knows where's the problem?
Code:
<?php
// Load the table view into a variable
$html = $_POST["table"];
require_once('php/PHPExcel/Classes/PHPExcel.php');
// Put the html into a temporary file
$objPHPExcel = new PHPExcel();
$tmpfile = time().'.html';
file_put_contents($tmpfile, $html);
// Read the contents of the file into PHPExcel Reader class
$reader = new PHPExcel_Reader_HTML;
$content = $reader->load($tmpfile);
// Pass to writer and output as needed
$objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"results.xlsx\"");
header("Cache-Control: max-age=0");
$objWriter->save("php://output");
ob_clean();
// Delete temporary file
unlink($tmpfile);
?>

Using phpexcel to export MySql to excel

Hi everyone I have created a program to export MySQL data to excel but at some point I am getting the error the time I am trying to open the excel file.
The error: The file you are trying to open is in a different format,than specified by the file extension.Verify that the file is not corrupt and is from the trusted source before opening the file.Do you want to open the file now?
When I click yes i get the some funny characters on that excel file.
Here is my code:
include 'setup.php';
_connectsurvey(); //Database
require_once 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
$query = "SELECT * FROM questionanswer";
$result = mysql_query($query) or die(mysql_error());
$objPHPExcel = new PHPExcel();
$rowCount = 1;
while($row = mysql_fetch_array($result)){
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['gender']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['city']);
$rowCount++;
pr($objPHPExcel);
}
header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="survey.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
Near the bottom when you name the file use file extension .xlsx it is the proper file extension for openxml based documents from office 2007 should fix your issue.
header('Content-Disposition: attachment;filename="survey.xlsx"');
If you absolutely need to use xls change the output type to Excel2003 but then you need to change the content type header to match that as well.

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

PHPExcel: Download the Excel file on the client side

The problem was solved: For other users that may have this problem - notice the encoding of the PHP file. If you use PHPExcel it must be ANSII encoding and not UTF8, otherwise the EXCEL will be downloaded corruptly. The Headers that were added (answer 1) solved the problem after i changed the encoding of the file itself.
I am using PHPExcel in order to create an EXCEL from a table in MYSQL DB, so the user can download it to his computer.
The code bellow creates a correct Excel file but the problem is that it is downloaded to my server. I read in PHPExcel manual that i need to add headers:
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name.'.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
But if i do that, the downloaded file is:
1. Has some jibrish inside
2. Says that Excel needs to fix it because the file is not good.
The problem is that this file is saved as UTF8 and if i encode it as ANSI then it is working properly but of course it is a manual change and i need a working properly excel to reach the users.
What is the bug?
My code that works (but download the file to the server):
<?php
include 'connection.php';
include 'checkUser.php';
//Getting all the needed information from the DB
$task_id=$_GET['id'];
$query2 = "SELECT * FROM projects WHERE ProjectID=$task_id";
$data2 = mysqli_query($con, $query2);
$row = mysqli_fetch_array($data2);
$project_type = $row['ProjectType'];
$project_name = $row['ProjectName'];
switch ($project_type){
case 2: $NumberOfRows=22; $project = "slivedetails"; break;
case 3: $NumberOfRows=30; $project = "plivedetails"; break;
default: $NumberOfRows=0; $project = "none";
}
//column names
if ($project="slivedetails"){
$ColumnNames = mysqli_query($con,"SHOW COLUMNS FROM slivedetails") or die("mysql error");
}
else if ($project="plivedetails"){
$ColumnNames = mysqli_query($con, "SHOW COLUMNS FROM plivedetails") or die("mysql error");
}
$query = "SELECT * FROM $project WHERE TaskID=$task_id";
$data = mysqli_query($con, $query);
$num_rows = mysqli_num_rows($data);
/** Include PHPExcel */
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel.php';
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel/IOFactory.php';
// create new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel = new PHPExcel();
// writer already created the first sheet for us, let's get it
$objSheet = $objPHPExcel->getActiveSheet();
// rename the sheet
$objSheet->setTitle('Task Results');
// let's bold and size the header font and write the header
// as you can see, we can specify a range of cells, like here: cells from A1 to A4
$objSheet->getStyle('A1:AD1')->getFont()->setBold(true)->setSize(12);
$char = 65;
// write header]
for ($i=1;$i<=$NumberOfRows;$i++){
$col_name = mysqli_fetch_array($ColumnNames);
$objSheet->getCell(chr($char).'1')->setValue($col_name['Field']);
$char++;
}
// Now we need to get the data from the DB. While we have a row in the result:
$rowIterator=2; //our row number. We begin from 2 because the first one is the title.
while ($RowInfo = mysqli_fetch_array($data)){
//We will fill the information based on the amount of columns:
$char = 65; //we set the first char as column A
for ($i=0;$i<$NumberOfRows;$i++){
$objSheet->getCell(chr($char).$rowIterator)->setValue($RowInfo[$i]);
$char++;
}
$rowIterator++;
}
// autosize the columns
$char = 65;
for ($i=1;$i<=$NumberOfRows;$i++){
$objSheet->getColumnDimension(chr($char))->setAutoSize(true);
$char++;
}
// create the writer
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
$objWriter->save('results.xlsx');
?>
Remove the following inclusion:
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel/IOFactory.php';
You declared the object twice. Remove one of them:
// create new PHPExcel object
$objPHPExcel = new PHPExcel();
Insert the following headers just before creating the Writer:
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"results.xlsx\"");
header("Cache-Control: max-age=0");
Instead of the following (which actually saves the file in the server):
$objWriter->save('results.xlsx');
Insert the following (which will create the downloadable file):
$objWriter->save("php://output");
This should solve the gibberish text. If you still get such text, insert the following code before the last line ($objWriter->save("php://output");):
ob_clean();
This worked for me. Hope it helps.
This should work, try to modify your code like this:
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=my_excel_filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
flush();
require_once 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
// here fill data to your Excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
Alex, I had the same problem. I did all of them but again result was the same. I just changed
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
to
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
and changed all .xlsx to .xls.
Hope It will help.
Download PHPExcel-1.8(from github) and replace it with the old PHPExcel.
Rename PHPExcel-1.8 to PHPExcel. In my code I have included require '../library/excel/PHPExcel.php';
I deleted all contents from this file (PHPExcel.php) and added only one line of code
require 'PHPExcel/Classes/PHPExcel.php';
(Note: I uploaded PHPExcel in the folder library/excel)
Works perfectly

Categories