I have an excel template which gets populated with data in one of the sheets. That data is used to update the charts in other sheets of the same excel file. the updated file now gets downloaded automatically. I use laravel-excel to update the data sheet but when i open the file that is downloaded, i find no charts in the sheets.The sheets just become empty. Although, the data gets updated in the other sheets, the charts are getting disappeared. Here's the code that i have written for the same.
Excel::load('maintemplate.xlsx', function($reader) use($data1, $data2)
{
$reader->setIncludeCharts(TRUE);
$sheet = $reader->sheet('Sales');
$sheet->fromArray($data1);
$sheet1 = $reader->sheet('Pivot');
$sheet1->fromArray($data2, null, 'D4', true);
})->download('xlsx');
maintemplate.xlsx is the template file i have which gets populated with data once i run the php code. I have done lot of search for this kind of error on net and found about setIncludeCharts(); Even after setting it to true , i couldn't solve the problem. I even checked the downloaded file with different versions of excel.The problem persists in all of the versions.Please, someone help me with this.
Related
So I installed Laravel Debugbar today in an attempt to make my local dev a better experience, and for the most part, it has. An issue has arisen though when I try to download an Excel generated from PhpOffice\PhpSpreadsheet (https://phpspreadsheet.readthedocs.io/en/latest/) Here's a snippet of the code in question:
$excelFile = new Spreadsheet();
// Load stuff from DB in Sheets, etc. etc.
$writer = new Xlsx($excelFile);
$filename = 'testing.xlsx';
$writer->save('php://output');
This works great, and the content is loaded into and downloaded as an .xlsx file without issue. When I open the file, I get this alert:
We found a problem with some content in 'testing.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.
I click "Yes", and then I get this alert:
Excel was able to open the file by repairing or removing the unreadable content.
I click "Delete" and my file is opened, with nothing deleted (compared file against previous commit on master branch, no change).
Now here's the fun part, I only get this alert when Debugbar is enabled. In .env, I have DEBUGBAR_ENABLED=true, and if I set that to DEBUGBAR_ENABLED=false and reload/redownload, I don't get the alert. Has anyone seen this issue before? Why would Debugbar be messing with this? Is it an issue with $writer->save('php://output'); being polluted by Debugbar's injection?
Sidenote, this will be a non-issue in production, as Debugbar is a require-dev dependence, but I'm just curious if I can avoid this during local development or not.
Apparently, this has been asked and answered on the official documentation; was just a little tricky to find. I was correct about object pollution via debugbar, and the simply soultion was to add exit(); after $writer->save();:
$excelFile = new Spreadsheet();
// Load stuff from DB in Sheets, etc. etc.
$writer = new Xlsx($excelFile);
$filename = 'testing.xlsx';
$writer->save('php://output');
exit();
There were a couple more notes about ob_clean() and/or ob_end_clean(), ob_flush(), etc etc., but none of those worked with Laravel + Debugbar. For reference:
https://github.com/PHPOffice/PhpSpreadsheet/issues/217
Sidenote, this prevents Debugbar from handling the GET or POST request associated with the Excel download, so there's some give and take required. Simply commenting out the exit() locally will allow debugging, but the Excel will be marked as corrupt. Simple repeat action with exit() on will handle.
Does anyone have an idea to create the dynamic form with uploaded excel file.
We need to facilitate users to add data in the pre-uploaded file. Can anyone help with this? We are doing in this way:
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'HTML');
$objPHPExcelWriter->save('php://output');
But this above code only displays structure of excel on the web still we're unable to edit it.
you will need 2 pear packages
PHP-ExcelReader package
Spreadsheet_Excel_Writer package
What you need to do is read first the excel file use PHP-ExcelReader package It reads the binary format of XLS files directly and can return values and formats from any cell. http://code.google.com/p/php-excel-reader/
read the excel file
$data = new Spreadsheet_Excel_Reader("test.xls");
show the data of the file
$data->dump($row_numbers=false,$col_letters=false,$sheet=0,$table_class='excel')
Once you have stored the data in a variable save the data in another file this time you will use the The Spreadsheet_Excel_Writer package https://github.com/pear/Spreadsheet_Excel_Writer
<?php
require_once 'Spreadsheet/Excel/Writer.php';
$workbook = new Spreadsheet_Excel_Writer('test.xls');
$worksheet =& $workbook->addWorksheet('My first worksheet');
if (PEAR::isError($worksheet)) {
die($worksheet->getMessage());
}
$workbook->close();
?>
I have a Excel xlsm file with a few queries.
Currently I open it every day and click on the "Refresh All" command in the "Data" tab. I want this to be done automatically. I wrote a script in python (I'm a newbie in Python).
The problem is that after the data is refreshed and the Excel file has been saved, the refreshed data is not visible (I know that refreshing works because if I prevent to save and close the Excel file, the refreshed data is visible in the file )
The weird thing is that saving also works fine because when I try to modify cell B2 from 'config' it is changed...
Where is the problem?
import win32com.client
import datetime
from datetime import date, timedelta
yesterday = date.today() - timedelta(1)
office = win32com.client.Dispatch("Excel.Application")
wb = office.Workbooks.Open(r'\\server\CC_source.xlsm')
office.DisplayAlerts = True
office.Visible = True
wb.RefreshAll()
sh = wb.Worksheets("config")
sh.Cells(2,2).Value = wczoraj
wb.Close(True)
Maybe someone can recommend another script? For example, Powershell? PHP?
Or an other library for Python3?
Since you requested another script to perform this action, I recommend PowerShell. As a simple test, I created an Excel document, SheetToRefresh.xlsx, which has a data connection to a simple xml file. However, the script below will work for any data source that the Excel document is connected to. I have tested this on my own machine, the data refresh and save persists, as expected.
RefreshExcel.ps1
#Set the file path (can be a network location)
$filePath = "C:\Scripts\SheetToRefresh.xlsx"
#Create the Excel Object
$excelObj = New-Object -ComObject Excel.Application
#Make Excel visible. Set to $false if you want this done in the background
$excelObj.Visible = $true
#Open the workbook
$workBook = $excelObj.Workbooks.Open($filePath)
#Focus on the top row of the "Data" worksheet
#Note: This is only for visibility, it does not affect the data refresh
$workSheet = $workBook.Sheets.Item("Data")
$workSheet.Select()
#Refresh all data in this workbook
$workBook.RefreshAll()
#Save any changes done by the refresh
$workBook.Save()
#Uncomment this line if you want Excel to close on its own
#$excelObj.Quit()
To refresh your query then your pivot, in Excel go to data> queries and connections> right click query for query properties> uncheck the enable background refresh.
Greetings !!
I have to insert a logo(image) on the row[0],column[0].I am using "Spreadsheet_Excel_Writer" for that.i tried its insertBitmap() methode ,program working fine but it doesn't show the bitmap image on xls sheet,instead blank row. what could be reason ? can you please let me know the exact string format for the argument. Is there any other way to insert image on xls sheet using PHP5.i am very new to php ,it will be a great help .
Have a nice time ahead !!
[edit]
Here is the code, as per Aman's comment below:
$sew =& new Spreadsheet_Excel_Writer ();
$worksheet =& $sew->addWorksheet (substr (strval ($name).strval ($sht), 0, 31));
$worksheet->insertBitmap ($row,$col,$image,$x,$y,$scale_x,$scale_y);
I never could get Spreadsheet_Excel_Writer to work properly with image insertions. Not sure if it's a bug in the library or what. But in any case, S_E_W is hideously outdated, you should switch to PHPExcel instead, which supports recent Excel formats (including .xlsx) for reading AND writing, whereas S_E_W is limited to BIFF 5.0, which is Excel '95 (or thereabouts) and only supports writing.
I've just ran a test using Spreadsheet_Excel_Writer. SEW saves the excel file using BIFF5 format. Open Office Calc will read images from BIFF8, but not from BIFF5 files.
EDIT
Further testing:
Setting SEW to write BIFF8 by using $workbook->setVersion(8); still doesn't write the bitmap image correctly as a BIFF8 file. It would seem that unless you want to rewrite SEW to store images correctly for BIFF8, then you won't see them when opening the file in OOCalc... without reading through the OOCalc or SEW code, I couldn't say what the problem is. Nor does Gnumeric read the image when the file is saved as BIFF5, but it will display the image correctly when the file is saved as BIFF8.
I've gotten this to work. The thing is, this writer is EXTREMELY sensitive to cell overwriting.
Your syntax is correct.
Things to look out for:
Make sure the file is 24 bit BMP. That's the only thing this writer supports.
Make sure nothing overwrites the cell where you place the image.
Make sure that the image path is correct.
And make sure the scale is set. If it's not set, it goes to 0 which doesn't display the image. The default is noted as 1, but it's not.
Greetings !!
I have to insert a logo(image) on the row[0],column[0].I am using "Spreadsheet_Excel_Writer" for that.i tried its insertBitmap() methode ,program working fine but it doesn't show the bitmap image on xls sheet,instead blank row. what could be reason ? can you please let me know the exact string format for the argument. Is there any other way to insert image on xls sheet using PHP5.i am very new to php ,it will be a great help .
Have a nice time ahead !!
[edit]
Here is the code, as per Aman's comment below:
$sew =& new Spreadsheet_Excel_Writer ();
$worksheet =& $sew->addWorksheet (substr (strval ($name).strval ($sht), 0, 31));
$worksheet->insertBitmap ($row,$col,$image,$x,$y,$scale_x,$scale_y);
I never could get Spreadsheet_Excel_Writer to work properly with image insertions. Not sure if it's a bug in the library or what. But in any case, S_E_W is hideously outdated, you should switch to PHPExcel instead, which supports recent Excel formats (including .xlsx) for reading AND writing, whereas S_E_W is limited to BIFF 5.0, which is Excel '95 (or thereabouts) and only supports writing.
I've just ran a test using Spreadsheet_Excel_Writer. SEW saves the excel file using BIFF5 format. Open Office Calc will read images from BIFF8, but not from BIFF5 files.
EDIT
Further testing:
Setting SEW to write BIFF8 by using $workbook->setVersion(8); still doesn't write the bitmap image correctly as a BIFF8 file. It would seem that unless you want to rewrite SEW to store images correctly for BIFF8, then you won't see them when opening the file in OOCalc... without reading through the OOCalc or SEW code, I couldn't say what the problem is. Nor does Gnumeric read the image when the file is saved as BIFF5, but it will display the image correctly when the file is saved as BIFF8.
I've gotten this to work. The thing is, this writer is EXTREMELY sensitive to cell overwriting.
Your syntax is correct.
Things to look out for:
Make sure the file is 24 bit BMP. That's the only thing this writer supports.
Make sure nothing overwrites the cell where you place the image.
Make sure that the image path is correct.
And make sure the scale is set. If it's not set, it goes to 0 which doesn't display the image. The default is noted as 1, but it's not.