How to refresh pivot table in excel using php - php

I have a file in xls format that I need to refresh (use 'refresh all' button in Excel) once a day and then retrieve the data from the pivot table and inset them into the database (MySQL). The file gets data from an external source (retrieve data from sharepoint 2007).
How is the easiest way to do this?
I'm thinking abaout PHP but do I not quite know how you go about it. From what I read PHPExcel does not support this operations.
When you try to use COM I get an error:
Fatal error: in D:\xampp\htdocs\sp\xls\index.php on line 11
And here is a php code:
<?php
// Start Excel
$excel = new COM("Excel.Application") or die ("Could not load Excel.Application");
// Make Excel visible.
$excel->Application->Visible = 1;
// Open workbook
$Workbook = $excel->Workbooks->Open('D:/xampp/htdocs/sp/xls/emails.xls', 'r+') ;
// Refresh all
$Workbook->RefreshAll();
// Save updated excel file out to disk somewhere
$Workbook->SaveAs('D:/xampp/htdocs/sp/xls/emails.xls');
// Close all instances of excel:
$Workbook->Close(false);
unset($Workbook);
$excel->Workbooks->Close();
$excel->Quit();
unset($excel);
?>
I'm using windows 7 and xampp with php 5.5.6
In php.ini I've added this line:
extension=php_com_dotnet.dll
Alternate: is it possible to run *.iqy file generated by sharepoint in php?

I found a workaround to my problem.
Instead, refresh the data by PHP pointed out in a query that collects data during the re-launch of the sheet. To this I added this VBA script save changes and close file.
At the end of the added task scheduler to run the sheet once per day.
The rest of downloading data from Excel using PHPExcel.

Related

Crystal Reports 11.5 with PHP and MySQL

I am new to Crystal Reports, and I am using version Crystal Reports 11.5.
My requirement is as below:
Programming Environment is PHP .
Database is MySQL.
I want to generate PDF reports using Crystal Report + PHP + MySQL.
Currently, I am using COM object to connect to Crystal Report using PHP, and I am able to generate a sample static PDF report.
My main task is to do all processing in PHP by fetching values from MySQL and pass the values to Crystal Reports and generate a PDF. I need assistance to achieve this task. If anybody can provide sample code then it will be much better.
This is what I have so far:
$my_report = "E:\\xampp\\htdocs\\crystal\\Test1.rpt";
$my_pdf = "E:\\xampp\\htdocs\\crystal\\test.pdf";
$o_CrObjectFactory = new COM('CrystalReports11.ObjectFactory.1');
// Create the Crystal Reports Runtime Application.
$o_CrApplication =$o_CrObjectFactory->CreateObject("CrystalDesignRunTime.Application");
//------ Open your rpt file ------
$creport = $o_CrApplication->OpenReport($my_report, 1);
//------ Connect to DB2 DataBase ------
**this is the hard part where I am not able to complete connection to mysql**
$o_CrApplication->LogOnServer('which library','mlims','root','');
//------ Put the values that you want --------
$creport->RecordSelectionFormula="{parameter.id}='1'";
//------ This is very important. DiscardSavedData make a
// Refresh in your data -------
$creport->DiscardSavedData;
//------ Read the records :-P -------
$creport->ReadRecords();
//------ Export to PDF -------
$creport->ExportOptions->DiskFileName=$my_pdf;
$creport->ExportOptions->FormatType=31;
$creport->ExportOptions->DestinationType=1;
$creport->Export(false);
//------ Release the variables
$creport = null;
$crapp = null;
$ObjectFactory = null;
As you see In the above code, I need to connect the Mysql server which I have been trying to do for the last few days. I have tried many examples on the net, but most of them are for SQL Server, not MySQL.
Please follow the steps:
Download the MySQL Connector J jar file. That download should contain a jar file that looks something like:
mysql-connector-java-3.1.14-bin.jar
Add the location of your newly downloaded jar file to the Classpath, as defined in CrystalReports CRConfig.xml file. On a Windows machine, the config file will be located somewhere like:
C:\Program Files\Business Objects\Common\3.5\java\CRConfig.xml
Once you have altered your CRConfig.xml, close and reopen Crystal Reports.
From the menu: File -> New -> Standard Report
In the “Available Data Sources” list, double-click to expand “Create New Connection”
Double-click to expand “JDBC (JNDI)”
Double-click “Make New Connection”
Connection URL: “jdbc:mysql://db.example.com/dbname” (Use your own database host name and db name).
Database Classname: “com.mysql.jdbc.Driver”
Click “Next”
Enter a database user/password combination when prompted.
You should now be able to inspect the tables/columns in the database to begin reporting.
See reference

edit existing excel on web page using php

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();
?>

Read .xlsx file when the file is opened using php

I need to read the data from the .xlsx file when that file is opened without saving it.
The .xlsx file is frequently updating (in every 1 or 2 seconds), so I need last updated data from the file.
<?php
include 'xls/simplexlsx.class.php';
$xlsx = new SimpleXLSX('export_data.xlsx');
$xlxs_value = $xlsx->rows();
echo $xlxs_value;
?>
But when I executed this code, I get data only when the file is last saved, so please help me to read the data without saving the file and it is opened by MS OFFICE.

How to update Excel data? (Refresh all queries)

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.

Open an excel file using COM and save it as .xml file

Im trying the following code:
<?php
$workbook = "D:\b2\\test.XLS";
$sheet = "Sheet1";
#Instantiate the spreadsheet component.
$ex = new COM("Excel.sheet") or Die ("Did not connect");
#Get the application name and version
print "Application name:{$ex->Application->value}<BR>" ;
print "Loaded version: {$ex->Application->version}<BR>";
#Open the workbook that we want to use.
$wkb = $ex->application->Workbooks->Open($workbook) or Die ("Did not open");
#Create a copy of the workbook, so the original workbook will be preserved.
$ex->Application->ActiveWorkbook->SaveAs("D:\b2\Ourtest.xml");
#$ex->Application->Visible = 1; #Uncomment to make Excel visible.
#Optionally, save the modified workbook
$ex->Application->ActiveWorkbook->SaveAs("D:\Ourtest.xml");
#Close all workbooks without questioning
$ex->application->ActiveWorkbook->Close("False");
unset ($ex);
?>
This actually works and creates the Ourtest.xml file. But im getting characters like:
ÐÏࡱá > þÿ þÿÿÿ
I have tried with SaveAs("D:\Ourtest.pdf") and it says the file has been corrupted or incorrectly decoded.
Can anyone help me please?Thanks
That is because you are saving it as Excel Format. Check the Excel documentation for how to save it as an XML document - it might be a separate paramter to SaveAs or a different method alltogether (Export*).
EDIT: It seems SaveAs is the right method to use. Check the msdn documentation here. You probably want to specify the second parameter FileFormat. Maybe setting it to the XlFileFormat.xlXMLSpreadsheet value?
Problem solved by using
$ex->Application->ActiveWorkbook->SaveAs("D:\Ourtest.xml",46);
46 is the value for xlXMLSpreadsheet
Thanks everyone

Categories